discord.py 试图从用户中删除所有角色

时间:2020-12-22 22:04:11

标签: python discord.py

我有一个问题,我正在尝试删除用户对某种静音角色的所有角色,但它给了我这个错误 _df = (df.reset_index() .merge(df, left_on=['A', 'B'], right_on=['A', 'C'], how='inner', suffixes=['_ori', '_target'])) _df.loc[_df.E_ori.isna(), 'E_ori'] = _df.loc[_df.E_ori.isna(), 'D_target'] _df.set_index('index', inplace=True) df.loc[_df.index, 'E'] = _df['E_ori']

这是我的代码

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role

我知道这里已经有人问过这种问题了:How to remove all roles at once (Discord.py 1.4.1)。 但它没有得到回答,根本没有帮助我

1 个答案:

答案 0 :(得分:3)

问题是所有用户都有一个“隐形角色”,@everyone。如果你尝试,你会看到它出现

for i in member.roles:
    print(i)

remove_roles 是一个高级函数,它会尝试删除导致错误的 @everyone

要清除用户的所有当前角色,您可以执行以下操作:

@client.command(aliases=['m'])
@commands.has_permissions(kick_members = True)
async def mute(ctx, member : discord.Member):
    muteRole = ctx.guild.get_role(775449115022589982)
    await member.edit(roles=[muteRole]) # Replaces all current roles with roles in list
    await ctx.channel.purge(limit = 1)
    await ctx.send(str(member)+' has been muted!')

await member.edit(roles=[]) 用您在列表中拥有的角色替换所有当前角色。将列表留空以删除用户的所有角色。

discord.Member.edit

虽然如果你想用 for loop 来做,你可以用 try

@client.command(aliases=['m'])
@commands.has_permissions(kick_members = True)
async def mute(ctx, member : discord.Member):
    muteRole = ctx.guild.get_role(775449115022589982)
    for i in member.roles:
        try:
            await member.remove_roles(i)
        except:
            print(f"Can't remove the role {i}")
    await member.add_roles(muteRole)
    await ctx.channel.purge(limit = 1)
    await ctx.send(str(member)+' has been muted!')