【问题标题】:Setting a maximum size for a music queue设置音乐队列的最大大小
【发布时间】:2019-04-02 19:54:06
【问题描述】:

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

【问题讨论】:

  • 嗨,欢迎来到 SO。您的问题太宽泛且难以理解:您能否向我们展示您的代码的相关部分、您之前的尝试和期望的行为?看看这里 ;) How do I ask a good question?
  • 我添加了代码
  • 我编辑了这个问题。我的语法不好。英语不是我的母语。
  • 不用担心,每个人都尽力了 :) 您是希望音乐队列限制为 10 首歌曲,还是只将嵌入显示放在前 10 首,其余的留在队列中以供稍后使用?
  • 我希望它显示前 10 首歌曲,然后再显示其余歌曲。

标签: discord.js


【解决方案1】:

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

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

【讨论】:

  • 让 ytLink = ytBaseUrl + song.id;歌曲未定义
  • 糟糕,忘记声明song
  • 哦,那是因为你的代码中有i++,再次修复它。如果仍然显示相同的歌曲,请尝试通过console.loggin' 检查队列(q)的情况。
  • image.ibb.co/iqDasf/capture.png 现在是这样。我想让它像 01、02、03... 并继续这样下去。
  • 我添加了相同的歌曲。 :)
猜你喜欢
  • 1970-01-01
  • 2019-10-14
  • 2010-11-24
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
相关资源
最近更新 更多