Permission System for Discord.py Bot

后端 未结 3 1984
挽巷
挽巷 2021-01-04 21:50

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 avai

3条回答
  •  清酒与你
    2021-01-04 22:20

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

    if ctx.message.author.server_permissions.administrator:
     # you could also use server_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(error, ctx):
        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)
    

提交回复
热议问题