首页 > 解决方案 > 我怎样才能允许人们在 discord.py 中只踢低于他们角色的成员?

问题描述

我正在尝试使用 Python 和 discord.py 库创建一个 kick 命令,但我不知道如何让人们只能踢低于其角色的成员,因此版主将无法踢管理员。

在此处输入图像描述

标签: discorddiscord.pydiscord.net

解决方案


您可以将Member对象与名为的函数进行比较top_role

一个示例代码是:

@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    if member.top_role >= ctx.author.top_role: # Check if the role is below the authors role
        await ctx.send("You can only kick users with a lower role!")
        return
    else:
        await member.kick(reason=reason)
        await ctx.send(f"{member.id} got kicked with the reason: {reason}.")

推荐阅读