0

im fairly new to python and have been writing a discord bot for a server with me and a few friends as practice. I've added a few commands that could be pretty disruptive if all users had access to them at all times and have been trying to add a method to enable and disable access to them without using discord roles but a python list instead. I added a command that i can use to to add their user id to a list, but when the user tries to use the command they receive a permission error.

the code below is the problematic code from a cog

modlist = []

    def ListCheck():
        async def IsInList(ctx):
            member = ctx.message.author.id
            return member in modlist
        return commands.check(IsInList)

    @commands.command()
    async def AddUser(self, ctx, *, question):
        modlist.append(question)

    @commands.command()
    async def ViewModList(self, ctx):
        await ctx.send('User ids that have access:')
        for i in range(0, len(modlist)):
            await ctx.send(modlist[i])

    @commands.command()
    async def ClearModList(self, ctx):
        modlist.clear()
        await ctx.send('Modlist cleared')

    @commands.command()
    @ListCheck()
    async def PermTest(self, ctx):
        await ctx.send('user is in modlist')

Any help would be hugely appreciated

1 Answers1

3

One way you could do it would be to use commands.check(). The function in the code below checks if the message author, ctx.author, is in the given list, modList. If the author is in this list when the command 'test' is invoked, then the bot will send "You are a mod!", otherwise it will raise a check error.

modList = [394506589350002688, 697725862128386048] # just some ids as examples

def check_Mod(ctx):
    if ctx.author.id in modList:
        return ctx.author.id in modList

@commands.command()
@commands.check(check_Mod)
async def test(ctx):
    await ctx.send("You are a mod!")

The above working is shown in the image below:

Working mod check command


To answer is there is a way to add user ids to the list via a command?, you may either use a json or text file. In this case, I will use a text file since it's simpler, and also because my json isn't as fluent as others.

Here's an example of how your text file may look like:

394506589350002688
697725862128386048

First you need to read the file and check whether the author's id is in the file. We will be changing the check_Mod function for this. If you want to know more about reading through a text file, or feel that you may need a more efficient way, you may have a look at this: How to search for a string in text files?

def check_Mod(ctx):
    with open('Mod.txt') as f: # do change the 'Mod.txt' to the name that suits you. Ensure that this file is in the same directory as your code!
        if str(ctx.author.id) in f.read(): # this is reading the text file and checking if there's a matching string
            return ctx.author.id        

Now for writing the author id into your text file.

@commands.command()
@commands.check(check_Mod)
async def add_Mod(ctx, user:discord.Member=None):
    if user == None:
        await ctx.send("Please provide a user to add as a Mod!")
        return

    # First we'll make some functions for cleaner, more readable code #

    def is_Mod(user_id): 
    ## This function will check if the given id is already in the file. True if in file, False if not ##
        with open('Mod.txt', 'r') as f:
            if str(user_id) in f.read():
                return True
            else:
                return False

    def add_Mod(user_id):
    ## This function will add the given user id into the given text file, Mod.txt ##
        with open('Mod.txt', 'a') as f: # 'a' is used for appending, since we don't want to overwrite all the ids already in the file
            f.write(f"{str(user_id)}\n")
            f.close()

    # Now we put those functions to use #
    if is_Mod(user.id) == True:
        await ctx.send(f"The user {user} is already a Mod!")
    else:
        add_Mod(user.id)
        await ctx.send(f"{user} added as a Mod!")

The above should work as seen in the image below.

Working add_mod command

p.s. If you have any other questions, please post them as a new question, thank you!

Bagle
  • 2,326
  • 3
  • 12
  • 36
  • Your solution above works perfectly so long as the user id is added to the list before the bot is deployed but do you know if there is a way to add user ids to the list via a command? I still get the same error message after adding a user id using my command in the above code – Decadescence Mar 26 '21 at 14:05
  • You could use a json or text file, which would write the user into the file and save the id, since adding to a list will only store the data until the bot is restarted. Then you can parse through it to check if the author's id is in the text file. I can write how to do this in my answer. – Bagle Mar 27 '21 at 00:55
  • Edited my question to include the add_Mod command as well as checking a text file if the author is a Mod or not. If you have any other questions, please create a new question, thanks! – Bagle Mar 27 '21 at 03:14
  • awesome ill give it a go, thank you so much for your help – Decadescence Mar 27 '21 at 13:04