【问题标题】:Discord.py Remove all users from 1 specific roleDiscord.py 从 1 个特定角色中删除所有用户
【发布时间】:2021-02-18 05:55:41
【问题描述】:

我已经搜索了很多,但在堆栈上找不到此案例的解决方案。

我想使用 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

【问题讨论】:

    标签: python discord discord.py


    【解决方案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.')
    

    到这里:

    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 变量更改为您希望从服务器的所有成员中删除的角色的名称。

    【讨论】:

      【解决方案2】:

      正如您在回溯中看到的,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)
      

      参考文献

      【讨论】:

      • @corsax1993 你刚刚泄露了你的不和谐机器人令牌。你应该改变它。
      • @corsax1993 所以你说你当前的代码是: ``` from discord.ext import commands from discord.utils import get import discord client = commands.Bot(command_prefix='.', help_command=None) @client.command() async def swipeall(ctx, role: discord.Role): if ctx.author.guild_permissions.administrator: for member in role.members: await member.remove_roles(role) ``` 做你的服务器有管理员权限吗?如果你这样做,你会得到任何错误吗?该命令可能不起作用,因为您没有管理员权限。
      • 我的服务器和我的添加和删除命令完美运行异步 def add(ctx, role: discord.Role, user: discord.Member): if ctx.author.guild_permissions.administrator: await user。 add_roles(role) 我不会在@Nurqm 的代码中遇到任何错误.. 但用户仍在角色中..
      • 在公会的角色层次结构中,您的机器人角色是否低于您要删除的角色?如果是,只需将机器人的角色拖到该角色上方即可。它应该可以工作。
      • bot 角色是可能的最高角色,我已经仔细检查过了 :-(
      猜你喜欢
      • 1970-01-01
      • 2021-07-31
      • 2021-04-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2020-11-19
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多