【问题标题】:Discord.js: (node:4976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecatedDiscord.js:(节点:4976)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝
【发布时间】:2021-10-21 10:47:04
【问题描述】:

您将在下面找到我的代码,然后是我收到的错误消息。我不确定为什么会收到此错误或如何解决此问题。

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 = "!";

client.on("ready", () => {
    console.log(`${client.user.tag} Está Online`);
});

client.on("message", async (message) => {
    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();

    // Queue status template
    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"}\``;

    // DisTube event listeners, more in the documentation page
    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)}`))
        // DisTubeOptions.searchSongs = true
        .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*`);
        })
        // DisTubeOptions.searchSongs = true
        .on("searchCancel", (message) => message.channel.send(`Searching canceled`))
        .on("error", (message, e) => {
            console.error(e);
            message.channel.send("An error encountered: " + e);
        });
    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.stop(message);
        message.channel.send("Você Parou A Musica");
    }
});

client.login(process.env.TOKEN);

结果是以下错误信息:

**(node:4976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:4976) UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable.
(node:4976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)**

谁能给我解释一下:

  1. 为什么我会收到此错误消息?
  2. 如何开始正确调试?
  3. 如果有快速示例/解决方案。

【问题讨论】:

  • 不要把你的整个程序都扔到问题中,看看如何创建Minimal, Reproducible Example
  • 对于错误,使用=====进行比较,而不是=,一个等号是赋值。

标签: javascript node.js discord discord.js


【解决方案1】:

正如 Skulaurun 在 cmets 中所说,您需要使用 ===== 进行比较,而不是使用赋值运算符 =

您的代码中的问题在于以下几行:

if ((command = "play")) { ... }

由于您只使用了一个=,因此节点会将其解释为您要将"play" 的值分配给command,这是一个常量且无法更改,因此会出现错误。您想使用===== 进行比较,例如:

if (command === "play") { ... }

if ((command = "pause")) 也有同样的问题。

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 2019-10-13
    • 2018-09-14
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多