亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號