Python 如何让某人在Discord.py重写中扮演角色
我正在为我的discord服务器创建一个bot。当用户发出特定命令时,机器人会给他们一个角色 我已经尝试在其他stackoverflow问题中查找其他代码,并查看了Discord.Py重写文档。同样对于视频,它们都不是discord.py rewritePython 如何让某人在Discord.py重写中扮演角色,python,discord,discord.py-rewrite,Python,Discord,Discord.py Rewrite,我正在为我的discord服务器创建一个bot。当用户发出特定命令时,机器人会给他们一个角色 我已经尝试在其他stackoverflow问题中查找其他代码,并查看了Discord.Py重写文档。同样对于视频,它们都不是discord.py rewrite @bot.command() 异步def(ctx): user=ctx.message.author role=discord.utils.get(user.guild.roles,name=“Recruit”) 等待用户.公会.角色(角色)
@bot.command()
异步def(ctx):
user=ctx.message.author
role=discord.utils.get(user.guild.roles,name=“Recruit”)
等待用户.公会.角色(角色)
我希望我能学会如何编写机器人赋予角色的代码
@client.command()
async def role(ctx, member:discord.Member, role: discord.Role): #pass user and role
if role in member.roles: #checks all roles the member has
await member.remove_roles(role) #removes the role
else:
await member.add_roles(role) #adds the role
在这种情况下,代码首先检查成员拥有的所有角色中的指定角色。如果角色不存在,它会添加角色;如果角色已经存在,它会删除角色。介绍了如何创建和添加角色。@Anu6is是什么意思?@Anu6is但它不起作用….@Anu6is wait nvm。因为你,我找到了解决办法。谢谢,LOL,没问题,希望它能解决问题,但请添加对代码的解释,这样用户就能完全理解他/她真正想要的。嗨!我添加了评论和解释。希望这会有帮助。谢谢