Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission System for Discord.py Bot

I am in the process of making a discord bot using discord.py and asyncio. The bot has commands like kick and ban which obviously should not be available to normal users.

I want to make a simple system which will detect what permissions the user's role has using ctx.message.author to get the user who sent the command.

I do not want the bot to detect a specific role name as these vary across servers. I also prefer not to have multiple files for the bot to keep it simple.

I have seen the discord.py documentation and various other sources but none contain examples of how to implement the various methods they talk about.

As an example, here is a single command from my bot:

async def kick(ctx, userName: discord.User):
    if True: #ctx.message.author.Permissions.administrator
        await BSL.kick(userName)
    else:
        permission_error = str('Sorry ' + ctx.message.author + ' you do not have permissions to do that!')
        await BSL.send_message(ctx.message.channel, permission_error)

Where the if else statement is my attempt of doing this on my own. The #ctx.message.author.Permissions.administrator is commented out as it does not work and replaced with True for testing purposes.

Thank you for any help and suggestions in advance.

like image 327
user9123 Avatar asked Feb 04 '18 20:02

user9123


People also ask

How do I set Discord bot permissions?

Adjust the Bot's Role on DiscordClick the name of your server in the top-left and click Server Settings > Roles. Within the Roles column, you should see the bot's new role. Left click and drag the role above all others. This ensures that the bot can successfully manage other members on the server.

What permissions does a Discord bot need?

Only people who have Administrative or “Manage Server” permissions on the server can invite a bot. If you don't have either of these roles, you won't be able to add bots.


2 Answers

Permissions is the name of the class. To get the message authors permissions, you should access the guild_permissions property of the author.

if ctx.message.author.guild_permissions.administrator:
 # you could also use guild_permissions.kick_members

Update:

A better way to validate the permissions of the person invoking the commands is by using the check feature of the commands extension, specifically the has_permissions check. For example, if you wanted to open your command only to people who had either the manage_roles permission or the ban_members permission, you could write your command like this:

from discord import Member
from discord.ext.commands import has_permissions, MissingPermissions

@bot.command(name="kick", pass_context=True)
@has_permissions(manage_roles=True, ban_members=True)
async def _kick(ctx, member: Member):
    await bot.kick(member)

@_kick.error
async def kick_error(ctx, error):
    if isinstance(error, MissingPermissions):
        text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
        await bot.send_message(ctx.message.channel, text)
like image 181
Patrick Haugh Avatar answered Sep 19 '22 13:09

Patrick Haugh


You could also use decorators.

@bot.command(name = "Kick")
@bot.has_permissions(kick_user = True)
@bot.bot_has_permissions(kick_user = True)
async def _kick(ctx, member: Member):
    #Do stuff...

The advantage of checking user and bot permissions means it is easier to handle errors from either providing useful "Insufficient Permission" error messages.

like image 24
RushTheFox Avatar answered Sep 18 '22 13:09

RushTheFox