更新Discord Music Bot中嵌入的“正在播放”

时间:2020-10-20 11:24:55

标签: javascript node.js discord discord.js

我正在制作一个具有播放音乐主要功能的不和谐机器人。 我有一个正在播放命令,该命令可以向您显示歌曲中您走的距离,我希望每5秒钟左右进行一次更新。我知道编辑嵌入的知识,但是我需要使其不断循环播放,直到歌曲停止播放为止。现在是下面的代码:

const createBar = require("string-progressbar");
const { MessageEmbed } = require("discord.js");

module.exports = {
  name: "np",
  description: "Show now playing song",
  execute(message) {
    const queue = message.client.queue.get(message.guild.id);
    if (!queue) return message.reply(`Nothing's playing right now!`).catch(console.error);
    const song = queue.songs[0];
    const seek = (queue.connection.dispatcher.streamTime - queue.connection.dispatcher.pausedTime) / 1000;
    const left = song.duration - seek;

    let nowPlaying = new MessageEmbed()
      .setTitle("Now playing:")
      .setDescription(`${song.title}\n\`Requested by:\` ${message.author}`)
      .setColor("#ff0000")
      .setThumbnail('https://img.icons8.com/clouds/2x/play.png')
      .addField(
        "\u200b",
        new Date(seek * 1000).toISOString().substr(11, 8) +
          "[ " +
          createBar(song.duration == 0 ? seek : song.duration, seek, 10)[0] +
          "] " +
          (song.duration == 0 ? " ◉ LIVE" : new Date(song.duration * 1000).toISOString().substr(11, 8)),
        false
      );

    if (song.duration > 0)
      nowPlaying.setFooter("Time Remaining: " + new Date(left * 1000).toISOString().substr(11, 8));

    return message.channel.send(nowPlaying);
  }
};

1 个答案:

答案 0 :(得分:1)

您可以使用setInterval()定期编辑嵌入,然后使用clearInterval()停止编辑(一旦完成歌曲播放)。他们是这样工作的:

var countdown = 10;

// this is fine, except it doesn't stop at 0
setInterval(() => console.log(countdown--), 1000);

// we can use `clearInterval()` to stop the interval once it gets to 0
var countdown = 10;

const interval = setInterval(() => {
 console.log(countdown--);
 if (countdown < 0) clearInterval(interval);
}, 1000);