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



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

这是控制台输出:

你根本不应该使用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.' )

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

它不起作用的原因: 这样做a = "string" +func()+"string2 ; print(a)"会给出错误,因为您将它视为字符串。

最新更新