【发布时间】:2021-10-19 22:18:44
【问题描述】:
我有一个 Discord.py 命令,我想做一个自定义权限处理程序。
我的命令:
@commands.command()
@custom_permission("administrator")
async def example(self, ctx, args):
...
在这种情况下,@custom_permission() 是 Permission 处理程序,现在我如何使它适用于 async def 中的装饰器?
装饰器功能:
async def custom_permission(permission):
async def predicate(ctx, permission):
if ctx.author.id in config.owners:
return True
elif permission == "administrator":
if ctx.author.guild_permissions.administrator:
return True
else:
embed = discord.Embed(timestamp=ctx.message.created_at, description=f"You do not meet the required guild permissions the command \"`{ctx.command.name}`\" requires to be executed.\n\nYou need `{permission.upper()}` Permission in this Guild to be able to execute/run/use this command.", color=242424)
embed.set_author(name="Insufficient Permissions", icon_url=config.forbidden_img)
embed.set_footer(text="Numix", icon_url=config.logo)
await ctx.send(embed=embed)
elif permission == "manage_messages":
if ctx.author.guild_permissions.manage_messages:
return True
else:
embed = discord.Embed(timestamp=ctx.message.created_at, description=f"You do not meet the required guild permissions the command \"`{ctx.command.name}`\" requires to be executed.\n\nYou need `{permission.upper()}` Permission in this Guild to be able to execute/run/use this command.", color=242424)
embed.set_author(name="Insufficient Permissions", icon_url=config.forbidden_img)
embed.set_footer(text="Numix", icon_url=config.logo)
await ctx.send(embed=embed)
elif permission == "kick":
if ctx.author.guild_permissions.kick:
return True
else:
embed = discord.Embed(timestamp=ctx.message.created_at, description=f"You do not meet the required guild permissions the command \"`{ctx.command.name}`\" requires to be executed.\n\nYou need `{permission.upper()}` Permission in this Guild to be able to execute/run/use this command.", color=242424)
embed.set_author(name="Insufficient Permissions", icon_url=config.forbidden_img)
embed.set_footer(text="Numix", icon_url=config.logo)
await ctx.send(embed=embed)
elif permission == "ban":
if ctx.author.guild_permissions.ban:
return True
else:
embed = discord.Embed(timestamp=ctx.message.created_at, description=f"You do not meet the required guild permissions the command \"`{ctx.command.name}`\" requires to be executed.\n\nYou need `{permission.upper()}` Permission in this Guild to be able to execute/run/use this command.", color=242424)
embed.set_author(name="Insufficient Permissions", icon_url=config.forbidden_img)
embed.set_footer(text="Numix", icon_url=config.logo)
await ctx.send(embed=embed)
elif permission == "manage_guild":
if ctx.author.guild_permissions.manage_guild:
return True
else:
embed = discord.Embed(timestamp=ctx.message.created_at, description=f"You do not meet the required guild permissions the command \"`{ctx.command.name}`\" requires to be executed.\n\nYou need `{permission.upper()}` Permission in this Guild to be able to execute/run/use this command.", color=242424)
embed.set_author(name="Insufficient Permissions", icon_url=config.forbidden_img)
embed.set_footer(text="Numix", icon_url=config.logo)
await ctx.send(embed=embed)
return commands.check(predicate(ctx, permission))
现在,我该如何进行这项工作? 我无法将其更改为正常功能,因为如果我这样做了,那么在满足权限要求时我无法发送嵌入消息。
【问题讨论】:
标签: python discord.py python-decorators