我试图让我的代码来获取用户的输入,并有两个选项的代码必须检查,但代码只注册了第一个选项。
我试过不同的语法像(content=('m!mode computer' or 'm!mode player'))
和他们没有工作。
async def AgainstWho():
global mode
await client.send_message(message.channel, content='Play against the computer or another player?\n')
mode = await client.wait_for_message(content=('m!mode computer' or 'm!mode player'))
if mode == 'm!mode computer':
mode = 1
if mode == 'm!mode player':
mode = 2
如果m!mode player
代码的用户类型没有做任何事情,但m!mode computer
工作完全正常。
>>> 'm!mode computer' or 'm!mode player'
'm!mode computer'
这就是为什么。我假设这是discord.py,according to the docs你想要的是这样的:
mode = await client.wait_for_message(check=lambda m: return m.content.startswith('m!mode'))
等。
在做'm!mode computer' or 'm!mode player'
; or
将只是True
返回第一个字符串作为它的布尔值,也就是说,任何非空字符串;所以它会默认为第一个(在这里'm!mode computer'
)。
你需要检查的内容startswith 'm!mode '
和下面的“字”完全符合你的任何选项:
async def AgainstWho():
global mode
await client.send_message(message.channel, content='Play against the computer or another player?\n')
mode = await client.wait_for_message(check=lambda m: return m.content.startswith('m!mode ') and m.content.split(' ')[1] in ('player', 'computer'))
if mode == 'm!mode computer':
mode = 1
if mode == 'm!mode player':
mode = 2
这仅检查命令的前两个“单词”。 “M模式的玩家!”将工作;但这不是“M!玩家模式别的背后”。如果你想避免这种行为,它降低到严密指令拨打电话;您还可以检查len(m.content.split(' ')) == 2
。
此外,应避免使用global
。传递变量作为参数传递给该函数或使其与变量作为属性的对象的方法。