【发布时间】:2021-02-21 22:27:52
【问题描述】:
我正在尝试找出如何使用 json 文件将人员列入黑名单并检查用户 ID 是否在文件中。到目前为止,这是我的代码:
import json
with open("ids.json", "r") as f:
ids = json.load(f)
@client.command()
async def test(ctx):
if ctx.message.author.id in ids:
await ctx.send('Unfortunately, you have been blacklisted from the bot. If you wish to know why or appeal, join this server: ')
else:
#do stuff here
但它不起作用。没有错误,但它仍然允许我使用命令。我该如何解决这个问题,我当前的代码有什么问题?
在我的 json 文件中:
["713780345035817022", "701792352301350973"]
【问题讨论】:
-
这是您的代码中使用的确切缩进吗?
if和else不在同一级别。await ctx.send应该比if低一级。 -
哦,抱歉,我想我把问题中的间距弄乱了,但这不是我的代码中的确切缩进。
-
ctx.author.id是一个 int,您应该将其转换为 str 因为检查它是否在 ids 中:if str(ctx.author.id) in ids。
标签: python discord discord.py discord.py-rewrite