首页 > 解决方案 > 为什么要删除这个错误?类型错误:on_message() 缺少 1 个必需的位置参数:“消息”

问题描述

我正在尝试创建一个带有用户 id 的代码,然后检查它是否与一个匹配,如果匹配,它会发送一条消息...我不断收到错误TypeError: on_message() missing 1 required positional argument: 'message ' ...

@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

解决方案


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

@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)

推荐阅读