Broadcast Dispatcher .resume() 函数 Discord JS v12 的问题

Posted

技术标签:

【中文标题】Broadcast Dispatcher .resume() 函数 Discord JS v12 的问题【英文标题】:Problem with Broadcast Dispatcher .resume() function Discord JS v12 【发布时间】:2021-04-30 15:43:21 【问题描述】:

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

    歌曲正在播放。

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

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

    歌曲无法继续播放,其他一切正常,!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 之前添加一个 .pause() () 和另一个额外的 .resume() 并按照相同的步骤,它的工作原理:

    正在播放歌曲。 !pause 被调用,歌曲暂停并显示暂停确认消息。 !resume 调用并显示恢复确认消息。 !resume 再次调用,歌曲继续播放,几乎没有任何声音故障,还在聊天中发送恢复确认消息。 !pause 被调用,歌曲暂停并发送暂停确认消息。 !resume 只调用了一次,歌曲继续播放,几乎没有任何声音故障,还在聊天中发送恢复确认消息。 从现在开始,这些命令可以正常工作。

这是修改后的!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% 的感谢您的帮助。

【问题讨论】:

您可以通过message.guild.me.voice.connection.dispatcher.resume() 试试运气。否则,您可以尝试npm i discordjs/discord.js 安装主版本(未经过 100% 测试)并检查您是否继续遇到此问题。 这些都没有帮助。我对此感到非常困惑。 很可能没有人真正知道这里发生了什么。您最好的选择是尽可能多地减少代码,并在删除所有非 discord.js 部分时查看是否仍有问题。 【参考方案1】:

适合所有寻求解决方案的人。 dispacter.resume() 在节点 v14.16.1+ 中有错误 所以如果你想让它工作使用节点

【讨论】:

【参考方案2】:

我也遇到了这个问题,可以使用 LTS 版本的 Node.js (14.15.5) 解决

【讨论】:

以上是关于Broadcast Dispatcher .resume() 函数 Discord JS v12 的问题的主要内容,如果未能解决你的问题,请参考以下文章

Android_Broadcast

Android学习总结——Broadcast

Ordered Broadcast有序广播

使用Broadcast实现android组件之间的通信

catch(Exception e) 有啥意思?

等待 Dispatcher.InvokeAsync 与 Dispatcher.Invoke