How do you create a custom decorator for discord.py?

 ̄綄美尐妖づ 提交于 2021-01-25 06:54:53

问题


I am working on a bot. For a certain cog, I wish to create a custom check decorator that checks to see if the person running the command has a certain role. The role is stored as a role class as an instance variable. When I tried running it, it doesn't work. How do you make the decorator?

class Moderation(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot
        self.mod_role = None  # Assume there's already a role here

    class Decorator:
        @classmethod
        def requires_mod(cls, func):
            async def decorator(self, ctx: commands.Context, *args, **kwargs):
                if self.mod_role not in ctx.author.roles:
                    await ctx.send("You do not have permission to use this command")
                func(ctx, *args, **kwargs)
            return decorator

    @commands.command()
    @Decorator.requires_mod
    async def purge(self, ctx: commands.Context, amt: int):
        await ctx.channel.purge(limit=amt+1)
        await ctx.send(f":white_check_mark: | Deleted {amt} messages.")

回答1:


This concept is built into the commands extension as Checks

Even the cog-specific checks like cog_check aren't aware of the cog itself: none of them receive self as an argument.

You need to rewrite your check such that it doesn't rely on self. If you know the role names or ids now, or when creating the Moderation class, you can use the built-in has_any_role check.

Otherwise the easiest way is probably to use either a class attribute of Moderation or a global value to store the role:

from discord.ext import commands

def predicate(ctx):
    return Moderation.mod_role in ctx.author.roles

has_mod_role = commands.check(predicate)


class Moderation(commands.Cog):
    mod_role = None 

    def __init__(bot):
        self.bot = bot
        Moderation.mod_role = ... 

    @commands.command()
    @has_mod_role
    async def yourcommand(ctx, ...):
        ...


来源:https://stackoverflow.com/questions/56637056/how-do-you-create-a-custom-decorator-for-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!