简体   繁体   English

如何在 discord.py 中赋予变量一个角色 - Discord Python

[英]How to give a variable a role in discord.py - Discord Python

I want to specify that the variable "role" is a role that I created named 'testcom'.我想指定变量“role”是我创建的名为“testcom”的角色。 So that when i call it in user.add_roles, it will give the user the specified role.这样当我在 user.add_roles 中调用它时,它会给用户指定的角色。 Any help?有什么帮助吗? Thank you.谢谢你。

@client.command(pass_context=True)
async def testcom(ctx, user: discord.Member):
    role = ##help here ##
    await user.add_roles(role)
    await ctx.send("done")
role = discord.utils.get(guild.roles,name="name_of_the_role_you_need")

This way role is a variable that contains a discord.Role object.这种方式role是一个包含discord.Role object 的变量。


You can also get the role by int id:也可以通过int id 获取角色:

role = ctx.guild.get_role(0238023802302) # put your id

I would suggest you to read the content of this gist too.我建议你也阅读这个要点的内容。

You need to access the role id from the guild:您需要从公会访问角色 ID:

@client.command(pass_context=True)
async def testcom(ctx, user: discord.Member):
    role = ctx.guild.get_role(role int here)
    await user.add_roles(role)
    await ctx.send("done")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM