BroadcastDispatcher 完成事件仅调用一次 (discord.js)

BroadcastDispatcher finish event only called once (discord.js)

我正在尝试为 Discord 制作 LoFi 收音机。每当机器人上线时,它都会加入服务器上每个名为 lofi 的频道(这是非常糟糕的编码,如果我知道一台服务器中有两个以上的 lofi 频道,它将无法工作,稍后会修复。)我遇到的问题是广播调度程序的完成事件只被调用一次。我希望它在每次最后一首停止时播放一首新的随机歌曲,但它只在上线时播放一首,在那首歌之后播放一首,然后调度程序设置为 null 这让我感到困惑。

代码:

var fs = require('fs');
var client = new Client();
var files = fs.readdirSync("./mp3/");

var broadcast;
var curSong;
var dispatcher;

client.on("ready", () => {
    console.log(`------------${client.user.tag}------------\n`);

    curSong = `./mp3/${files[Math.floor(Math.random() * files.length)]}`;
    broadcast = client.voice.createBroadcast();
    broadcast.play(curSong);
    broadcast.dispatcher.on("finish", () => {
        curSong = `./mp3/${files[Math.floor(Math.random() * files.length)]}`;
        broadcast.play(curSong);
        client.user.setPresence({ activity: { name: curSong.split("/")[2].replace(".mp3", ""), type: "STREAMING", url: "https://twitch.tv/TwitchChannel" }, status: "Online" });
    });

    client.user.setPresence({ activity: { name: curSong.split("/")[2].replace(".mp3", ""), type: "STREAMING", url: "https://twitch.tv/TwitchChannel" }, status: "Online" });

    client.channels.cache.array().forEach((chnl) => {
        if(chnl.name.toLowerCase() == "lofi") {
            chnl.join().then(connection => {
                connection.play(broadcast);
            });
        }
    });
});

我通过在客户端的就绪事件中创建一个函数来解决这个问题。

这是整个机器人的代码(不包括登录和消息事件)

var fs = require('fs');
var client = new Client();
var files = fs.readdirSync("./mp3/");

var broadcast;
var curSong;
var dispatcher;

client.on("ready", () => {
    console.log(`------------${client.user.tag}------------\n`);

    curSong = `./mp3/${files[Math.floor(Math.random() * files.length)]}`;
    broadcast = client.voice.createBroadcast();
    broadcast.play(curSong);
    dispatcherLol();

    client.user.setPresence({ activity: { name: curSong.split("/")[2].replace(".mp3", ""), type: "STREAMING", url: "https://twitch.tv/AtlasDevelopment" }, status: "Online" });

    client.channels.cache.array().forEach((chnl) => {
        if(chnl.name.toLowerCase() == "lofi") {
            chnl.join().then(connection => {
                connection.play(broadcast);
            });
        }
    });

    function dispatcherLol() {
        broadcast.dispatcher.on("finish", () => {
            curSong = `./mp3/${files[Math.floor(Math.random() * files.length)]}`;
            broadcast.play(curSong);
            client.user.setPresence({ activity: { name: curSong.split("/")[2].replace(".mp3", ""), type: "STREAMING", url: "https://twitch.tv/AtlasDevelopment" }, status: "Online" });
            dispatcherLol();
        });
        return;
    }
});