我如何检查列表中的响应
How do i check the response from a list
我正在创建一个带有输入代码的机器人。但是如何检查消息内容是否包含列表中的代码之一?
这是我现在拥有的:
@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')
您可以使用 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')
参考文献:
我正在创建一个带有输入代码的机器人。但是如何检查消息内容是否包含列表中的代码之一?
这是我现在拥有的:
@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')
您可以使用 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')
参考文献: