Decadescence
Decadescence

Reputation: 17

Toggling command access - Discord.py

im pretty new to python so i thought a good learning project would be to create a discord bot for a personal server, i have a few commands that only i as the owner of the bot can access but i would like to be able to toggle that access with a command so that my friends can also use them but i have a problem, this is the piece of the code giving me an error:

MyID = '<@xxxxxxxxxxxxxxxx>' 
FFA = False

class TestCommands(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
    def IDCheck(ctx):
        return ctx.message.author.id == xxxxxxxxxxxxxxxxxxxxxx

   @commands.command()
    async def ToggleFFA(ctx):
        if FFA == False:
            FFA = True
            print (FFA)
            await message.channel.send('All user can now use owner locked commands')
        if FFA == True:
            FFA = False
            print (FFA)
            await message.channel.send('All user can no longer use owner locked commands')
###########################################################################
    @commands.command()
    if FFA == False:
        @commands.check(IDCheck)
        async def FFATest(self, ctx, *, question):
            loopnumber = 0
            while spamnumber < int(question):
                    await ctx.send('Test' + MyID);
                    await asyncio.sleep(1)
                    loopnumber += 1
                    print ({loopnumber})
    if FFA == True:
        async def LoopTest(self, ctx, *, question):
            loopnumber = 0
            while loopnumber < int(question):
                    await ctx.send('Test' + MyID);
                    await asyncio.sleep(1)
                    loopnumber+= 1
                    print ({loopnumber})
###########################################################################

i get an invalid syntax error within the highlighted piece of code. If anyone knows a simpler way of toggling the access or a way that i can correct the error i would really appreciate it.

thanks in advance.

Upvotes: 1

Views: 1132

Answers (3)

Karan
Karan

Reputation: 498

You can use a LIST for this, Inside that you can store the USER ID and Status ID.
Note: This is a just snippet to give you an idea, The ID Will reset when the script is restarted, I recommend you to save it in a file and load it from there.
You can also use a function to return True/False based on the USER ID instead of writing a bunch of code in each command.

users = []
status = 'Global'

@commands.is_owner()
@commands.command()
async def add_user(self,ctx,user:discord.User):
    global users
    id = user.id
    await ctx.send(f'{user.name} has been added into the mod list.')
    return users.append(id)

@commands.is_owner()
@commands.command()
async def change_status(self,ctx):
    global status
    if status == 'Global':
        status = 'Local'
    elif status == 'Local':
        status = 'Global'
    await ctx.send(f'Status has been changed to {status}')
    return status

@commands.command()
async def test_command(self,ctx):
    global status
    global users
    #IF Status is Local
    if status == 'Local':
        if ctx.user.id in users:
            #ALLOW THE USERS' WHOS' ID IS IN THE LIST
            pass
        else:
            #EXIT THE FUNCTION IF USER NOT IN THE LIST
            return
    #IF The status is Global
    else:
        #ALLOW THE COMMAND IF IT'S GLOBAL
        pass

Upvotes: 1

Patrick Haugh
Patrick Haugh

Reputation: 60974

You can specify a bot_check_once method that will be used as a check on all commands from the bot. We can then have an attribute of the cog that controls which mode we are in:

class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.owner_only = True

    async def bot_check_once(self, ctx):
        app = await self.bot.application_info()
        if self.owner_only:
            return ctx.author == app.owner
        else:
            return True

    @commands.command()
    async def all_users(self, ctx):
        self.owner_only = False

    @commands.command()
    async def just_owner(self, ctx):
        self.owner_only = True

Upvotes: 1

Redfer
Redfer

Reputation: 162

Well you could add a check method outside. Here is an example.

def FFA_Enabled(ctx):
     global FFA
     if commands.is_owner():
          return True
     else:
          return FFA

@commands.check(FFA_enabled):
async def SomeCommand(self, ctx:Context):
     ctx.send("Message")

This Should work if you don't know what ctx:Context means It derives ctx from the type Context(i use it for autofill) if you wanna you is i suggest you type this:

from discord.ext.commands import Context

Upvotes: 1

Related Questions