Disxord.py上的非前缀消息

时间:2020-06-10 11:43:03

标签: python discord.py

@client.event
async def on_message(message,channel):
    if message.content.startswith("sa"):
        await channel.send(message.channel, "as")

    await client.process_commands(message)

当我说as时,此代码应说sa。它检测到单词,但不响应。这是我得到的错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\---\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'channel'

我在想这可能是过时的代码,因此我尝试将其更改为尽可能新的内容,但是我遇到了该错误。

 @client.event
 async def on_message(message):
     if message.content.startswith('sa'):
         await message.channel.send('as')
     await client.process_commands(message)

1 个答案:

答案 0 :(得分:1)

我不知道您从哪里获得代码,但是我在2018年所做的一个旧项目使用此函数签名:

client = discord.Client()

@client.event
async def on_message(message):
    if message.content.startswith("sa"):
        await client.send_message(message.channel, "as")

但是,从那时起,它看起来像discord.py has migrated to a new version。这是从quickstart documentation开始的新方法:

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

所以您想要的可能是最后几部分:

@client.event
async def on_message(message):
    if message.content.startswith('sa'):
        await message.channel.send('as')

编辑

您的代码似乎也弄错了process_commands部分。 process_commandsdiscord.ext.commands.Bot而非client的方法。因此应该是bot.process_commands(message)

相关问题