Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

python 3.x - How to give someone a role with role ID?

I tried

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

But i get an error:

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

Hopefully you can help me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You use : in parameters when you specify the class of the parameter. If you want to assign a value, you should use = like async def removerole(ctx, user: discord.Member, role=763045556200931348):. But I think that that's not what you want. You want to remove the role from the user. You can do it by mentioning the role.

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

With that, you just have to mention the that you want to remove from the user.

But if you just want to type the role id to remove the role, you can use 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...