【问题标题】:Cannot push a second URL into the queue for my Discord bot无法将第二个 URL 推送到我的 Discord 机器人的队列中
【发布时间】:2020-10-24 20:22:15
【问题描述】:

我有适当的播放和停止功能。但是,问题是我无法将第二个 URL 添加到队列中。

如果我调用 -play URL,此命令将停止当前歌曲并跳到新歌曲。另外,如果一首歌曲播放完毕,我必须断开连接才能播放另一首歌曲,所以我也无法实现跳过功能。

我使用的是新版本 Discord.js,它是 v.12。

case "play":

function play(connection, msg) {
    var server = servers[msg.guild.id]

    server.dispatcher = connection.play(ytdl(server.queue[0], {
        filter: "audioonly"
    }))

    server.queue.shift()

    server.dispatcher.on("end", function() {
        //dispatcher = null
        if (server.queue[0]) {
            play(connection, msg)
        } else {
            connection.disconnect()
        }
    })

}

if (!arguments[0]) {
    msg.channel.send("You need to provide a link!")
    return
}

console.log(msg.member.voice.channel)
if (!msg.member.voice.channel) {
    msg.channel.send("You must first join a voice channel!")
    return
}

if (!servers[msg.guild.id])
    servers[msg.guild.id] = {
        queue: []
    }

var server = servers[msg.guild.id]

server.queue.push(arguments[0])

if (!msg.guild.voiceConnection)
    msg.member.voice.channel.join().then(function(connection) {
        play(connection, msg)
    })


break

case "skip":
var server = servers[msg.guild.id]
if (server.dispatcher)
    server.dispatcher.end();

msg.channel.send("skip to the next one")
break

case "stop":
var server = servers[msg.guild.id]
if (msg.guild.voice.connection) {
    for (var i = server.queue.length - 1; i >= 0; i--) {
        server.queue.splice(i, 1)
    }

    server.dispatcher.end();
    msg.channel.send("Stop playing")

}

if (msg.guild.voice.connection)
    msg.guild.voice.connection.disconnect()
break

提前谢谢你!

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    guild.voiceConnection 未定义,将其替换为 guild.voice。但更好的解决方案是在播放新歌之前在结束事件中移动队列并测试队列是否为空。

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 2020-12-08
      • 2018-07-22
      • 2021-05-24
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多