【问题标题】:Why remove this error? TypeError: on_message() missing 1 required positional argument: 'message'为什么要删除这个错误?类型错误:on_message() 缺少 1 个必需的位置参数:“消息”
【发布时间】:2021-05-19 17:52:26
【问题描述】:

我正在尝试创建一个带有用户 ID 的代码,然后检查它是否与一个匹配,如果匹配则发送一条消息...我不断收到错误 TypeError: on_message() missing 1所需的位置参数:“消息”...

@client.event
async def on_message(ctx,message):
    member = message.author.id 
    if member == <userid>:
        await ctx.send("yo whats up")
    else:
        return
    await client.process_commands(message)

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您没有按照应有的方式定义函数。 on_message 事件只接受一个参数:message。在on_message 事件中,您将不得不在没有ctx 参数的情况下工作。考虑到这一点,您可以从以下位置重新格式化当前功能代码:

    @client.event
    async def on_message(ctx,message):
        member = message.author.id 
        if member == <userid>:
            await ctx.send("yo whats up")
        else:
            return
        await client.process_commands(message)
    

    到:

    @client.event
    # Only pass in "message" argument
    async def on_message(message):
        member = message.author.id 
        if member == <userid>:
            # Use message.channel.send() instead of ctx.send()
            await message.channel.send("yo whats up")
        else:
            return
        await client.process_commands(message)
    

    【讨论】:

    • 哦,我明白了,所以 on_message 只能有一个参数。非常感谢!
    猜你喜欢
    • 2021-05-03
    • 2021-01-16
    • 2021-05-10
    • 2021-05-04
    • 2019-05-26
    • 1970-01-01
    • 2019-10-30
    • 2022-01-11
    相关资源
    最近更新 更多