我正在创建带有代码的机器人。但是,如何检查邮件内容是否包含列表中的代码之一?
这就是我现在拥有的:
@bot.command(name='get')
async def get(ctx):
await ctx.send('Enter your code')
msg = await bot.wait_for('message')
if msg.content == ['code1', 'code2']:
await ctx.send('Here is your treasure!')
else:
await ctx.send('That code is invalid')
答案 0 :(得分:1)
您可以使用in
关键字:
>>> a = 2
>>> mylist = [1, 2, 3]
>>> a in mylist
True
>>> mylist.remove(2)
>>> a in mylist
False
并适用于您的情况:
@bot.command(name='get')
async def get(ctx):
await ctx.send('Enter your code')
msg = await bot.wait_for('message')
# converting to lower case - make sure everything in the list is lower case too
if msg.content.lower() in ['code1', 'code2']:
await ctx.send('Here is your treasure!')
else:
await ctx.send('That code is invalid')
我还建议添加一个check
,以便使您知道消息是来自原始作者的,并且在同一频道中,等等:
@bot.command(name='get')
async def get(ctx):
await ctx.send('Enter your code')
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
msg = await bot.wait_for('message', check=check)
if msg.content.lower() in ['code1', 'code2']:
await ctx.send('Here is your treasure!')
else:
await ctx.send('That code is invalid')
参考: