Discord.js报DEP0018弃用警告及常量赋值错误问题求助
1 报错触发原因
- 核心语法错误:你用
const声明了command变量,后续条件判断中错误使用赋值运算符=而非相等运算符===,试图给const常量重新赋值,直接触发TypeError: Assignment to constant variable报错。 - 异步错误未捕获:上述错误抛出在
async修饰的message事件回调函数中,你没有添加try/catch块捕获异常,导致错误变成未处理的Promise拒绝,触发后续的弃用警告。 - 额外逻辑隐患:即便你把
const改成let,单等号的赋值逻辑会让条件永远返回true,所有命令都会同时触发播放、暂停两段逻辑,功能完全无法正常运行。
2 调试思路与步骤
- 优先读取报错核心信息:看到
TypeError: Assignment to constant variable直接定位代码中所有const声明的变量,排查是否有重新赋值的操作。 - 校验条件判断语法:区分赋值运算符
=、宽松相等==、严格相等===的使用场景,所有判断变量值的条件都应该用===。 - 补全异步错误捕获:所有
async函数内部的逻辑都用try/catch包裹,避免未处理的Promise拒绝导致进程退出。 - 逻辑验证:修复语法问题后,分别测试不同命令的执行结果,确认逻辑符合预期。
3 快速修复示例
关键代码修改点:
- 把条件判断的
=改为=== - 将DisTube的事件监听移到
ready事件中,避免每次收到消息重复绑定监听 - 给异步逻辑加
try/catch块 - 修复暂停逻辑中方法调用错误的问题
修改后的完整代码片段:
const Discord = require("discord.js"); const client = new Discord.Client(); const DisTube = require("distube"); const distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: true }); const prefix = "!"; // 队列状态模板抽离到全局,避免重复声明 const status = (queue) => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? (queue.repeatMode == 2 ? "All Queue" : "This Song") : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``; client.on("ready", () => { console.log(`${client.user.tag} Está Online`); // DisTube事件监听只需要在启动时绑定一次 distube .on("playSong", (message, queue, song) => message.channel.send(`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`)) .on("addSong", (message, queue, song) => message.channel.send(`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`)) .on("playList", (message, queue, playlist, song) => message.channel.send(`Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`) ) .on("addList", (message, queue, playlist) => message.channel.send(`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`)) .on("searchResult", (message, result) => { let i = 0; message.channel.send(`**Choose an option from below**\n${result.map((song) => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`); }) .on("searchCancel", (message) => message.channel.send(`Searching canceled`)) .on("error", (message, e) => { console.error(e); message.channel.send("An error encountered: " + e); }); }); client.on("message", async (message) => { try { if (message.author.bot) return; if (!message.content.startsWith(prefix)) return; const args = message.content.slice(prefix.length).trim().split(/ +/g); const command = args.shift(); if (command === "play") { if (!args[0]) return message.channel.send("Voce Precisa Falar Algo Para Eu Tocar"); if (!message.member.voice.channel) return message.channel.send("Entre Em Um Canal De Voz Primeiro"); distube.play(message, args.join(" ")); } if (command === "pause") { const bot = message.guild.members.cache.get(client.user.id); if (!message.member.voice.channel) return message.channel.send("Entre Em Um Canal De Voz Primeiro"); if (bot.voice.channel !== message.member.voice.channel) return message.channel.send("Você Não Está No Mesmo Canal De Voz!"); distube.pause(message); message.channel.send("Você Parou A Musica"); } } catch (e) { console.error(e); message.channel.send("执行命令出错:" + e.message); } }); client.login(process.env.TOKEN);
内容的提问来源于stack exchange,提问作者Joao Pedro Turra
相关产品推荐
相关产品推荐

