我正在尝试找出如何使用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"]
答案 0 :(得分:0)
这是我使用的系统。哦,创建一个名为Mutted.json的json文件,然后在该文件中键入{}
。确保该文件与bot python文件位于同一目录中。
async def open_muted(user):
users = await get_muted_data()
if str(user.id) in users:
return False
else:
users[str(user.id)] = {}
users[str(user.id)]["mute"] = 0
with open("muted.json","w") as f:
json.dump(users,f)
return True
async def get_mute(user):
await open_muted(user)
users = await get_muted_data()
wallet_amt = users[str(user.id)]['mute']
return wallet_amt
async def get_muted_data():
with open("muted.json") as f:
users = json.load(f)
return users
async def add_mute(user):
await open_muted(user)
users = await get_muted_data()
users[str(user.id)]['mute'] += 1
with open("muted.json","w") as f:
json.dump(users, f)
async def remove_mute(user):
await open_muted(user)
users = await get_muted_data()
users[str(user.id)]['mute'] -= 1
with open("muted.json","w") as f:
json.dump(users, f)
@bot.command()
@commands.has_permissions(kick_members=True)
async def blacklist(ctx,user:discord.Member, *,reason=None):
if await get_mute(user) == 0:
await add_mute(user)
await ctx.send("{} has blacklisted {} for : {}".format(ctx.author.name, user.name, reason))
else:
await ctx.send("The person is already blacklisted.")
@bot.command()
@commands.has_permissions(kick_members=True)
async def unblacklist(ctx,user:discord.Member, *,reason=None):
if await get_mute(user) != 0:
await remove_mute(user)
await ctx.send("{} has unblacklisted {} for : {}".format(ctx.author.name, user.name, reason))
else:
await ctx.send("The person is already unblacklisted.")
@bot.command()
async def test(ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are not allowed to use this command")
else:
#do stuff
如果您感到困惑,请问我一个问题。
答案 1 :(得分:0)
问题是您在json文件中的ID是字符串。 $ cat ~/Android/Sdk/ndk/21.3.6528147/source.properties
Pkg.Desc = Android NDK
Pkg.Revision = 21.3.6528147
是一个整数。
要解决此问题,您可以将ID转换为整数:
ctx.message.author.id
放在您的json文件中。