简体   繁体   English

尝试在 python 上执行缺少 discord 机器人所需权限的错误消息

[英]Trying to do an error message of missing required permission for discord bot on python

I am making an discord bot on python, and I don't wanna that regular users used the moderation commands.我正在 python 上制作 discord 机器人,我不希望普通用户使用审核命令。 I know how to do a check for permissions, @commands.has_permissions(kick_members=True) , but I don't know how to do an error message of missing required permissions.我知道如何检查权限@commands.has_permissions(kick_members=True) ,但我不知道如何处理缺少所需权限的错误消息。 I tried that:我试过了:

@kick.error
async def kick_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send(':redTick: You don\'t have permission to kick members.')

I found that on Stack Overflow我在 Stack Overflow 上发现了

But in the cmd it says that: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: kick_error() missing 1 required positional argument: 'error'但是在 cmd 中它说: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: kick_error() missing 1 required positional argument: 'error'

And if I swapped 'ctx' and 'error', like async def kick_error(self, error, ctx): , it says the same thing, only instead of missing 1 required positional argument: 'error' he wrote missing 1 required positional argument: 'ctx'如果我交换 'ctx' 和 'error',比如async def kick_error(self, error, ctx): ,它说的是同样的事情,只是没有missing 1 required positional argument: 'error'他写了missing 1 required positional argument: 'ctx'

How can I fix it?我该如何解决? And is there another way to do it?还有另一种方法吗?

You need to check if the error is instance of commands.MissingRequiredArgument您需要检查错误是否是commands.MissingRequiredArgument的实例

Seeing the error I assume that you are not in a cog, so remove self看到错误我认为您不在齿轮中,因此请删除self

@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send(':redTick: You don\'t have permission to kick members.')
    elif isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("Missing Argument")
    else: raise(error) # for other errors so they dont get suppressed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试安装Discord Bot Cog时发生Python权限错误 - Python permission error when trying to install a Discord bot cog Discord 机器人问题“缺少必需的参数” python - Discord bot issue "is a required argument missing" python Python 缺少 1 个必需的位置参数(Discord Bot) - Python missing 1 required positional argument (Discord Bot) Discord bot kick 命令错误消息 python - Discord bot kick command error message python Python Discord 机器人 - 缺少 1 个必需的位置参数:“名称” - Python Discord Bot - missing 1 required positional argument: 'name' 尝试在出现该错误时将 discord 重命名权限错误转换为默认消息,我该怎么做? - Trying To Convert a discord rename permission error to a default message when that error comes up, How Do I do That? 尝试运行Discord机器人时出错(Python) - Error while trying to run a discord bot (python) 尝试运行命令时收到错误,提示“ctx 是缺少的必需参数”。 [Python,discord.py] - Am receiving an error when trying to run a command saying 'ctx is a required argument that is missing'. [Python, discord.py] 尝试制作 discord.py 级别机器人时出现错误消息 - Error message while trying to make discord.py level bot discord 机器人缺少执行命令的权限 - discord bot missing a permission to execute a command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM