【问题标题】:Updating 'Now Playing' embed in Discord Music Bot更新 Discord Music Bot 中嵌入的“正在播放”
【发布时间】:2021-02-03 05:20:44
【问题描述】:

我正在制作一个主要功能是播放音乐的不和谐机器人。 我有一个正在播放命令,它会显示你在歌曲中的进度,我想每 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);
  }
};

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案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);

    【讨论】:

    • 这很好,感谢您的回答 - 但是,我遇到了一个问题,它显示“无法编辑其他作者用户发送的消息”。你知道如何解决这个问题吗?它是由同一个用户发送的,所以我不明白为什么这个问题仍然存在。这是错误:(node:7420) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot edit a message authored by another user
    • 嗯,这真的很奇怪。您是否有多个客户端同时运行?
    • 我有一个客户端在服务器上运行,所以不 - 奇怪的是它不工作。我会看看它并尝试让它工作,但感谢您的帮助!
    猜你喜欢
    • 2021-06-26
    • 2020-09-19
    • 2021-10-29
    • 2020-08-02
    • 1970-01-01
    • 2021-11-07
    • 2021-08-12
    • 2022-10-06
    • 2019-02-27
    相关资源
    最近更新 更多