如何使用Discord Bot添加角色

2024-04-25 18:07:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始用Discord的pythonapi创建Discord机器人。因此,我尝试发出一个命令,在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}.")

当我尝试使用该命令时,任何错误都会发生。我试着像我做过的websearch一样做,但仍然不起作用。有人能帮我吗


Tags: 命令服务器client角色密码pythonapi机器人await
1条回答
网友
1楼 · 发布于 2024-04-25 18:07:55

我不认为这些基本的事情需要不和谐的意图,尤其是“成员意图”。我所做的是清理了您的代码,并要求在需要时在多字符串角色之前传递一个成员

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

相关问题 更多 >