Discord.js 机器人不回复消息:发送confess后无法检测后续回复
Discord.js 告白机器人响应异常排查与修复
核心错误点(按影响优先级排序)
- DM频道判断逻辑完全失效
- 错误使用单等号
=做赋值代替双等号==/三等号===做条件判断:if(message.channeltype = 'dm')会强制将channeltype属性设置为'dm',判断永远为真,完全失去频道过滤作用 - 属性名拼写错误:Discord SDK中不存在
channeltype属性,正确的DM频道判断属性是message.channel.type,对应枚举值为'DM'
- 错误使用单等号
awaitMessagesAPI调用完全不符合规范- 传入参数格式错误:官方API要求第一个入参是筛选配置对象,原代码传入了字符串
"response",导致等待逻辑直接失效 - 未设置有效筛选规则,无法定向捕获对应用户的消息
- 传入参数格式错误:官方API要求第一个入参是筛选配置对象,原代码传入了字符串
- 全局事件嵌套注册的设计错误
在消息回调内部重复注册client.on("message", ...)监听器,每触发一次confess指令就会新增一个永久存在的全局监听器,不仅会造成内存泄漏,还会导致逻辑冲突,根本无法定向等待对应用户的后续输入 - 代码块作用域缺失
所有条件分支没有用大括号{}包裹,导致awaitMessages逻辑不属于confess指令的判断分支,执行流程完全混乱 - 频道获取方法错误
DM频道对象没有.get()方法,需要通过client.channels.fetch(频道ID)异步拉取对应频道实例,才能完成消息发送
修复实现逻辑
用串联式的多轮消息等待替代嵌套全局事件监听,完整交互链路如下:
- 前置校验:仅处理来自DM频道、非机器人发送、内容完全匹配
confess的用户消息,提前过滤无效输入 - 触发初始提示后,第一次等待用户输入:仅匹配发起confess指令的同一个用户,在2分钟内发送的第一条消息,作为要投递的私密告白内容
- 拿到告白内容后,发送服务器选择提示,第二次等待用户输入:仅匹配同一个用户发送的、内容为"1"或"2"的有效选择消息
- 根据用户选择的目标服务器,通过预先配置的频道ID异步拉取对应日志频道和投递频道实例,先发送投递者身份信息到日志频道,再把私密内容匿名发送到投递频道,最后告知用户投递完成
- 增加超时异常处理,用户未在规定时间内完成操作时,提示用户重新发起流程
修复参考代码
// 预先配置固定的投递目标常量,替换为实际频道ID const TARGET_CONFIG = { "1": { logChannelId: "替换为你原代码中WDIC1对应的频道ID", deliverChannelId: "替换为你原代码中CC1对应的频道ID" }, "2": { logChannelId: "替换为你原代码中WDIC2对应的频道ID", deliverChannelId: "替换为你原代码中CC2对应的频道ID" } } // Discord.js v14+ 请使用messageCreate事件,旧版本可替换为message事件 client.on("messageCreate", async (message) => { // 前置校验,不满足条件直接退出逻辑 if ( message.channel.type !== "DM" || message.author.bot || message.content.trim() !== "confess" ) return; // 发送初始提示语 await message.channel.send("OOOOOOoooooo A SECRET??!?!?! WHAT IS IT?!??!"); try { // 等待用户发送告白内容 const contentCollector = await message.channel.awaitMessages({ filter: (msg) => msg.author.id === message.author.id, max: 1, time: 120_000, errors: ["time"] }); const secretContent = contentCollector.first().content; // 发送服务器选择提示 await message.channel.send("Um.... theres two servers that this bot works on. 1: Loves in the snow or 2:🌙Moomins and Shep's Therapy Service🌙. Pick a number to send it to one of them"); // 等待用户选择投递服务器 const choiceCollector = await message.channel.awaitMessages({ filter: (msg) => msg.author.id === message.author.id && ["1", "2"].includes(msg.content.trim()), max: 1, time: 120_000, errors: ["time"] }); const selectedChoice = choiceCollector.first().content.trim(); const target = TARGET_CONFIG[selectedChoice]; // 异步拉取目标频道 const logChannel = await client.channels.fetch(target.logChannelId); const deliverChannel = await client.channels.fetch(target.deliverChannelId); // 发送日志(投递者信息) await logChannel.send( `Ok ok so who did it is ${message.author.tag} and their id is ${message.author.id}` ); // 匿名发送告白内容 await deliverChannel.send(secretContent); // 反馈投递结果给用户 await message.channel.send("✅ Your anonymous secret has been delivered successfully!"); } catch (error) { await message.channel.send("⌛ Operation timed out, please send `confess` again to restart the process."); } });
内容的提问来源于stack exchange,提问作者Meh Spencer
相关产品推荐
相关产品推荐

