Yuvanesh Anand
Yuvanesh Anand

Reputation: 81

Why cant I restrict command use for my bot - discord.py

'''
@client.event
@commands.has_role('xrole')
async def on_message(message):
#Commands

  if message.content.startswith("!set"):
    credits_message = message.content.split(" ", 2)

    user_name = credits_message[1]
    credit_add_amount = credits_message[2]

    db[user_name] = int(credit_add_amount)   

    #DB Stuff
    await message.channel.send(user_name + "has been set with " + credit_add_amount + ", For further credit adgustment use !add")

 
'''

For some reason Users with or without the xrole can acces the bot even with the @commands restriction

user without xrole

User without xrole^

user with xrole

user with xrole ^

Upvotes: 0

Views: 114

Answers (2)

omar
omar

Reputation: 77

firstly don't use on_message for commands, it's much better to use @bot.command

second: this code should work well for you

@bot.command(pass_context=True)
@commands.has_role("xrole")
async def role_check(ctx):
    await ctx.channel.send('''you have the role "xrole"''')


@role_check.error
async def info_error(ctx, error):
    if isinstance(error, (commands.MissingRole, commands.MissingAnyRole)):
        await ctx.channel.send('''you do not have the role "xrole"''')

Upvotes: 2

stijndcl
stijndcl

Reputation: 5650

Because Checks are for Commands, not for Events, so the has_role decorator will never be used. You should be using commands instead of manually parsing everything in on_message anyways, it makes a lot of stuff way less cumbersome.

Here's an explanation from the docs how to make proper commands instead: https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html

Upvotes: 1

Related Questions