繁体   English   中英

Discord.py 从 1 个特定角色中删除所有用户

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM