我已经搜索了很多,但在这种情况下找不到堆栈解决方案。
我想使用 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
答案 0 :(得分:0)
您可以修改当前代码:
@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
变量更改为要从服务器的所有成员中删除的角色的名称。
答案 1 :(得分:0)
正如您在回溯中看到的,discord.Role
对象是不可迭代的。唯一的可迭代属性 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)