python - 当用户没有权限时发送错误消息(Discord Bot)
问题描述
我正在制作一个不和谐的机器人,并且想在用户没有权限使用我尝试了几个小时的命令(使用 if 和 else)时添加一条“你无权使用此命令”消息,并且可以没办法
@commands.command(aliases=['limpar'])
@commands.has_permissions(manage_messages=True)
async def clear(self, ctx, amount : int):
await ctx.channel.purge(limit=amount+1)
embed=discord.Embed(title="", url="", description=f"Squeaky clean!!!", color=0x000000)
await ctx.send(embed=embed, delete_after=2)
print('Squeaky clean!!!')
解决方案
您可以使用错误处理程序事件,它将用于所有命令。
@commands.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send(f"You do not have {commands.MissingPermissions.missing_perms} to run this command.")
推荐阅读
- elasticsearch - 弹性搜索索引模式中的计算字段
- sql - 在 T-SQL 中的顺序行中适当地设置计数器
- java - Android Activity 生命周期:Onstop() 的问题
- c# - jQuery 和 Razor 执行顺序
- html - 如何在 HTML 中使用 div 创建类似表的结构
- jquery - 在 React 应用程序中扩展 jQuery(自定义链式 jQuery 函数)
- docker - 在 Windows 容器中的 Docker 构建期间运行 NPM 安装
- tfs - 如何使用 TFS 2013 在“错误工作项”上创建“错误类别”,一旦我尝试,我在“参考字段名称”上遇到错误
- moodle - 如何在moodle中查看老师的进度
- javascript - 调用上下文函数在 React 中不起作用