繁体   English   中英

Bot 黑名单 Discord.py

[英]Bot Blacklists Discord.py

我试图找出如何使用 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"]

这是我使用的系统。 哦,创建一个名为 muted.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

如果你感到困惑,就问我问题。

问题是您在 json 文件中的 ID 是字符串。 ctx.message.author.id是一个整数。

为了解决这个问题,您可以将您的 ID 转换为整数: [713780345035817022, 701792352301350973]在 json 文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM