简体   繁体   English

我怎样才能使这个代码循环? [discord.js]

[英]How can I make this code loop? [discord.js]

I'm trying to get this to successfully loop in my Discord bot.我试图让它在我的 Discord 机器人中成功循环。 What I want is when the command runs, the bot picks a song from 1-10 and plays it (already done), and after that song is played it should repeat picking a song and playing it.我想要的是当命令运行时,机器人从1-10中挑选一首歌曲并播放(已经完成),并且在播放完这首歌之后,它应该重复挑选一首歌曲并播放它。

I'm not sure how to do this.我不知道该怎么做。

This is my code:这是我的代码:

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;
        };
    };
});

I have a couple suggestions, the last of which will allow you to play your playlist forever, but requires both of the preceding ones to work.我有几个建议,最后一个可以让你永远播放你的播放列表,但需要前面的两个都起作用。

First, you need to put your songs in an array.首先,您需要将您的歌曲放在一个数组中。 That way you don't need to repeat yourself to play different songs.这样你就不需要重复自己来播放不同的歌曲。

const songs = [
   'KOCK_MUSIC/a.mp3',
    // ... more music
];

Second your song picker should use songs.length to pick the next song, instead of relying on you having 10 songs.其次,您的歌曲选择器应该使用 song.length 来选择下一首歌曲,而不是依赖于您拥有 10 首歌曲。 This way you can add and remove songs later without your code breaking.这样您就可以在以后添加和删除歌曲而不会破坏代码。

Finally, you should have a function like this.最后,你应该有一个像这样的 function。

playSong() {
    const songId = Math.floor(Math.random() * songs.length);
    connection.play(songs[songId]).on("finish", playSong);
}

This will recursively play a new song when the previous song finishes.这将在上一首歌曲结束时递归播放一首新歌曲。 Be careful, this should run forever.小心,这应该永远运行。 You may want to give it a way to stop.你可能想给它一个停止的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM