简体   繁体   中英

Problem with Broadcast Dispatcher .resume() function Discord JS v12

I am coding a music bot in Discord JS v12 and am currently working on the.pause and,resume commands. They're quite simple: and the code has no errors. This is what happens:

  1. Song is playing.

  2. .pause invoked and song pause and the pause confirmationn message is displayed.

  3. .resume invoked and the resume confirmation message is displayed.

  4. Song does not resume, everything else works perfectly, ,queue. !play commands as well.

    This is my !pause command code:

// 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"
}

This is my !resume command code:

// 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"
}

What I have noticed is that if I modify the resume command and remove the last if sentence checking if the song is already paused, and if I also add a.pause() before the.resume() and another extra.resume() and follow the same steps, it works:

  1. Song is playing.
  2. .pause invoked and song pause and the pause confirmationn message is displayed.
  3. .resume invoked and the resume confirmation message is displayed.
  4. ,resume invoked a second time and the song resumes with barely any sound glitches. also sends a resume confirmation message on the chat.
  5. .pause invoked and song pauses and sends the pause confirmation message.
  6. ,resume invoked only one time and the song resumes with barely any sound glitches. also sends a resume confirmation message on the chat.
  7. From now on the commands work okay.

This is the modified !resume command code:

// 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"
}

I know this is a bit weird and haven't seen much info on it but I have seen many people that have the same error. I checked fetched and it's good, so I don't know what the problem is. I would 110% appreciate your help here, guys.

I had this problem too, it can be fixed by using the LTS version of Node.js (14.15.5)

For everyone looking for solution. dispacter.resume() is buggy in node v14.16.1+ So if you want it to work use node <=14.16.1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM