设置音乐队列的最大大小

Setting a maximum size for a music queue

我想让队列列表一次只显示 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);
}

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

// 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 个字符。在我看来,这是一个简单但有效的解决方案。
如果有什么不能正常工作,请随时告诉我 ;)