discord.py静音角色权限添加

discord.py mute role permission adding

所以我为 discord 机器人设置了静音命令。它有效,但没有向 'Muted' 角色添加任何权限?任何想法出了什么问题? 代码:

@client.command(name = 'mute')
@commands.has_permissions(manage_permissions = True)
async def mute(ctx, member: discord.Member, *, reason = None):
    muted = discord.utils.get(ctx.guild.roles, name = 'muted')
    perms = discord.Permissions(send_messages = False)
    if muted not in ctx.guild.roles:
        muted = await ctx.guild.create_role(name = 'muted', permissions = perms)
    memroles = discord.utils.get(member.roles)
    if muted in memroles:
        await ctx.send(f':x: This person is already muted')
    await member.add_roles(muted)
    await ctx.send(f':white_check_mark: **User <@{member.id}> was muted!**')
    print(f'User {member} was muted!')

错误:

Traceback (most recent call last):
  File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "D:\endport_semegen\discordpy\smth.py", line 62, in mute
    if muted in memroles:
TypeError: argument of type 'Role' is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'Role' is not iterable```

问题出在您 discord.utils.get 成员角色上。

From the docs,强调我的:

A helper that returns the first element in the iterable that meets all the traits passed in attrs. This is an alternative for find().

由于您没有提供任何内容供其检查,它将 return 第一个 满足要求的角色 - 成员拥有的第一个角色。显然,这不是您想要的角色列表。

解决方案是简单地删除 get。没有理由这样做。现在,member.roles 将成为您可以检查的列表。

    memroles = member.roles
    if muted in memroles:
        await ctx.send(f':x: This person is already muted')