如何停止播放机器人音乐? (Discord.js)

时间:2020-07-26 17:37:37

标签: discord bots discord.js

此代码用于播放音乐,我想在我的机器人上发出停止命令。你能帮我吗?

const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json")
const ytdl = require("ytdl-core")
const streamOptions = {seek:0, volume:1}

client.on("message", async message => {

    if(message.author.bot) return
    if(message.channel.type === "dm") return
    if(!message.content.startsWith(config.prefix)) return

    const args = message.content.slice(config.prefix.length).trim().split(/ +/g)
    const comando = args.shift().toLocaleLowerCase()

    if(comando === "play") {
        var voiceChannel = message.guild.channels.cache.get("733122593019789314")
        let file = args[0]

        if(voiceChannel == null) {
            console.log("Canal não encontrado.")
        }

        if(voiceChannel != null) {
            console.log("Canal encontrado.")

            await voiceChannel.join().then(connection => {
                const stream = ytdl(file, {filter:"audioonly", quality:"highestaudio"})
                const DJ = connection.play(stream, streamOptions)

                DJ.on("end", end => {
                    voiceChannel.leave()
                })
            }).catch(console.error)
        }

    }/* I was trying to do the command this way:
      else if(comando === "stop") {
        voiceChannel.leave()
    }*/
}

client.login(config.token);

一切正常,我只想停止播放音乐。

(我是巴西人,某些单词或句子可能是错误的。)

谢谢您的帮助! ?

1 个答案:

答案 0 :(得分:1)

您可以使用StreamDispatcherpause()和StreamDispatcherresume()。

您的StreamDispatcher被定义为DJ,因此您可以使用:

DJ.pause(); // Pauses the stream.

DJ.resume(); // Resumes the stream.

DJ.destroy(); // Ends the stream.