如何给某人一个带有角色 ID 的角色?

时间:2021-01-08 17:27:45

标签: python-3.x discord.py

我试过了

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

希望你能帮助我。

2 个答案:

答案 0 :(得分: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,这是角色变量的错误实现。

答案 1 :(得分: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)
相关问题