Search code examples
pythondiscord.pyreplit

bot for unwhitelisting people


I'm trying to make a bot for whitelisting and unwhitelisting people. I've use replit because it has easy DB - code will run in their DB. So I started making bot whitelist, which was easy. When I started unwhitelisting there was an error ... when I was V.I.P bot it said that I wasn't. Please help me... code :

@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 looks like this:

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

DB is plugin of replit so... don't get confused


Solution

  • TLDR

    Replace

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

    with

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

    Explanation

    Since you have

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

    The variable db is a set, which is not subscriptable. So when your code runs db[str(user)] (as part of the if condition), you get the error

    TypeError: 'set' object is not subscriptable
    

    This error is caught in your except block, and so your program sends the message saying you are not VIP.