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

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

我有以下代码

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')

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

我还添加了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}.")

【问题讨论】:

  • 您确认role.members 确实包含任何用户吗?成员缓存有时是不可靠的,尤其是在没有特权成员意图的情况下
  • 目前角色“test”有3个用户。
  • 当您执行print(role.members) 之类的操作时,它们三个都会出现吗?
  • 我添加了await ctx.send(role.members),结果我得到了一个空列表。
  • 那是你的问题。您可以尝试启用特权成员意图(如果您的机器人在 100 台或更多服务器中,这需要 Discord 员工的验证和白名单)。这应该会填充公会的成员缓存,然后是 role.members 列表

标签: python discord discord.py


【解决方案1】:

根据 cmets 中的讨论,问题在于未填充 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}.")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 2021-04-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多