<bdo id='nGrzQ'></bdo><ul id='nGrzQ'></ul>

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

    1. <small id='nGrzQ'></small><noframes id='nGrzQ'>

      <legend id='nGrzQ'><style id='nGrzQ'><dir id='nGrzQ'><q id='nGrzQ'></q></dir></style></legend>

      1. 引发自定义错误消息

        时间:2023-09-10
            <bdo id='lW7HH'></bdo><ul id='lW7HH'></ul>

                <tbody id='lW7HH'></tbody>

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

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

                1. <small id='lW7HH'></small><noframes id='lW7HH'>

                  本文介绍了引发自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以当他们使用 ?hello 时,下面的代码会阻止列表中的服务器和用户 ID,所以我试图引发自定义错误消息.如果用户 id 在列表中,它会告诉 User Blacklisted,如果服务器 id 在列表中,它会告诉 Server 已被列入黑名单.

                  So below code blocks server and user id in the list when they use ?hello, so I am trying to raise custom error message. If user id is in list, it will tell User Blacklisted and if server id is in list, it will tell Server has been Blacklisted.

                  LIST_OF_USER_IDS = ['34534545546', '34534545546']
                  LIST_OF_SERVER_IDS = ['34534545546', '34534545546']
                  
                  def blacklists(users, servers):
                      def predicate(ctx):
                          return ctx.message.author.id not in users and ctx.message.server.id not in servers
                      return commands.check(predicate)
                  
                  @bot.command(pass_context=True)
                  @blacklists(LIST_OF_USER_IDS, LIST_OF_SERVER_IDS)
                  async def hello(ctx):
                       await bot.say("Hello {}".format(ctx.message.author.mention))
                  

                  所以我尝试了下面的代码,但出现错误.我只是一个初学者,所以我的代码不正确,所以我需要帮助来解决这个问题.

                  So I tried below code but getting error. I'm just a beginner so my code is not correct, so I need a help to fix this.

                  def blacklists(users, servers):
                      def predicate(ctx):
                          return ctx.message.author.id not in users and ctx.message.server.id not in servers
                      return commands.check(predicate)
                  try:
                         if ctx.message.author.id in LIST_OF_USER_IDS:
                             raise UserBlacklisted
                         elif ctx.message.server.id in LIST_OF_SERVER_IDS:
                             raise ServerBlacklisted
                         break
                     except UserBlacklisted:
                         await bot.send_message(ctx.message.channel, "User Blacklisted")
                     except ServerBlacklisted:
                         await bot.send_message(ctx.message.channel, "Server has been Blacklisted")
                  

                  推荐答案

                  如果检查失败,而不是返回 False,而是引发 CommandError 的子类,然后处理on_command_error 事件中的错误.

                  Instead of returning False if the check fails, instead raise a subclass of CommandError, then handle that error in the on_command_error event.

                  class UserBlacklisted(commands.CommandError):
                      def __init__(self, user, *args, **kwargs):
                          self.user = user
                          super().__init__(*args, **kwargs)
                  
                  class ServerBlacklisted(commands.CommandError):
                      def __init__(self, server, *args, **kwargs):
                          self.server = server
                          super().__init__(*args, **kwargs)
                  
                  
                  def blacklists(users, servers):
                      def predicate(ctx):
                          if ctx.message.author.id in users:
                              raise UserBlacklisted(ctx.message.author)
                          elif ctx.message.server.id in servers:
                              raise ServerBlacklisted(ctx.message.server)
                          else:
                              return True
                      return commands.check(predicate)
                  
                  @bot.event
                  async def on_command_error(error, ctx):
                      if isinstance(error, UserBlacklisted):
                          await bot.send_message(ctx.message.channel, "User {} has been blacklisted".format(error.user.mention))
                      elif isinstance(error, ServerBlacklisted):
                          await bot.send_message(ctx.message.channel, "Server {} has been blacklisted".format(error.server.name))
                  
                  
                  
                  @bot.command(pass_context=True)
                  @blacklists(LIST_OF_USER_IDS, LIST_OF_SERVER_IDS)
                  async def hello(ctx):
                       await bot.say("Hello {}".format(ctx.message.author.mention))
                  

                  这篇关于引发自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 discord.py 中转换时间的 Tempmute 命令 下一篇:Client = discord.client() TypeError: 'module' object

                  相关文章

                  最新文章

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

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

                        <bdo id='dfHET'></bdo><ul id='dfHET'></ul>

                    1. <legend id='dfHET'><style id='dfHET'><dir id='dfHET'><q id='dfHET'></q></dir></style></legend>
                      <tfoot id='dfHET'></tfoot>