我正在尝试使用命令从用户中删除角色,但我不太确定该命令是如何工作的。
这是代码:
@bot.command(pass_context=True)
@commands.has_role('staff')
async def mute(ctx, user: discord.Member):
await bot.remove_roles(user, 'member')
await bot.say("{} has been muted from chat".format(user.name))
最佳答案
它看起来像 remove_roles
需要一个 Role
对象,而不仅仅是角色的名称。我们可以使用 discord.utils.get
获得角色
from discord.utils import get
@bot.command(pass_context=True)
@commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role = get(ctx.message.server.roles, name='member')
await bot.remove_roles(user, role)
await bot.say("{} has been muted from chat".format(user.name))
我不知道如果您尝试删除目标成员没有的角色会发生什么,因此您可能需要进行一些检查。如果您尝试从服务器管理员中删除角色,这也可能会失败,因此您可能也想检查一下。
关于python - 如何从用户中删除角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48906438/