首页 > 解决方案 > Broadcast Dispatcher .resume() 函数 Discord JS v12 的问题

问题描述

我正在用 Discord JS v12 编写音乐机器人,目前正在处理 !pause 和 !resume 命令。它们非常简单,代码没有错误。这就是发生的事情:

  1. 歌曲正在播放。

  2. !pause 调用和歌曲暂停,并显示暂停确认消息。

  3. !resume 调用并显示恢复确认消息。

  4. 歌曲不恢复,其他一切正常,!queue, !play 命令也是如此。

    这是我的!pause命令代码:

// Discord.js initialization.
const Discord = require("discord.js");

// Functions initialization.
const functions = require("../../functions.js");

// Initialization of the server specific variables data.
const serverData = require("../../serverData.json");

// The command's code goes inside an async function.
module.exports.run = async (bot, message, args, ops) => {

    /****************************/
    /****  MESSAGE DELETION  ****/
    /****************************/

    // Deletes the command invocation.
    await message.delete().catch(O_o => { });

    /************************/
    /****  CONDITIONALS  ****/
    /************************/

    // Fetches the guild object from the Map.
    let fetched = ops.active.get(message.guild.id);

    // Checks if there is any object in the fetched.
    if (!fetched) {

        // End of the function and message prints
        return message.channel.send(`> ${message.author}, there is no music playing or queued.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    // Checks if the message author is in the same voice channel as the bot.
    if (message.member.voice.channel !== message.guild.me.voice.channel) {

        // End of the function and message prints.
        return message.channel.send(`> ${message.author}, you must be in the same voice channel as the bot.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    // Then, check if the dispatcher is already paused.
    if(fetched.dispatcher.paused){

        // End of the function and message prints.
        return message.channel.send(`> ${message.author}, the song is already paused.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    /*****************************/
    /****  COMMAND EXECUTION  ****/
    /*****************************/

    // If nothing made the command exit, it executes the pause.
    fetched.dispatcher.pause(true);

    // Otherwise tell them in chat that they added a vote to skip.
    message.channel.send(`> ${message.member}, the current song has been paused.`).then(msg => msg.delete({ timeout: 4000 }));
}

// Command name.
module.exports.help = {
    name: "pause"
}

这是我的!resume命令代码:

// Discord.js initialization.
const Discord = require("discord.js");

// Functions initialization.
const functions = require("../../functions.js");

// Initialization of the server specific variables data.
const serverData = require("../../serverData.json");

// The command's code goes inside an async function.
module.exports.run = async (bot, message, args, ops) => {

    /****************************/
    /****  MESSAGE DELETION  ****/
    /****************************/

    // Deletes the command invocation.
    await message.delete().catch(O_o => { });

    /************************/
    /****  CONDITIONALS  ****/
    /************************/

    // Fetches the guild object from the Map.
    let fetched = ops.active.get(message.guild.id);

    // Checks if there is any object in the fetched.
    if (!fetched) {

        // End of the function and message prints
        return message.channel.send(`> ${message.author}, there is no music playing or queued.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    // Checks if the message author is in the same voice channel as the bot.
    if (message.member.voice.channel !== message.guild.me.voice.channel) {

        // End of the function and message prints.
        return message.channel.send(`> ${message.author}, you must be in the same voice channel as the bot.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    // Then, check if the dispatcher is already paused.
    if(!fetched.dispatcher.paused){

        // End of the function and message prints.
        return message.channel.send(`> ${message.author}, the song is not paused.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    /*****************************/
    /****  COMMAND EXECUTION  ****/
    /*****************************/

    // If nothing made the command exit, it executes the pause.
    fetched.dispatcher.resume(true);

    // Otherwise tell them in chat that they added a vote to skip.
    message.channel.send(`> ${message.member}, the current song has been resumed.`).then(msg => msg.delete({ timeout: 4000 }));
}

// Command name.
module.exports.help = {
    name: "resume"
}

我注意到的是,如果我修改resume命令并删除最后一个 if 语句,检查歌曲是否已经暂停,如果我还在 .resume() 和另一个额外的 .resume() 之前添加一个 .pause() 和按照相同的步骤,它的工作原理:

  1. 歌曲正在播放。
  2. !pause 调用和歌曲暂停,并显示暂停确认消息。
  3. !resume 调用并显示恢复确认消息。
  4. !resume 第二次调用,歌曲恢复,几乎没有任何声音故障,还在聊天中发送恢复确认消息。
  5. !pause 被调用,歌曲暂停并发送暂停确认消息。
  6. !resume 只调用了一次,歌曲恢复时几乎没有任何声音故障,还在聊天中发送恢复确认消息。
  7. 从现在开始,命令可以正常工作。

这是修改后的 !resume命令代码:

// Discord.js initialization.
const Discord = require("discord.js");

// Functions initialization.
const functions = require("../../functions.js");

// Initialization of the server specific variables data.
const serverData = require("../../serverData.json");

// The command's code goes inside an async function.
module.exports.run = async (bot, message, args, ops) => {

    /****************************/
    /****  MESSAGE DELETION  ****/
    /****************************/

    // Deletes the command invocation.
    await message.delete().catch(O_o => { });

    /************************/
    /****  CONDITIONALS  ****/
    /************************/

    // Fetches the guild object from the Map.
    let fetched = ops.active.get(message.guild.id);

    // Checks if there is any object in the fetched.
    if (!fetched) {

        // End of the function and message prints
        return message.channel.send(`> ${message.author}, there is no music playing or queued.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    // Checks if the message author is in the same voice channel as the bot.
    if (message.member.voice.channel !== message.guild.me.voice.channel) {

        // End of the function and message prints.
        return message.channel.send(`> ${message.author}, you must be in the same voice channel as the bot.`).then(msg => msg.delete({ timeout: 3000 }));
    }

    /*****************************/
    /****  COMMAND EXECUTION  ****/
    /*****************************/

    // If nothing made the command exit, it executes the pause.
    fetched.dispatcher.pause(true);
    fetched.dispatcher.resume();
    fetched.dispatcher.resume();
    

    // Otherwise tell them in chat that they added a vote to skip.
    message.channel.send(`> ${message.member}, the current song has been resumed.`).then(msg => msg.delete({ timeout: 4000 }));
}

// Command name.
module.exports.help = {
    name: "resume"
}

我知道这有点奇怪,也没有看到太多关于它的信息,但我见过很多人有同样的错误。我检查了fetched,它很好,所以我不知道问题是什么。伙计们,我将 110% 感谢您的帮助。

标签: javascriptnode.jsdiscorddiscord.js

解决方案


对于所有寻找解决方案的人。dispacter.resume() 在节点 v14.16.1+ 中有错误,所以如果你想让它工作,请使用节点 <=14.16.1。


推荐阅读