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

  • <tfoot id='UybME'></tfoot>

        <i id='UybME'><tr id='UybME'><dt id='UybME'><q id='UybME'><span id='UybME'><b id='UybME'><form id='UybME'><ins id='UybME'></ins><ul id='UybME'></ul><sub id='UybME'></sub></form><legend id='UybME'></legend><bdo id='UybME'><pre id='UybME'><center id='UybME'></center></pre></bdo></b><th id='UybME'></th></span></q></dt></tr></i><div id='UybME'><tfoot id='UybME'></tfoot><dl id='UybME'><fieldset id='UybME'></fieldset></dl></div>
        • <bdo id='UybME'></bdo><ul id='UybME'></ul>
        <legend id='UybME'><style id='UybME'><dir id='UybME'><q id='UybME'></q></dir></style></legend>
      1. Discord.py - 使用命令更改前缀

        时间:2023-10-11

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

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

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

                • <tfoot id='KS1NI'></tfoot>
                    <tbody id='KS1NI'></tbody>
                • 本文介绍了Discord.py - 使用命令更改前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想创建一个命令,管理员可以在其中更改命令的前缀(例如:而不是使用."他们可以将其更改为-",并且只有-"才会起作用)我会能够设置权限以使只有管理员能够使用该命令

                  I want to make a command where an admin can change the prefix for commands (eg: instead of using "." they can change it to "-" and only "-" will work if they do) I'd be able to setup the permissions to make only admins able to use the command

                  我环顾四周,通过 docs &互联网,但没有找到任何东西,我不知道如何做到这一点

                  I've looked around, through the docs & interwebs but haven't found anything and I haven't had any idea on how to do this

                  推荐答案

                  您应该使用 command_prefix 参数/api.html#bot" rel="noreferrer">discord.Bot 这接受字符串 (表示一个机器人范围的前缀) 或可调用的 (表示根据条件返回前缀的函数).

                  You should use the command_prefix argument for discord.Bot this accepts either a string (meaning one bot wide prefix) or a callable (meaning a function that returns a prefix based on a condition).

                  您的条件依赖于调用 message.因此,您可以允许公会定义自己的前缀.我将使用一个 dict 作为一个简单的例子:

                  Your condition relies on the invoking message. Therefore you can allow guilds to define their own prefixes. I'll use a dict as a simple example:

                  ...
                  
                  custom_prefixes = {}
                  #You'd need to have some sort of persistance here,
                  #possibly using the json module to save and load
                  #or a database
                  default_prefixes = ['.']
                  
                  async def determine_prefix(bot, message):
                      guild = message.guild
                      #Only allow custom prefixs in guild
                      if guild:
                          return custom_prefixes.get(guild.id, default_prefixes)
                      else:
                          return default_prefixes
                  
                  bot = commands.Bot(command_prefix = determine_prefix, ...)
                  bot.run()
                  
                  @commands.command()
                  @commands.guild_only()
                  async def setprefix(self, ctx, *, prefixes=""):
                      #You'd obviously need to do some error checking here
                      #All I'm doing here is if prefixes is not passed then
                      #set it to default 
                      custom_prefixes[ctx.guild.id] = prefixes.split() or default_prefixes
                      await ctx.send("Prefixes set!")
                  

                  关于这个使用的一个很好的例子,请参考 Rapptz (discord.py 的创建者) 自己的 RoboDanny 机器人,他将其作为教育目的的公共回购例子.具体见prefix_callable函数,这是我的 determine_prefix 示例的更强大的版本.

                  For a great example of this in use, refer to Rapptz (the creator of discord.py)'s very own RoboDanny bot, he made it a public repo for educational purposes as an example. In specific, see prefix_callable function, it's a more robust version of my determine_prefix example.

                  这篇关于Discord.py - 使用命令更改前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为多个机器人加载 cog 下一篇:使用 tweepy 和 discord.py 将推文发布到特定频道

                  相关文章

                  最新文章

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

                • <small id='gLkrD'></small><noframes id='gLkrD'>

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

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