How do you create a custom decorator for discord.py?(如何为 discord.py 创建自定义装饰器?)
问题描述
我正在开发一个机器人.对于某个 cog,我希望创建一个自定义检查装饰器来检查运行命令的人是否具有特定角色.角色作为实例变量存储为角色类.当我尝试运行它时,它不起作用.装饰器是怎么做的?
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.")
推荐答案
这个概念被内置到 commands 扩展中作为 检查
This concept is built into the commands extension as Checks
即使是特定于 cog 的检查,例如 cog_check 不知道 cog 本身:它们都不接收 self 作为参数.
Even the cog-specific checks like cog_check aren't aware of the cog itself: none of them receive self as an argument.
您需要重写您的支票,使其不依赖于self.如果您现在知道角色名称或 ID,或者在创建 Moderation 类时,可以使用内置的 has_any_role 检查.
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.
否则最简单的方法可能是使用 Moderation 的类属性或全局值来存储角色:
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, ...):
...
这篇关于如何为 discord.py 创建自定义装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 discord.py 创建自定义装饰器?
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
