[英]How can i make the ban command in discord.py not ban admins?
我的 discord.py 機器人中有一個禁止命令。 權限有效,因此如果您不是管理員,則無法禁止某人。 但是,當管理員試圖禁止其他管理員或高於他的角色時,它會起作用。 我該怎么做才能讓管理員只能禁止他們下面的角色?
@commands.command(description = 'Bans a specified member with an optional reason')
@commands.has_permissions(ban_members = True)
async def ban(self,ctx, member:discord.Member, *, reason = "unspecified reason"):
if member.id == ctx.author.id:
await ctx.send("You cannot ban yourself, sorry! :)")
return
else:
await member.ban(reason = reason)
reasonEmbed = discord.Embed(
description = f'🔒👮♂️Succesfully banned {member.mention} for {reason}\n \n ',
colour = 0xFF0000
)
reasonEmbed.set_author(name=f"{member.name}" + "#"+ f"{member.discriminator}", icon_url='{}'.format(member.avatar_url))
reasonEmbed.set_footer(text=f"Banned by {ctx.author.name}", icon_url = '{}'.format(ctx.author.avatar_url))
await ctx.send(embed=reasonEmbed)
您可以使用>=
和top_role
屬性來比較用戶與您嘗試禁止的成員的角色,如果您的權限低於您嘗試禁止的成員,它將阻止 rest 的代碼運行. 這是一個簡單的方法,
if member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only moderate members below your role")
return
這是應用於您的代碼的示例,
@commands.command(description = 'Bans a specified member with an optional reason')
@commands.has_permissions(ban_members = True)
async def ban(self,ctx, member:discord.Member, *, reason = "unspecified reason"):
if member.id == ctx.author.id:
await ctx.send("You cannot ban yourself, sorry! :)")
return
if member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only moderate members below your role")
return
else:
await member.ban(reason = reason)
reasonEmbed = discord.Embed(
description = f'🔒👮♂️Succesfully banned {member.mention} for {reason}\n \n ',
colour = 0xFF0000
)
reasonEmbed.set_author(name=f"{member.name}" + "#"+ f"{member.discriminator}", icon_url='{}'.format(member.avatar_url))
reasonEmbed.set_footer(text=f"Banned by {ctx.author.name}", icon_url = '{}'.format(ctx.author.avatar_url))
await ctx.send(embed=reasonEmbed)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.