discord.js监听message事件报错msg.replay不是函数该如何修复?
错误原因
- 弃用警告:discord.js v13及更高版本已废弃
message事件,需使用messageCreate事件替代,原message事件不会被触发,所以机器人收到消息无响应 - 类型错误:属于拼写错误,消息对象的回复方法为
reply(),你代码中误写为replay(),该方法不存在所以抛出错误
修复步骤
- 将事件监听的
message替换为messageCreate - 将
msg.replay()修正为msg.reply(),同时传入需要回复的内容(reply方法不支持空调用,必须传内容参数)
修复后完整代码
import { Client, Intents } from 'discord.js'; export const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); bot.on('ready', () => console.log(`${bot.user.username} is online`)) // 替换废弃的message事件 bot.on('messageCreate', async (msg) => { if (msg.author.bot) return; // 修正拼写错误,传入回复内容 await msg.reply('你想要回复的消息内容') });
提示:如果需要@触发消息的用户,直接使用
msg.reply()即可,不需要额外拼接用户提及标签,方法会自动添加。
内容的提问来源于stack exchange,提问作者Vogel_YT
相关产品推荐
相关产品推荐

