为了账号安全,请及时绑定邮箱和手机立即绑定

Disxord.py 上的非前缀消息

Disxord.py 上的非前缀消息

当年话下 2023-01-04 10:06:12
@client.eventasync 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_messageTraceback (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 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

我不知道你从哪里得到代码,但我在 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 似乎已经迁移到新版本了。这是快速入门文档中的新方法:


@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_commands是一种方法discord.ext.commands.Bot,不是client。所以它应该是bot.process_commands(message)。


查看完整回答
反对 回复 2023-01-04
  • 1 回答
  • 0 关注
  • 51 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信