【问题标题】:How do I stop playing bot music? (Discord.js)如何停止播放机器人音乐? (不和谐.js)
【发布时间】:2020-11-16 02:08:38
【问题描述】:

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

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);

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

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

感谢您的帮助! ????

【问题讨论】:

    标签: discord bots discord.js


    【解决方案1】:

    您可以使用StreamDispatcher.pause() 和StreamDispatcher.resume()。

    你的StreamDispatcher被定义为DJ,所以你可以使用:

    DJ.pause(); // Pauses the stream.
    
    DJ.resume(); // Resumes the stream.
    
    DJ.destroy(); // Ends the stream.
    

    【讨论】:

    • 好的,我尝试这样做,但返回一个错误,提示“DJ 未定义”。我试图将“const”更改为“var”,但它不起作用。
    • 尝试在代码开头定义 DJ,如下所示:let DJ;,并将 const DJ = connection.play(stream, streamOptions) 更改为 DJ = connection.play(stream, streamOptions)
    • 它没有用。我更改了代码,但它仍然给我一个错误...感谢您的帮助。我要去 discord.js 文档寻找答案。我只是想使用 voiceChacnnel.leave() 。我试试看告诉你。
    • 成功了!!!!我只是验证命令是播放还是停止。如果是停止,这段代码运行:await voiceChannel.leave() 很简单。谢谢!!! ?
    猜你喜欢
    • 2021-11-24
    • 2022-01-21
    • 2019-08-03
    • 2019-06-27
    • 2019-04-04
    • 2021-05-05
    • 2020-12-19
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多