繁体   English   中英

如何在discord.py消息之间添加停顿?

[英]How do I add a pause between discord.py messages?

我有一个用python编程的discord机器人。 我希望机器人说笑话的第一部分,即time.sleep,然后讲笑话的第二部分(都在同一个变量中)。 这是我的代码:

if message.content.startswith('!joke'):
    a = 'Can a kangaroo jump higher than a house?' + time.sleep(3) + 'Of course, a house doesn’t jump at all.'
    b = 'Anton, do you think I’m a bad mother?' + time.sleep(3) + 'My name is Paul.'
    c = 'Why can\'t cats work with a computer?' + time.sleep(3) + 'Because they get too distracted chasing the mouse around, haha!'
    d = 'My dog used to chase people on a bike a lot.' + time.sleep(3) + 'It got so bad, finally I had to take his bike away.'
    e = 'What do Italian ghosts have for dinner?' + time.sleep(3) + 'Spook-hetti!'
    msg = random.choice([a, b, c, d, e]).format(message)
    await client.send_message(message.channel, msg)

这是控制台输出:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Python\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "archie_official.py", line 138, in on_message
    a = 'Can a kangaroo jump higher than a house?' + time.sleep(3) + 'Of course, a house doesn’t jump at all.'
TypeError: must be str, not NoneType

你能帮助我吗? 谢谢。

你不应该使用time.sleep所有的,因为它不会发挥好asyncio ,这discord.py是建立在。 相反,我们应该有一个成对的列表,随机选择一个,然后使用asyncio.sleep在消息之间暂停。

jokes = [
    ('Can a kangaroo jump higher than a house?', 'Of course, a house doesn’t jump at all.'),
    ('Anton, do you think I’m a bad mother?', 'My name is Paul.'),
    ('Why can\'t cats work with a computer?', 'Because they get too distracted chasing the mouse around, haha!'),
    ('My dog used to chase people on a bike a lot.', 'It got so bad, finally I had to take his bike away.'),
    ('What do Italian ghosts have for dinner?', 'Spook-hetti!')]

setup, punchline = random.choice(jokes)
await client.send_message(message.channel, setup)
await asyncio.sleep(3)
await client.send_message(message.channel, punchline)

你这一切都错了。

a = 'Can a kangaroo jump higher than a house?' + time.sleep(3) + 'Of course, a house doesn’t jump at all.' 

将不起作用,原因是因为您希望time.sleep(3)是一个字符串,对于您要从中获取的每个字符串(据我所知)。 需要做以下

await bot.say("Can a kangaroo jump higher than a house?")
time.sleep(3)
await bot.say('Of course, a house doesn’t jump at all.' )

当然,您需要将bot更改为客户端,但这基本上是您必须要做的。

不能正常工作的原因:执行a = "string" +func()+"string2 ; print(a)"会产生错误,因为您将所有字符串都视为字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM