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

时间:2021-02-18 09:25:10

标签: python discord discord.py

由于我的问题,我昨天已经发布了,但我们无法解决。

我有以下代码

from discord.ext import commands
from discord.utils import get
import discord

client = commands.Bot(command_prefix='.', help_command=None)


@client.command()
async def swipe(ctx, role: discord.Role):
    for member in role.members:
        await member.remove_roles(role)
        await ctx.send(f"Successfully removed all members from {role.mention}.")


@client.command()
async def add(ctx, role: discord.Role, user: discord.Member):
    if ctx.author.guild_permissions.administrator:
        await user.add_roles(role)
        await ctx.send(f"**Successfully give {role.mention} to {user.mention}.**")


@client.command()
async def remove(ctx, role: discord.Role, user: discord.Member):
    if ctx.author.guild_permissions.administrator:
        await user.remove_roles(role)
        await ctx.send(f"**Successfully removed {role.mention} from {user.mention}.**")



client.run('censored')

我的“添加”和“删除”命令完美运行。 我的命令“滑动”不想工作。 机器人具有管理权限,所选角色位于机器人下方的层次结构中。

我还添加了 if ctx.author.guild_permissions.administrator: 来检查是否有问题,但它没有任何改变。

我的回溯中不会出现任何错误,但是在我使用“swipe”这个命令后,用户仍然在角色中。

为了排除错误,我还与另一个用户创建了一个新的 Discord-Server 并在那里进行了尝试。结果一样。

我的命令有什么问题?

 @client.command()
    async def swipe(ctx, role: discord.Role):
        for member in role.members:
            await member.remove_roles(role)
            await ctx.send(f"Successfully removed all members from {role.mention}.")

1 个答案:

答案 0 :(得分:0)

根据评论中的讨论,问题是未填充 role.members 列表。这可以通过启用 privileged members intent 来解决。请注意,如果您的机器人在 100 台或更多服务器中,这需要 Discord 工作人员进行验证和列入白名单。

要在您的机器人中使用其他意图,您需要先在 discord developers profile 中为您的机器人启用意图。完成后,像这样将意图添加到您的机器人客户端

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix='.', help_command=None, intents=intents)

这样,您的服务器的成员缓存(以及随后的 role.members 列表)应在启动时填充,使您的命令能够正常工作


附带说明,您可能希望将确认消息行移出一个缩进级别,否则机器人会在从角色中删除每个成员后发送一条消息

@client.command()
async def swipe(ctx, role: discord.Role):
    for member in role.members:
        await member.remove_roles(role)
    await ctx.send(f"Successfully removed all members from {role.mention}.")
相关问题