试图删除用户的所有角色

试图删除用户的所有角色

本文介绍了discord.py 试图删除用户的所有角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个问题,我试图删除用户对某种静音角色的所有角色,但它给了我这个错误 discord.ext.commands.errors.CommandInvokeError: Command raise an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role

I have a problem that I`m trying to remove all roles a user has for some kind of mute role but it gives me this error discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role

这是我的代码

@client.command(aliases=['m'])
@commands.has_permissions(kick_members = True)
async def mute(ctx,member : discord.Member):
    muteRole = ctx.guild.get_role(728203394673672333)
    for i in member.roles:
        await member.remove_roles(i)
    await member.add_roles(muteRole)
    await ctx.channel.purge(limit = 1)
    await ctx.send(str(member)+' has been muted!')

我知道这种问题已经在这里问过了:如何一次删除所有角色 (Discord.py 1.4.1).但它没有得到回答,根本没有帮助我

I know that this kind of questiion was alredy asked here: How to remove all roles at once (Discord.py 1.4.1).But it wasn`t answered and did not help me at all

推荐答案

问题是所有用户都有一个隐形角色",@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!')

这篇关于discord.py 试图删除用户的所有角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:20