[英]Discord.py Remove all users from 1 specific role
我已經搜索了很多,但在這個案例的堆棧上找不到解決方案。
我想使用 1 個命令從角色中刪除所有用戶。
@client.command()
async def swipe(ctx, role: discord.Role):
if ctx.author.guild_permissions.administrator:
roles = tuple(get(ctx.guild.roles, name=n) for n in role)
for m in ctx.guild.members:
try:
await member.remove_roles(*roles)
except:
print(f"Couldn't remove roles from {m}")
await ctx.send(f'Removed **all** experimental roles.')
我收到以下錯誤:
File "C:\Users\Admin\Desktop\epic guard\bot.py", line 37, in swipe
roles = tuple(get(ctx.guild.roles, name=n) for n in role)
TypeError: 'Role' object is not iterable
您可以修改當前代碼:
@client.command()
async def swipe(ctx, role: discord.Role):
if ctx.author.guild_permissions.administrator:
roles = tuple(get(ctx.guild.roles, name=n) for n in role)
for m in ctx.guild.members:
try:
await member.remove_roles(*roles)
except:
print(f"Couldn't remove roles from {m}")
await ctx.send(f'Removed **all** experimental roles.')
對此:
import discord
@client.command()
async def swipe(ctx):
if ctx.author.guild_permissions.administrator:
for user in ctx.guild.members:
role_name = 'Member' # Or enter the name of the role you want to remove
member = discord.utils.get(ctx.guild.roles, name=role_name)
await user.remove_roles(member)
您可能應該將role_name
變量更改為您希望從服務器的所有成員中刪除的角色的名稱。
正如您在回溯中看到的, discord.Role
object 是不可迭代的。 Discord.Role
的唯一可迭代屬性是discord.Role.members
,我認為這就是您要尋找的。 您可以遍歷所有角色用戶,然后可以從所有角色中刪除角色。
此外,您可以使用commands.has_permissions()
來檢查用戶是否具有必要的權限。
@commands.has_permissions(administrator=True)
@client.command()
async def swipe(ctx, role: discord.Role):
for member in role.members:
await member.remove_roles(role)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.