在给定提示(Message1)时,用户应该能够响应左侧、右侧或静止不动,discord机器人将提供自定义消息(Message2-4中的任意一个)。我尝试创建多个检查来检测不同的用户响应,但都无济于事。我在网上寻找了其他解决方案,其中一些提到了使用asyncio,但我不太熟悉它,即使使用它,也无法解决问题。感谢任何帮助,我是python的新手。谢谢。
代码如下:
if message.content.startswith('gametest'):
channel = message.channel
await channel.send('Would you like to play?')
def check(m):
return m.content == 'yes' and m.channel == channel
msg = await client.wait_for('message', check=check)
await channel.send('Message1')
def check2(m):
return m.content == 'left' and m.channel == channel
def check3(m):
return m.content == 'right' and m.channel == channel
def check4(m):
return m.content == 'stay still' and m.channel == channel
msg2 = await client.wait_for('message', timeout=30.0, check=check2)
await channel.send('Message2')
msg3 = await client.wait_for('message', timeout=30.0, check=check3)
await channel.send('Message3')
msg4 = await client.wait_for('message', timeout=30.0, check=check4)
await channel.send('Message4')发布于 2021-02-10 20:34:16
您可以比较消息内容是否在列表中
>>> content = 'right'
>>> valid_responses = ['right', 'left', 'stay still']
>>> content in valid_responses
True您可以在check函数中使用相同的原则
def check(m):
return m.content in ['right', 'left', 'stay still'] and m.channel == channel如果希望它不区分大小写,可以使用str.lower()方法
def check(m):
return m.content.lower() in ['right', 'left', 'stay still'] and m.channel == channelhttps://stackoverflow.com/questions/66135232
复制相似问题