python - 多场景用户输入 Discord.py
问题描述
根据给定的提示(消息 1),用户应该能够向左、向右或保持静止响应,并且不和谐机器人将提供自定义消息(消息 2-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')
解决方案
您可以比较消息内容是否在列表中
>>> content = 'right'
>>> valid_responses = ['right', 'left', 'stay still']
>>> content in valid_responses
True
您可以在检查功能中使用相同的原理
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 == channel
推荐阅读
- python - 使用 PyODBC 将自定义类型参数传递给存储过程
- stl - STL 连通性检查
- javascript - 如何根据反应中的唯一ID获取API结构化数据
- tuples - 试图在 OCaml 中创建一个全局变量
- python - 一个问题的美丽汤多个答案
- php - 下载已使用 post 方法提交的文件
- java - Hazelcast 集群:无法使用 Kubernetes API 发现 Hazelcast 成员
- string - 如何确定字符串是否是另一个字符串的子字符串
- azure - Azure 函数 - 挂起队列触发器
- snowflake-cloud-data-platform - 如果我有会话 ID,如何获取会话参数详细信息