且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-30 12:07:34

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

The problem is that all users have an "invisible role", @everyone. You will see it show up if you try

for i in member.roles:
    print(i)

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

remove_roles is a high level function and it will try to remove @everyone, which is causing your error.

要清除用户的所有当前角色,您可以:

To clear all current roles from the user, you can do:

@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=[]) 将所有当前角色替换为您在列表中拥有的角色.将列表留空以删除用户的所有角色.

await member.edit(roles=[]) Replaces all the current roles with the roles you have in the list. Leave the list empty to remove all roles from the user.

discord.Member.edit

虽然如果你想用 for 循环 来做,你可以使用 try

Although if you want to do it with a for loop, you can use 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!')