【问题标题】:How to give someone a role in the Discord.py Rewrite如何在 Discord.py 重写中赋予某人一个角色
【发布时间】:2019-10-28 23:34:00
【问题描述】:

我正在为我的 discord 服务器创建一个机器人。当用户说出特定命令时,机器人会给他们一个角色

我已经尝试在其他 stackoverflow 问题中查找其他代码,并查看了 Discord.Py 重写文档。同样对于视频,它们都不是 discord.py 重写

@bot.command()
async def recruitme(ctx):
    user = ctx.message.author
    role = discord.utils.get(user.guild.roles, name="Recruit")
    await user.guild.roles(role)

我希望我可以学习如何编写机器人赋予角色的代码。

【问题讨论】:

  • This answer 介绍了如何创建和添加角色。
  • @Anu6is 什么意思?
  • @Anu6is 但它不起作用.....
  • @Anu6is 等待 nvm。因为你,我找到了解决方案。谢谢
  • 哈哈,没问题

标签: python discord discord.py-rewrite


【解决方案1】:
@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

在这种情况下,代码首先检查成员拥有的所有角色中的指定角色。如果角色不存在则添加该角色,如果已存在则删除它。

【讨论】:

  • 希望它能解决问题,但请添加对代码的解释,以便用户完全理解他/她真正想要的。
  • 嗨!我添加了 cmets 和一个解释。希望它会有所帮助。谢谢,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2020-09-26
  • 2022-01-10
  • 2021-03-17
  • 1970-01-01
  • 2021-04-12
相关资源
最近更新 更多