【问题标题】:How to add a role with a Discord Bot如何使用 Discord Bot 添加角色
【发布时间】:2021-01-26 08:46:43
【问题描述】:

我开始使用 Discord 的 Python API 创建一个 Discord 机器人。因此,我尝试制作一个在不和谐服务器上赋予角色的命令。这是我的代码:

client = commands.Bot(command_prefix = '/')

@client.command()
async def addrole(ctx, role: discord.Role, user: discord.Member):
    if ctx.author.guild_permissions.administrator:
        await user.add_roles(role)
        await ctx.send(f"Successfully given {role.mention} to {user.mention}.")

当我尝试使用该命令时,会出现任何错误。我尝试像我所做的网络搜索一样,但仍然无法正常工作。有人可以帮我吗?

【问题讨论】:

  • 出现什么样的错误?这可能是一个意图问题。看看this answer。另一件事是确保您的机器人不会试图赋予高于其自身角色的角色。那也行不通。
  • 请提供错误,还有什么是“角色”?

标签: python discord.py


【解决方案1】:

我不相信像这样的基本事情需要 Discords 意图,尤其是“成员意图”。我所做的是清理了您的代码,并要求在多字符串角色之前传递一个成员(如果需要)。

@client.command()
@commands.has_permissions(manage_roles=True)
async def addrole(ctx, member: discord.Member = None, *, role: discord.Role = None):
        if role == None:
                await ctx.send(f'Provide a role to add')
                return

        if member == None:
                await ctx.send(f'Provide a member to add a role')
                return

        await member.add_roles(role)
        await ctx.send(f"Successfully added role, {role} to {member.name}")

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 2020-09-02
    • 2023-03-19
    • 2020-06-29
    • 1970-01-01
    • 2018-11-26
    • 2020-08-16
    • 2020-07-13
    相关资源
    最近更新 更多