python - 如何检查某人是否在列表中
问题描述
代码:
@commands.command()
async def allow(self, ctx, member: discord.Member=None):
if member != None:
if ctx.author.id in f'{allowedID}':
with open('owner.json', 'a') as f:
f.write(member.id)
else:
firstArg = (" ").join(content)
user = bot.get_user(firstArg)
if ctx.author.id in f'{allowedID}':
if user != None:
if user != user.Bot:
with open('owner.json', 'a') as f:
f.write(user.id)
错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'in <string>' requires string as left operand, not int
我想检查是否有人在 json 中,但我总是出现这个错误,但我不知道如何修复它
解决方案
您不能检查字符串中的整数,显然它不存在。在您的代码中更改ctx.author.id in f'{allowedID}'
为。str(ctx.author.id) in f'{allowedID}'
>> 2 in 'name'
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-24283495773c> in <module>
----> 1 2 in 'name'
TypeError: 'in <string>' requires string as left operand, not int
>> str(2) in 'name'
False