【发布时间】: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