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

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

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

      1. Discord.py Bot 的权限系统

        时间:2023-09-09
        • <small id='KWWep'></small><noframes id='KWWep'>

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

                <tbody id='KWWep'></tbody>

                1. <legend id='KWWep'><style id='KWWep'><dir id='KWWep'><q id='KWWep'></q></dir></style></legend>
                  本文介绍了Discord.py Bot 的权限系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 discord.py 和 asyncio 制作一个不和谐机器人.该机器人具有像 kickban 这样的命令,显然普通用户不应该使用这些命令.

                  I am in the process of making a discord bot using discord.py and asyncio. The bot has commands like kick and ban which obviously should not be available to normal users.

                  我想制作一个简单的系统,该系统将使用 ctx.message.author 检测用户角色的权限,以获取发送命令的用户.

                  I want to make a simple system which will detect what permissions the user's role has using ctx.message.author to get the user who sent the command.

                  我不希望机器人检测到特定角色名称,因为这些名称因服务器而异.我也不想让机器人有多个文件以保持简单.

                  I do not want the bot to detect a specific role name as these vary across servers. I also prefer not to have multiple files for the bot to keep it simple.

                  我看过 discord.py 文档和其他各种来源,但没有一个包含如何实现他们谈论的各种方法的示例.

                  I have seen the discord.py documentation and various other sources but none contain examples of how to implement the various methods they talk about.

                  例如,这是我的机器人发出的一个命令:

                  As an example, here is a single command from my bot:

                  async def kick(ctx, userName: discord.User):
                      if True: #ctx.message.author.Permissions.administrator
                          await BSL.kick(userName)
                      else:
                          permission_error = str('Sorry ' + ctx.message.author + ' you do not have permissions to do that!')
                          await BSL.send_message(ctx.message.channel, permission_error)
                  

                  if else 语句是我自己尝试做的.#ctx.message.author.Permissions.administrator 被注释掉,因为它不起作用并替换为 True 用于测试目的.

                  Where the if else statement is my attempt of doing this on my own. The #ctx.message.author.Permissions.administrator is commented out as it does not work and replaced with True for testing purposes.

                  提前感谢您的任何帮助和建议.

                  Thank you for any help and suggestions in advance.

                  推荐答案

                  Permissions 是类的名称.要获得消息作者权限,您应该访问 guild_permissions 作者的属性.

                  Permissions is the name of the class. To get the message authors permissions, you should access the guild_permissions property of the author.

                  if ctx.message.author.guild_permissions.administrator:
                   # you could also use guild_permissions.kick_members
                  

                  更新:

                  验证调用命令的人的权限的更好方法是使用 commands 扩展的 ="nofollow noreferrer">check 功能,特别是 has_permissions 检查.例如,如果您只想向拥有 manage_roles 权限或 ban_members 权限的人打开命令,则可以这样编写命令:

                  A better way to validate the permissions of the person invoking the commands is by using the check feature of the commands extension, specifically the has_permissions check. For example, if you wanted to open your command only to people who had either the manage_roles permission or the ban_members permission, you could write your command like this:

                  from discord import Member
                  from discord.ext.commands import has_permissions, MissingPermissions
                  
                  @bot.command(name="kick", pass_context=True)
                  @has_permissions(manage_roles=True, ban_members=True)
                  async def _kick(ctx, member: Member):
                      await bot.kick(member)
                  
                  @_kick.error
                  async def kick_error(ctx, error):
                      if isinstance(error, MissingPermissions):
                          text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
                          await bot.send_message(ctx.message.channel, text)
                  

                  这篇关于Discord.py Bot 的权限系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:每个服务器前缀 下一篇:使用 discord.py 获取频道名称

                  相关文章

                  最新文章

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

                      <small id='6ZEHB'></small><noframes id='6ZEHB'>