我想让我的bot在wait_for答案错误时发送消息
@client.event
async def on_message(message):
if message.content.startswith('$greet'):
channel = message.channel
await channel.send('Say hello!')
def check(m):
return m.content == 'hello' and m.channel == channel
msg = await client.wait_for('message', check=check)
await channel.send('Hello {.author}!'.format(msg))
所以当用户回答不是hello时,bot也应该发送消息
怎么做?
与其将m.content
放入检查函数本身,不如在此之外使用msg.content
调用它。这是因为变量msg
仍然是一个消息对象。请查看下面修改后的代码。
def check(m):
# we won't check the content here...
return m.author == message.author and m.channel == message.channel
msg = await client.wait_for('message', check=check)
# ...instead we will check the content using an if-else statement
if msg.content == 'hello':
await message.channel.send("Hello {.author}!".format(msg))
else:
await message.channel.send("You did not say hello...")
一些有用的文档:
- Python条件- w3schools
wait_for
- discord.py docsdiscord.Message
- discord.py docs