【问题标题】:Bot Blacklists Discord.py机器人黑名单 Discord.py
【发布时间】: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"]

【问题讨论】:

  • 这是您的代码中使用的确切缩进吗? ifelse 不在同一级别。 await ctx.send 应该比 if 低一级。
  • 哦,抱歉,我想我把问题中的间距弄乱了,但这不是我的代码中的确切缩进。
  • ctx.author.id 是一个 int,您应该将其转换为 str 因为检查它是否在 ids 中:if str(ctx.author.id) in ids

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    如果您感到困惑,请向我提问。

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2021-10-07
      • 2021-03-29
      • 2021-10-20
      • 2021-05-25
      相关资源
      最近更新 更多