在discord.py
中,如果你想让某些命令对管理员不可用,但又不想完全移除他们的权限,可以通过检查执行命令的用户是否具有管理员权限来实现。以下是如何实现这一点的步骤:
以下是一个示例代码,展示如何在discord.py
中实现这一功能:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
def is_not_admin(ctx):
return not ctx.author.guild_permissions.administrator
@bot.command(name='forbidden_command')
@commands.check(is_not_admin)
async def forbidden_command(ctx):
await ctx.send("You do not have permission to use this command.")
@forbidden_command.error
async def forbidden_command_error(ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send("This command is not available to administrators.")
else:
raise error
bot.run('YOUR_BOT_TOKEN')
通过这种方式,你可以确保管理员无法使用特定的命令,同时保持他们的其他权限不变。
领取专属 10元无门槛券
手把手带您无忧上云