如何在不协调的情况下添加角色层次结构.用户齿轮



我有一个使用discord的ban命令。用户认为禁止不在公会中的成员,但我想添加角色层次结构,但当我检查文档时,没有属性,有办法同时做到这两个吗?具有角色层次结构,能够禁止不在公会中的用户

您可以遍历用户的角色,并检查他在列表中是否有角色。我更喜欢使用角色ID,因为它无法更改。

如果用户不在服务器中,则使用typing.Union获取成员或id

from typing import Union
@client.command()
@commands.bot_has_permissions(ban_members=True)
@commands.has_permissions(ban_members=True)
async def ban(ctx, user: Union[discord.Member, int]):
if isinstance(user, int):
# user is not in guild -> ban him by id
pass
else:
whitelisted_roles = [123456, 456789, 789012] # List of Mod roles 
for role in user.roles:
if role.id in whitelisted_roles:
return await ctx.send("You can't ban this user! He is a moderator!")
else:
pass
# ban the member

欢迎来到StackOwerflow!

是的,可以检查角色层次结构,也可以禁止使用user_ids的用户。

我建议你看看古德迪的答案。

第一个也是最重要的:

  • 您需要检查角色继承权
  • 要做到这一点,以下是代码:
if not (user.guild_permissions.administrator and user.top_role.position > ctx.me.top_role.position and user.top_role.position > ctx.author.top_role.position):
user.ban()

要禁止不在公会中的用户,

if isinstance(user, int):
user = await bot.fetch_user(user)
await ctx.guild.ban(user)

我希望这会有所帮助。

最新更新