python-3.x 如何给给予某人一个具有角色ID的角色?

piok6c0g  于 4个月前  发布在  Python
关注(0)|答案(2)|浏览(75)

我试

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


希望你能帮我。

zdwk9cvp

zdwk9cvp1#

你应该试着把你的函数重写成这样:

@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,这是角色变量的错误实现。

uqdfh47h

uqdfh47h2#

当你指定参数的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)

相关问题