我希望能够将此代码更改为员工无论职位如何都不能禁止员工,并且不会像管理员不能禁止 mod 那样担任更高的角色。我试图将其与员工角色相关联,但机器人不发送输出。
@commands.command()
@commands.has_permissions(ban_members=True)
@commands.bot_has_guild_permissions(ban_members=True)
async def ban(self, ctx, user, *, reason: commands.clean_content = '[No reason given]'):
""" Ban a user from the current guild. """
author = ctx.author
guild = ctx.guild
user = await self.fetch_user(ctx, user)
if guild.get_member(user.id) and (guild.get_member(user.id).top_role.position >= guild.me.top_role.position): >This is where heirarchy comes into play
raise GenericCustomException(
"Sorry, but I can not ban that user. Maybe try checking my role hierarchy?"
)
else: # this is where I try to pass the mod role to be ignored from banning each other
if discord.utils.get(user.roles, id=<insert mod role id here>):
return await ctx.send("you cannot ban a staff member")
答案 0 :(得分:0)
我会尝试搜索用户是否拥有您的某个角色拥有的特定权限,如果为真,则发送消息。例如,如果我想查看用户是否拥有 ban_members
权限:
if user.guild_permissions.ban_members:
return await ctx.send("you cannot ban a staff member")
确保将 ban_members
替换为适合您需要的任何其他权限。
答案 1 :(得分:0)
您可以遍历用户的角色并检查他是否在 mod 角色列表中拥有角色。我更喜欢使用角色 ID,因为它无法更改。
@commands.command()
@commands.has_permissions(ban_members=True)
@commands.bot_has_guild_permissions(ban_members=True)
async def ban(self, ctx, user, *, reason: commands.clean_content = '[No reason given]'):
""" Ban a user from the current guild. """
author = ctx.author
guild = ctx.guild
user = await self.fetch_user(ctx, user)
if guild.get_member(user.id) and (guild.get_member(user.id).top_role.position >= guild.me.top_role.position): >This is where heirarchy comes into play
raise GenericCustomException(
"Sorry, but I can not ban that user. Maybe try checking my role hierarchy?"
)
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:
# ban the user
pass