【问题标题】:How to display error message if member doesn't have permissions? Discord.py如果成员没有权限,如何显示错误消息?不和谐.py
【发布时间】:2020-02-16 16:51:26
【问题描述】:
如果成员没有管理消息权限,我正在尝试制作一个简单的明确命令并让它返回错误消息。
@bot.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=10):
await ctx.channel.purge(limit=amount)
【问题讨论】:
标签:
python
discord
discord.py
discord.py-rewrite
【解决方案1】:
这样就可以了。
@bot.command()
async def clear(ctx, amount = 10):
authorperms = ctx.author.permissions_in(ctx.channel)
if authorperms.manage_messages:
await ctx.channel.purge(limit=amount)
else:
await ctx.send("You don't have the permissions to do that!")
可能有更好的方法来执行此操作,方法是使用在没有正确权限的人尝试使用命令时引发的异常,或者覆盖 on_error 方法,但我不知道如何。这应该会为您解决问题,直到您找到更好的解决方案。