1 回答

TA貢獻(xiàn)1869條經(jīng)驗 獲得超4個贊
我通過在客戶端的就緒事件中創(chuàng)建一個函數(shù)來解決這個問題。
這是整個機器人的代碼(不包括登錄和消息事件)
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;
}
});
添加回答
舉報