I have a discord bot programmed in python. I want the bot to say the first part of the joke, a time.sleep and then the second part of the joke (both in the same variable). This is my code:
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)
And this is the console output:
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
Can you help me? Thank you.
答案 0 :(得分:4)
您根本不应该使用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)
答案 1 :(得分:0)
Your going about it all wrong.
a = 'Can a kangaroo jump higher than a house?' + time.sleep(3) + 'Of course, a house doesn’t jump at all.'
Will not work, The reason for this is because you want time.sleep(3)
to be a string, for each of these you would from (what I know). Need to do the following
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.' )
Of course, you would need to change bot to client but this is basically what you have to do.
Reason it wouldn't work:
Doing a = "string" +func()+"string2 ; print(a)"
would give a error because your treating it all like a string.