如何通过角色ID给某人分配角色?

问题描述 投票:0回答:2

我试过了

    @bot.command(pass_context=True)
    @commands.has_role(764795150424866836)
    async def removerole(ctx, user: discord.Member, role: 763045556200931348):
        await user.remove_roles(role)

但是我收到错误:

discord.ext.commands.errors.MissingRequiredArgument: role is a required argument that is missing.

希望你能帮助我。

python-3.x discord.py
2个回答
1
投票

您应该尝试将您的函数重写为:

@bot.command(pass_context=True)
@commands.has_role(764795150424866836)
async def removerole(ctx, user: discord.Member, role=763045556200931348):
    role = ctx.guild.get_role(role)
    await user.remove_roles(remove_role)

您收到该错误的原因是因为您没有预先定义

role
变量。这是通过这样做来完成的:
role=763045556200931348
。相反,您通过这样做来定义变量的类型 (
discord.Member
):
role: 763045556200931348
,这是角色变量的不正确实现。


0
投票

当您指定参数的

:
时,您可以在参数中使用
class
。如果你想赋值,你应该像
=
一样使用
async def removerole(ctx, user: discord.Member, role=763045556200931348):
。但我认为这不是你想要的。您想要从用户中删除角色。你可以通过提及角色来做到这一点。

@bot.command(pass_context=True)
@commands.has_role(764795150424866836)
async def removerole(ctx, user: discord.Member, role: discord.Role):
    if role in user.roles:
        await user.remove_roles(role)

这样,您只需提及要从用户中删除的内容即可。

但是如果您只想输入角色 ID 来删除角色,则可以使用

guild.get_role(id)

@bot.command(pass_context=True)
@commands.has_role(764795150424866836)
async def removerole(ctx, user: discord.Member, role=763045556200931348):
    remove_role = ctx.guild.get_role(role)
    if remove_role in user.roles:
        await user.remove_roles(remove_role)
© www.soinside.com 2019 - 2024. All rights reserved.