<small id='jBF07'></small><noframes id='jBF07'>

  • <legend id='jBF07'><style id='jBF07'><dir id='jBF07'><q id='jBF07'></q></dir></style></legend>

      <tfoot id='jBF07'></tfoot>

        • <bdo id='jBF07'></bdo><ul id='jBF07'></ul>
      1. <i id='jBF07'><tr id='jBF07'><dt id='jBF07'><q id='jBF07'><span id='jBF07'><b id='jBF07'><form id='jBF07'><ins id='jBF07'></ins><ul id='jBF07'></ul><sub id='jBF07'></sub></form><legend id='jBF07'></legend><bdo id='jBF07'><pre id='jBF07'><center id='jBF07'></center></pre></bdo></b><th id='jBF07'></th></span></q></dt></tr></i><div id='jBF07'><tfoot id='jBF07'></tfoot><dl id='jBF07'><fieldset id='jBF07'></fieldset></dl></div>

        如何为 discord.py 创建自定义装饰器?

        时间:2023-10-11

        <small id='xpTtr'></small><noframes id='xpTtr'>

          <tbody id='xpTtr'></tbody>

        <tfoot id='xpTtr'></tfoot>
        <legend id='xpTtr'><style id='xpTtr'><dir id='xpTtr'><q id='xpTtr'></q></dir></style></legend>
              <bdo id='xpTtr'></bdo><ul id='xpTtr'></ul>
              <i id='xpTtr'><tr id='xpTtr'><dt id='xpTtr'><q id='xpTtr'><span id='xpTtr'><b id='xpTtr'><form id='xpTtr'><ins id='xpTtr'></ins><ul id='xpTtr'></ul><sub id='xpTtr'></sub></form><legend id='xpTtr'></legend><bdo id='xpTtr'><pre id='xpTtr'><center id='xpTtr'></center></pre></bdo></b><th id='xpTtr'></th></span></q></dt></tr></i><div id='xpTtr'><tfoot id='xpTtr'></tfoot><dl id='xpTtr'><fieldset id='xpTtr'></fieldset></dl></div>

                • 本文介绍了如何为 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 创建自定义装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 discord.py 中播放音轨队列 下一篇:如何响应 discord.py 中的错误用户命令

                  相关文章

                  最新文章

                      <small id='yH2CS'></small><noframes id='yH2CS'>

                      <tfoot id='yH2CS'></tfoot>
                      • <bdo id='yH2CS'></bdo><ul id='yH2CS'></ul>
                      <i id='yH2CS'><tr id='yH2CS'><dt id='yH2CS'><q id='yH2CS'><span id='yH2CS'><b id='yH2CS'><form id='yH2CS'><ins id='yH2CS'></ins><ul id='yH2CS'></ul><sub id='yH2CS'></sub></form><legend id='yH2CS'></legend><bdo id='yH2CS'><pre id='yH2CS'><center id='yH2CS'></center></pre></bdo></b><th id='yH2CS'></th></span></q></dt></tr></i><div id='yH2CS'><tfoot id='yH2CS'></tfoot><dl id='yH2CS'><fieldset id='yH2CS'></fieldset></dl></div>
                    1. <legend id='yH2CS'><style id='yH2CS'><dir id='yH2CS'><q id='yH2CS'></q></dir></style></legend>