如果用户无权踢球,则无法让 discord.py 引发错误

时间:2023-05-10
本文介绍了如果用户无权踢球,则无法让 discord.py 引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试制作一个可以踢人的快速机器人,如果它有权这样做并且用户有权这样做,并且如果机器人没有权限或用户.但是我目前的解决方案似乎不起作用.这是处理它的代码,当您拥有权限时,该代码可以踢人,但如果机器人或用户没有权限,则不会引发错误.

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 下一篇:Discord.py Bot 重命名 VoiceChannel 有时只能工作

相关文章