Discord音乐机器人上的跳过功能删除所有数组条目

时间:2018-03-17 17:05:10

标签: javascript arrays function discord.js

如果没有惊慌失措并删除整个播放列表,我无法在音乐机器人上跳过曲目。更多细节如下。

根据命令,此机器人将使用YouTube-DL从YouTube下载视频或从BandCamp进行跟踪,并通过语音频道播放。下载轨道,加载到阵列中,它们一个接一个地播放。当赛道结束时,它会毫无问题地移动到下一个赛道。但是当涉及到手动跳过曲目时,它就像整个功能一样错误并重复运行,直到播放列表为空并且文件已被删除。

让我们开始的一些变数:

// References
var VC;
var TC;
var AS;
var DS;
var playlist = new Array();
var reqlist = new Array();
var volume = 100;
var fulltitle;

这是进入下一首曲目的功能:

function Next()
{
  if(!(DS===undefined)) DS.pause();

  if (playlist.length > 0)
  {
    // Delete the first audio file in the list (the one that just finished)
        var fileRemove = false
        try {
            fs.remove(playlist[0].file)
            console.log('File removed!');

            fileRemove = true
        } catch(e) {
            fileRemove = false
            console.err(e)
        }

        if (fileRemove = true) {
            playlist.splice(0, 1);
            console.log('Spliced!');
        } else {
            console.err('Could not splice or file not removed.')
        }

        //console.log(playlist)
        //fulltitle = playlist[0].info.fulltitle
        return StartPlay();
  }

    console.log('Length: ' + playlist.length)

    if(playlist.length === 0) {
        client.user.setPresence( { status: 'idle', game: { name: null } } )
    }// else {
    //   // Start playing
    //   return StartPlay();
    // }
}

以下是StartPlay()

function StartPlay() // Automatically plays queue once a song has been added successfully
{
  if (playlist.length >= 1) {
    // Play the audio file
    DS = AS.playFile(playlist[0].file);
    DS.passes = 12;
    DS.setVolume(volume/200.0);
    //fulltitle = playlist[0].info.fulltitle

        // Display song title in 'Game played'
    client.user.setPresence({ status: 'online', game: { type:2, name: fulltitle } });

        // Once the song is done
    DS.on('end', reason => {
      // Go to the next track
      Next();
    });
  }
}

以下是让它跳过的命令:

  case 'skip':
    if(VC===undefined) return;
    if(TC===undefined) return;
    if(!CheckTCVC(message)) return;
    if(playlist.length === 0) return message.channel.send('There is nothing to skip.');
    if(playlist.length === 1) return message.channel.send('There is nothing to skip to. Queue something else first or use `' + prefix + 'leave`.')

        if(message.content.length > 5) {
            const trackNumber = parseInt(message.content.split(' ')[1])

            if (trackNumber !== 1) {
                fs.remove(playlist[trackNumber-1].file).then(() => {
                    console.log('File removed!');
                    playlist.splice(0, trackNumber-1)
                    message.channel.send('Future song skipped!')
                }).catch(err => {
                    console.error(err)
                })
            } else {
                Next();
                message.channel.send('Current song skipped!')
            }
        } else {
            Next();
            message.channel.send('Song skipped!')
        }

        break;

我不确定代码中还有什么需要。请告诉我,我会将其粘贴。

这是Discord.JS文档的链接,而不是我认为这里真的需要:https://discord.js.org/#/docs/main/stable/general/welcome

提前致谢!

1 个答案:

答案 0 :(得分:0)

当您致电playlist.splice(0, trackNumber-1)时,我相信您正在使用拼接错误。根据{{​​3}},splice有两个参数:起始位置和要删除的元素数量。您已将0作为您的位置,并将trackNumber作为要移除的金额。因此,如果曲目编号为42,则您将从阵列中删除42个元素。尝试使用playlist.splice(trackNumber-1, 1)查看是否能提供所需的效果。