【问题标题】:TypeError. on_message() missing 1 required positional argument: 'ctx'类型错误。 on_message() 缺少 1 个必需的位置参数:'ctx'
【发布时间】:2021-01-16 03:36:48
【问题描述】:

我尝试制作一个自动响应机器人,以在每次输入时提供自动支持之类的功能:hey i

机器人输出此错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\lequi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'ctx'

但我在 de 函数中做了 ctx,所以我不知道代码有什么问题 我希望你们能帮助我:D

如果可以的话请帮帮我:D

@bot.event
async def on_message(message, ctx):
    if 'hey' in message.content.lower():
        if 'i' in message.content.lower():
            embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
            await ctx.send(embed=embed)


    if 'test' in message.content.lower():
        embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
        await ctx.send(embed=embed)```

【问题讨论】:

    标签: python discord discord.py discord.py-rewrite traceback


    【解决方案1】:

    根据documentation on_message 只需要一个参数,即message。要解决此问题,您只需删除 ctx 作为参数即可。

    async def on_message(message):
    

    但我在 de 函数中做了 ctx,所以我不知道代码有什么问题,希望你们能帮助我:D

    你有一个额外的参数ctx,它不应该在那里。结果,Discord 将调用此事件并且永远不会在其中传递 ctx,因此调用的函数将缺少 1 个参数,从而引发该错误。该错误确实不是意味着您没有将它添加到函数签名中,而是它没有被任何调用它的函数传递给

    【讨论】:

    • 我该如何解决这个问题?
    • 我告诉过你如何解决它...我的回答字面意思是to fix it you should {do this}
    【解决方案2】:

    on_message 只有 1 个输入参数(请参阅documentation)。

    因此你的代码:

    @bot.event
    async def on_message(message, ctx):
        if 'hey' in message.content.lower():
            if 'i' in message.content.lower():
                embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
                await ctx.send(embed=embed)
    
    
        if 'test' in message.content.lower():
            embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
            await ctx.send(embed=embed)
    

    不正确,因为您指定了 2 个输入参数,而不是 1 个(即您正在添加一个新的必需位置参数)。


    所以现在的问题是为什么它说:TypeError. on_message() missing 1 required positional argument: 'ctx'

    这很简单,执行 on_message 事件的过程没有第二个输入参数,因为它只需要一个输入参数而不是两个。从而抛出这个错误。


    删除 ctx 将解决问题,如果没有,那么你有其他代码干扰(你没有显示的东西)。下面是如何执行此操作的示例。

    @bot.event
    async def on_message(message):
        if 'hey' in message.content.lower():
            if 'i' in message.content.lower():
                embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
                await message.channel.send(embed=embed)
    
    
        if 'test' in message.content.lower():
            embed = discord.Embed(title=f'test',description=f'**test** : ``test``', color=0x001fff )
            await message.channel.send(embed=embed)
    

    【讨论】:

    • 我该如何解决这个问题?
    • 那是什么?我在最后一句中说你需要做什么......
    猜你喜欢
    • 2021-05-03
    • 2021-05-10
    • 2021-02-01
    • 2020-04-02
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多