我正试图让它在我的 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);
}
这将在上一首歌曲结束时递归播放一首新歌曲。小心,这应该永远运行。你可能想给它一个停止的方法。
添加回答
举报
0/150
	提交
		取消
	