問題描述
我有一個(gè)問題,我試圖刪除用戶對(duì)某種靜音角色的所有角色,但它給了我這個(gè)錯(cuò)誤 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!')
我知道這種問題已經(jīng)在這里問過了:如何一次刪除所有角色 (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
推薦答案
問題是所有用戶都有一個(gè)隱形角色",@everyone
.如果你嘗試,你會(huì)看到它出現(xiàn)
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
是一個(gè)高級(jí)函數(shù),它會(huì)嘗試刪除導(dǎo)致您的錯(cuò)誤的 @everyone
.
remove_roles
is a high level function and it will try to remove @everyone
, which is causing your error.
要清除用戶的所有當(dāng)前角色,您可以:
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=[])
將所有當(dāng)前角色替換為您在列表中擁有的角色.將列表留空以刪除用戶的所有角色.
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 循環(huán)
來做,你可以使用 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!')
這篇關(guān)于discord.py 試圖刪除用戶的所有角色的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!