为非希特勒人设计的机器人

2024-09-30 14:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个机器人,用于将人列入白名单和不列入白名单。我使用replit是因为它有简单的数据库-代码将在他们的数据库中运行。所以我开始制作机器人白名单,这很容易。当我开始取消hitellisting时,出现了一个错误。。。 当我是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')

DB看起来像这样:

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

DB是replit的插件,所以。。。别弄糊涂了


Tags: 代码send数据库dbif错误机器人await
1条回答
网友
1楼 · 发布于 2024-09-30 14:29:35

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'}

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

TypeError: 'set' object is not subscriptable

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

相关问题 更多 >