【问题标题】:TypeError: on_command_error() missing 1 required positional argument: 'ctx'TypeError:on_command_error() 缺少 1 个必需的位置参数:'ctx'
【发布时间】:2021-12-15 01:57:04
【问题描述】:

错误: TypeError:on_command_error() 缺少 1 个必需的位置参数:'ctx'

但唯一的问题是定义了 ctx:

@bot.event
async def on_command_error(self, error: commands.CommandError, ctx):

    if isinstance(error, commands.CommandNotFound):
        return
    elif isinstance(error, commands.CommandOnCooldown):
        message = f"This command is on cooldown. Please try again after {round(error.retry_after, 1)} seconds."
    elif isinstance(error, commands.MissingPermissions):
        message = "You are missing the required permissions to run this command!"
    elif isinstance(error, commands.UserInputError):
        message = "Something about your input was wrong, please check your input and try again!"
    else:
        message = "Oh no! Something went wrong while running the command!"

    await ctx.send(message, delete_after=5)
    await ctx.message.delete(delay=5)

还是我太傻了?

【问题讨论】:

  • 没有看到对on_command_error 的实际调用很难说。但可能它被称为 on_command_error(error, ctx) instead of someobject.on_command_error(error, ctx). The argument self` 隐含在第二个而不是第一个中。
  • 也许您可以在问题中包含调用代码,以获得更好的答案

标签: python discord.py


【解决方案1】:

如果你想在没有 cog 的情况下使用事件,你需要像这样定义它:


async def on_command_error(ctx, error: commands.CommandError):

【讨论】:

  • 谢谢,我已经尽一切努力让它发挥作用,你值得点赞:)
  • 谢谢,你能接受我的回答吗?
猜你喜欢
  • 2021-02-01
  • 2020-04-02
  • 2021-01-16
  • 2014-09-13
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 2017-03-27
相关资源
最近更新 更多