Discord.py当wait_for答案错误时,让bot发送一些东西



我想让我的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 docs
  • discord.Message- discord.py docs

最新更新