就像 Dank memer bot 在 help 命令中所做的那样 我还希望在我的自定义帮助命令中使用该功能 enter image description here
我使用了此代码,但它无法用于自定义装饰器
import traceback
#This is the command
@bot.command()
@commands.has_permissions(administrator=True, manage_messages=True)
async def func(ctx):
pass
# In your whatever file or error handler.
command = bot.get_command("func") # Get the Command object
try:
# You can pretty much loop these to get all checks from the command.
check = command.checks[0] # get the first check
check(0) # This would raise an error, because `0` is passed as ctx
except Exception as e:
frames = [*traceback.walk_tb(e.__traceback__)] # Iterate through the generator
last_trace = frames[-1] # get the last trace
frame = last_trace[0] # get the first element to get the trace
print(frame.f_locals['perms']) # Output: {'administrator': True, 'manage_messages': True}
答案 0 :(得分:1)
如果您在错误处理程序中有该命令,您可以简单地使用 missing_perms
属性
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
missing_perms = error.missing_perms
await ctx.send(f"You are missing: {missing_perms} to run this command")