Demotry
Demotry

Reputation: 909

Restrict Commands

I got this code it blocks access to ?hello command for everyone except those user id in LIST_OF_ID so i modified this by replacing ctx.message.author.id to ctx.message.server.id now it bypasses for server also. so i added both code but now its not working for both user and server, only works for anyone. how to make it works for both server and user.

LIST_OF_SERVER_IDS = ['3557657657647676', '36567565756766767']
LIST_OF_USER_IDS = ['3557657657647676', '36567565756766767'] 

def is_any_user(ids):
    def predicate(ctx):
        return ctx.message.author.id in ids
    return commands.check(predicate)

def is_any_server(ids):
    def predicate(ctx):
        return ctx.message.server.id in ids
    return commands.check(predicate)

@bot.command(pass_context=True)
@is_any_user(LIST_OF_USER_IDS)
@is_any_server(LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

Upvotes: 1

Views: 183

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60974

By defining two is_any_user decorators, you just keep the most recently defined, and lose the first one. Give the server check a more representative name (after all, it's not checking Users, why have "user" in the name). Then we can keep two whitelists of ids. Since commands.check can chain, we simply decorate our command with both checks.

LIST_OF_SERVER_IDS = [3557657657647676, 36567565756766767, 343657687786435432]
LIST_OF_USER_IDS = [1, 2, 3]  
# Replace as needed.  Keep in mind that 0.16 ids are strings and 1.0 ids are integers


def is_any_user(ids):
    def predicate(ctx):
        return ctx.message.author.id in ids
    return commands.check(predicate)

def is_any_server(ids):
    def predicate(ctx):
        return ctx.message.server.id in ids
    return commands.check(predicate)

@bot.command(pass_context=True)
@is_any_user(LIST_OF_USER_IDS)
@is_any_server(LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

Edit:

Use these version of the checks to try and debug what's going on

def is_any_user(ids):
    def predicate(ctx):
        if ctx.message.author.id in ids:
            print("Good author")
            return True
        else:
            print("Bad author")
            return False
    return commands.check(predicate)

def is_any_server(ids):
    def predicate(ctx):
        if ctx.message.server.id in ids:
            print("Good server")
            return True
        else:
            print("Bad server")
            return False
    return commands.check(predicate)

Edit2:

You can write a check that looks to see if a person is in the user whitelist or accessing the command from a server on the server whitelist.

def whitelists(users, servers):
    def predicate(ctx):
        return ctx.message.author.id in users or ctx.message.server.id in servers
    return commands.check(predicate)

@bot.command(pass_context=True)
@whitelists(LIST_OF_USER_IDS, LIST_OF_SERVER_IDS)
async def hello(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

Upvotes: 1

Related Questions