python-3.x 如何在www.example.com邮件之间添加暂停discord.py?

oknwwptz  于 2022-11-26  发布在  Python
关注(0)|答案(2)|浏览(117)

我有一个用python编写的discord机器人。我想让机器人说笑话的第一部分,一个time.sleep,然后是笑话的第二部分(都在同一个变量中)。这是我的代码:
下面是控制台输出:

tkqqtvp1

tkqqtvp11#

你根本不应该使用time.sleep,因为它不适合asyncio,而discord.py是基于asyncio构建的。相反,我们应该有一个配对列表,随机选择一个,然后使用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)
vi4fp9gy

vi4fp9gy2#

你的做法全错了。

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)"会给予错误,因为您将其视为字符串。

相关问题