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

VIK*_*AUR -1 python bots python-3.x discord discord.py

我有一个用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)
Run Code Online (Sandbox Code Playgroud)

这是控制台输出:

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
Run Code Online (Sandbox Code Playgroud)

你能帮助我吗?谢谢。

Pat*_*ugh 5

你不应该使用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)
Run Code Online (Sandbox Code Playgroud)