python - 如何从用户中删除角色?

标签 python discord.py

我正在尝试使用命令从用户中删除角色,但我不太确定该命令是如何工作的。

这是代码:

@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/

相关文章:

python - 引发 MissingRequiredArgument(参数)discord.ext.commands.errors.MissingRequiredArgument : cooldown is a required argument that is missing

python - 如何隐藏QGraphicSscene中的特定项目?

python - 用 python 解析 puppet-api yaml

python - xlsxwriter Pandas 框架: to highlight rows if there are blank cells within a column

python - python 中 Map() 的固定参数

python - 如何在on_message中检查用户是否具有特定角色?

python - Discord.py 机器人不发送键帽表情符号

python - Discord python 解禁成员

python - sympy:如何因缺少常数项而得到零

python - 如何让后台任务仅在特定条件成立时运行?