【问题标题】:discord.py how to make a command has permissonsdiscord.py 如何使命令具有权限
【发布时间】:2020-12-15 15:28:53
【问题描述】:

我正在制作一个不和谐机器人,并制作了一段代码,可以让语音频道中的每个人都静音。我想确保只有 Mod 或具有管理员权限的人才能使用此命令。

这是我的静音命令代码:

@client.command()
async def vcmute(ctx):
    vc = ctx.author.voice.channel
    for member in vc.members:
        await member.edit(mute=True)
    await ctx.send('Mics are closed!')

这是我的取消静音命令(这将使用仅管理员或 Mod 使用它的相同概念):

@client.command()
async def vcunmute(ctx):
    vc = ctx.author.voice.channel
    for member in vc.members:
        await member.edit(mute=False)
    await ctx.send('Mics are opened!')

【问题讨论】:

    标签: python discord.py discord.py-rewrite


    【解决方案1】:

    要获取发件人当前频道的权限,您可以使用
    perms = ctx.author.permissions_in(ctx.author.voice.channel)。然后您可以检查该用户是否具有静音权限

    if perms.mute_members:
        #whatever you want your command to do
    else:
        #no permission error msg
    

    【讨论】:

      【解决方案2】:

      使用 has_permissions 装饰器

      @client.command()
      @commands.has_permissions(administrator=True) # Or whatever perms you want
      async def vcmute(ctx):
          ...
      
      
      # If you wanna send some message if the user doesn't have the needed permissions
      @vcmute.error
      async def vcmute_error(ctx, error):
          if isinstance(error, commands.MissingPermissions):
              await ctx.send(f"You need {error.missing_perms} permissions to use this command.")
      

      【讨论】:

      • 谢谢,但这是针对特定角色还是什么?好吧,如果是这样,那么我不想要那样的对不起。
      • 它叫has_permissions 为什么是角色?对于角色是commands.has_role。
      • 我想我误解了,抱歉,但这不是重点,重点是当我按照你说的做时,我总是会收到此错误:@commands.has_permissions(admin=True) File "C:\Users\muham\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 1758, in has_permissions raise TypeError('Invalid permission(s): %s' % (', '.join(invalid))) TypeError: Invalid permission(s): admin
      • 因为它是“管理员”而不是“管理员”
      • 顺便说一句,我也在尝试添加mute _members,但是当我添加该代码时,它只是验证管理员而不是静音成员,关键是,我有静音成员烫发,但我仍然不能静音任何人这个vcmute 命令
      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 2020-11-24
      • 2021-04-16
      • 2011-06-18
      • 2021-05-02
      • 2020-12-22
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多