亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎樣才能使這個代碼循環?

我怎樣才能使這個代碼循環?

守候你守候我 2022-10-08 15:03:26
我正試圖讓它在我的 Discord 機器人中成功循環。我想要的是當命令運行時,機器人從中挑選一首歌曲1-10并播放它(已經完成),并且在播放完這首歌之后,它應該重復挑選一首歌曲并播放它。我不知道該怎么做。這是我的代碼:client.on('message', async message => {    var isReady = true;    if (!message.content.startsWith(prefix) || message.author.bot) return;    const args = message.content.slice(prefix.length).split(/ +/);    const command = args.shift().toLowerCase();    if (message.content === `${prefix}KOLARADIO`) {        if (message.member.voice.channel) {            const connection = await message.member.voice.channel.join();            songRandom = Math.floor((Math.random() * 10) + 1);            if (songRandom === 1) {                const dispatcher = connection.play('KOCK_MUSIC/a.mp3')                dispatcher.on('start', () => {                    console.log('audio ' + songRandom + ' is playing');                });                dispatcher.on('finish', () => {                    console.log('audio has finished playing');                })                dispatcher.on('error', console.error);            }            else if (songRandom === 2) {                const dispatcher = connection.play('KOCK_MUSIC/b.mp3')                dispatcher.on('start', () => {                    console.log('audio ' + songRandom + ' is playing');                });                dispatcher.on('finish', () => {                    console.log('audio has finished playing');                });                dispatcher.on('error', console.error);            }            else if ...            }            return songRandom;        };    };});
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

我有幾個建議,最后一個可以讓你永遠播放你的播放列表,但需要前面的兩個都起作用。


首先,您需要將您的歌曲放在一個數組中。這樣你就不需要重復自己來播放不同的歌曲。


const songs = [

   'KOCK_MUSIC/a.mp3',

    // ... more music

];

其次,您的歌曲選擇器應該使用 song.length 來選擇下一首歌曲,而不是依賴于您擁有 10 首歌曲。這樣您就可以在以后添加和刪除歌曲而不會破壞代碼。


最后,你應該有這樣的功能。


playSong() {

    const songId = Math.floor(Math.random() * songs.length);

    connection.play(songs[songId]).on("finish", playSong);

}

這將在上一首歌曲結束時遞歸播放一首新歌曲。小心,這應該永遠運行。你可能想給它一個停止的方法。


查看完整回答
反對 回復 2022-10-08
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號