设置音乐队列的最大大小

时间:2018-10-29 20:04:40

标签: discord.js

我想使队列列表一次只显示10首歌曲,因为现在该机器人崩溃了,并说每个嵌入字段只能包含1024个字符。

我将最重要的部分放在下面,您可以找到其余的here

exports.playQueue = (guildId, channel) => {
  if (!guilds[guildId] || !guilds[guildId].nowPlaying) {
    var embed = new Discord.RichEmbed()
      .setColor(9955331)
      .setDescription(":mute: Not Playing");
    channel.send(embed);
    return;
  }

  var g = guilds[guildId];
  var q = "";
  var i = 1;
  let ytBaseUrl = "https://www.youtube.com/watch?v=";
  g.playQueue.forEach((song) => {
    let ytLink = ytBaseUrl + song.id;
    let title = song.title;
    if (title.length > 30) title = title.substring(0, 19) + "... ";
    q += "`" + i++ + "`. ";
    q += `[${title}](${ytLink}) | `;
    q += "`" + song.length + "`\n";
  });

  let currSong = g.nowPlaying.title;
  if (currSong.length > 30) currSong = currSong.substring(0, 19) + "... ";
  var cs = `[${currSong}](${ytBaseUrl+g.nowPlaying.id}) | `;
  cs += "`" + g.nowPlaying.length + "`";

  var embed = new Discord.RichEmbed()
    .setColor(9955331)
    .addField(":musical_note: Now Playing", cs);
  if (g.loop) embed.setFooter(" Looping playlist");
  if (q != "") embed.addField(":notes: Play Queue", q);

  channel.send(embed);
}

1 个答案:

答案 0 :(得分:0)

我将通过建立从第一首到第十首歌曲的队列来解决问题

// I chose a for loop just because I can break out of it
// This goes on until it reaches the end of the queue or the tenth song, whichever comes first
for (let i = 0; i < g.playQueue.length && i < 9; i++) {
  let song = g.playQueue[i];
  // This is your part
  let ytLink = ytBaseUrl + song.id;
  let title = song.title;
  if (title.length > 30) title = title.substring(0, 19) + "... ";

  // Instead of directly adding the next line to q, I store it in another variable
  let newline = "";
  newline  += "`" + (i+1) + "`. ";
  newline  += `[${title}](${ytLink}) | `;
  newline  += "`" + song.length + "`\n";

  // If the sum of the two lengths doesn't exceed the 1024 chars limit, I add them together
  // If it is too long, I don't add it and exit the loop
  if (q.length + newline.length > 1024) break;
  else q += newline;
}

这应该可以解决,因为q不能超过1024个字符。我认为这是一个简单但有效的解决方案。
随时让我知道是否出现某些问题;)