Discord.js v12升v13后DM申请机器人首答后无响应问题
问题现象
基于discord.js开发的私信申请流程机器人,在v12版本下运行正常,升级到v13版本后出现流程卡死问题:用户输入第一个申请问题的答案后,机器人不再回复,无法推进后续申请步骤。
原故障代码如下:
let userApplications = {} client.on('messageCreate', async function (message) { if (message.author.equals(client.user)) return; let emojii = "<a:emoji_66:840679078951911475>" let emoji = "<a:greencheck:714998866952323122>" let wemoji = "<a:pp62:730039453380378674>" let authorId = message.author.id; if (message.content === "-تسجيل") { message.reply("head to dm"); if (!(authorId in userApplications)) { userApplications[authorId] = { "step": 1 } message.author.send("whats your name?"); } } else { if (message.channel.type === "dm" && authorId in userApplications) { let authorApplication = userApplications[authorId]; if (authorApplication.step == 1) { authorApplication.answer1 = message.content; message.author.send("age?"); authorApplication.step++; } else if (authorApplication.step == 2) { authorApplication.answer2 = message.content; message.author.send("birth date"); authorApplication.step++; } else if (authorApplication.step == 3) { authorApplication.answer3 = message.content; message.author.send("birth place"); authorApplication.step++; } else if (authorApplication.step == 4) { authorApplication.answer4 = message.content; message.reply("done"); delete userApplications[authorId]; } } } });
故障根因
discord.js v13 对频道类型枚举值做了不兼容变更:
- v12版本中,私信频道的
type属性值为小写字符串"dm" - v13版本中,私信频道的
type属性值改为大写字符串"DM"
原代码中message.channel.type === "dm"的判断逻辑在v13环境下永远返回false,用户发送的第一条之后的私信回复无法进入申请流程处理分支,直接导致流程卡死。
另外v13版本强制要求声明intent,客户端初始化时必须开启DIRECT_MESSAGESintent,否则机器人无法接收私信消息,同样会导致私信场景无响应。
修复方案
需要做两处核心调整:
- 将DM频道类型判断的匹配值从
"dm"改为"DM",更稳妥的方式是引入discord.js导出的ChannelType枚举,用ChannelType.DM做判断,避免后续版本变更再次出现兼容问题 - 客户端初始化时补充开启
DIRECT_MESSAGES和GUILD_MESSAGES相关intent,保证机器人能正常接收服务器频道和私信频道的消息
修复后的完整可运行代码:
const { Client, Intents, ChannelType } = require('discord.js'); // 初始化客户端时声明所需intents const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES // 必须开启才能接收私信消息 ] }); let userApplications = {} client.on('messageCreate', async function (message) { if (message.author.equals(client.user)) return; let emojii = "<a:emoji_66:840679078951911475>" let emoji = "<a:greencheck:714998866952323122>" let wemoji = "<a:pp62:730039453380378674>" let authorId = message.author.id; if (message.content === "-تسجيل") { message.reply("head to dm"); if (!(authorId in userApplications)) { userApplications[authorId] = { "step": 1 } message.author.send("whats your name?"); } } else { // 修正v13版本的DM频道类型判断 if (message.channel.type === ChannelType.DM && authorId in userApplications) { let authorApplication = userApplications[authorId]; if (authorApplication.step == 1) { authorApplication.answer1 = message.content; message.author.send("age?"); authorApplication.step++; } else if (authorApplication.step == 2) { authorApplication.answer2 = message.content; message.author.send("birth date"); authorApplication.step++; } else if (authorApplication.step == 3) { authorApplication.answer3 = message.content; message.author.send("birth place"); authorApplication.step++; } else if (authorApplication.step == 4) { authorApplication.answer4 = message.content; // 私信场景直接发消息即可,不需要reply触发@ message.author.send("done"); delete userApplications[authorId]; } } } }); client.login('你的机器人Token');
内容的提问来源于stack exchange,提问作者Expensive Salt
相关产品推荐
相关产品推荐

