【问题标题】:bot for unwhitelisting people用于将人列入白名单的机器人
【发布时间】:2021-11-24 23:24:20
【问题描述】:

我正在尝试制作一个用于将人列入白名单和取消白名单的机器人。我使用 repli 是因为它有简单的数据库 - 代码将在他们的数据库中运行。所以我开始制作机器人白名单,这很容易。当我开始取消白名单时出现错误... 当我是 V.I.P 机器人时,它说我不是。请帮我... 代码:

@client.command()
async def unwhitelist(ctx, user):
    role = ctx.guild.get_role(893143464781422612)
    if role in ctx.author.roles:
        try:
            if len(db[str(user)]) != -1:
                del db[str(user)]
                await ctx.send(f'Succesfult deleted {str(user)} from V.I.P')
        except:
            await ctx.send(f'{str(user)} is not V.I.P')
    else:
        await ctx.send('U dont have Permission')

数据库看起来像这样:

{'user', 'user1', 'user2'}

DB 是 replit 的插件所以...不要混淆

【问题讨论】:

    标签: python discord.py repl.it


    【解决方案1】:

    TLDR

    替换

    try:
        if len(db[str(user)]) != -1:
            del db[str(user)]
            await ctx.send(f'Succesfult deleted {str(user)} from V.I.P')
    except:
        await ctx.send(f'{str(user)} is not V.I.P')
    

    if str(user) in db:
        db.remove(str(user))
        await ctx.send(f'Succesfult deleted {str(user)} from V.I.P')
    else:
        await ctx.send(f'{str(user)} is not V.I.P')
    

    说明

    因为你有

    db = {'user', 'user1', 'user2'}
    

    变量dbset,不可下标。因此,当您的代码运行db[str(user)](作为if 条件的一部分)时,您会收到错误

    TypeError: 'set' object is not subscriptable
    

    此错误在您的except 块中被捕获,因此您的程序会发送消息说您不是 VIP。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 2019-01-16
      • 2021-06-30
      • 1970-01-01
      • 2021-02-21
      • 2020-10-26
      • 1970-01-01
      相关资源
      最近更新 更多