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

2023-05-10Python开发问题
3

本文介绍了如果用户无权踢球,则无法让 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 引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11