Can#39;t get discord.py to raise an error if user doesn#39;t have permissions to kick(如果用户无权踢球,则无法让 discord.py 引发错误)
问题描述
我正在尝试制作一个可以踢人的快速机器人,如果它有权这样做并且用户有权这样做,并且如果机器人没有权限或用户.但是我目前的解决方案似乎不起作用.这是处理它的代码,当您拥有权限时,该代码可以踢人,但如果机器人或用户没有权限,则不会引发错误.
I'm trying to make a quick bot that can kick people if it has the permission to do so and the user has the permission to do so as well as raise an error in chat if either the bot doesn't have the permissions or the user. However my current solution doesn't seem to be working. Here is the code that handles it, the code works to kick people when you have the permissions but doesn't raise an error if the bot or the user doesn't have the permissions.
@client.command(name = 'kick', pass_context = True)
@has_permissions(kick_members = True)
@bot_has_permissions(kick_members = True)
async def _kick(ctx, member : discord.Member, *, reason='No Reason Given'):
await member.kick(reason = reason)
await ctx.send(f'{member} was kicked for {reason}')
@_kick.error
async def kick_error(error, ctx):
if isinstance(error, MissingPermissions):
text = (f'Sorry {ctx.message.author}, you do not have permission to do that')
await ctx.send(text)
推荐答案
试试这样的:
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have the permissions required for this command.")
return
raise error
这篇关于如果用户无权踢球,则无法让 discord.py 引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果用户无权踢球,则无法让 discord.py 引发错误


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01