编辑嵌入 Discord Music Bot 的“正在播放”

Posted

技术标签:

【中文标题】编辑嵌入 Discord Music Bot 的“正在播放”【英文标题】:Editing 'Now Playing' embed in Discord Music Bot 【发布时间】:2021-06-26 21:22:30 【问题描述】:

我想将此嵌入编辑到滑块“????”的位置每 7 秒移动一次。我还希望“剩余时间:”每 7 秒更改一次。

目前所有的变化是“queue.formattedCurrentTime”和“song.formattedDuration”

这是我到目前为止的代码。有什么建议吗?

const  MessageEmbed  = require('discord.js-light')
const createBar = require('string-progressbar')
const  toColonNotation  = require('colon-notation')
module.exports = 
    name: 'np',
    aliases: ['nowplaying', 'currentsong'],
    description: 'Show the currently playing song.',
    usage: `$prefixnp`,
    run: async(bot, message) => 
        const queue = bot.distube.getQueue(message)
        if(!queue) return message.channel.send('There are no songs in the queue.')
        if(!queue && !bot.distube.isPlaying(message)) return('There is nothing playing.')
        const song = queue.songs[0]
        const name = song.name
        const user = song.user.tag
        const avatar = song.user.displayAvatarURL( dynamic: true, format: "png" )
        const link = song.url
        const time = song.duration * 1000
        const currenttime = queue.currentTime
        const tn = song.thumbnail
        const remaining = (time - currenttime < 0 ? "◉ LIVE" : time - currenttime)
        try 
            const embed = new MessageEmbed()
            .setTitle(name)
            .setAuthor(user, avatar)
            .setURL(link)
            .setDescription(`$createBar(time === 0 ? currenttime: time, currenttime, 27, "─", "????")[0] \`[$queue.formattedCurrentTime/$song.formattedDuration]\`\n` +
            `$bot.distube.isPaused(message) === true ? "⏸" : "▶" $time === 0 ? "" : `| **Time Remaining:**\`$toColonNotation(remaining)\```)
            .addField('In Channel:',  `**$message.member.voice.channel.name**`,  inline: true )
            .addField('From Playlist:', song.fromPlaylist ? "✅" : "❌",  inline: true )
            .setColor('#c1abff')
            .setThumbnail(`$tn`)
            message.channel.send(embed).then((message) => 
                var countdown = 10;
                const interval = setInterval(() => 
                    if (countdown < 0) clearInterval(interval);
                    const embed = new MessageEmbed()
                    .setTitle(name)
                    .setAuthor(user, avatar)
                    .setURL(link)
                    .setDescription(`$createBar(time === 0 ? currenttime: time, currenttime, 27, "─", "????")[0] \`[$queue.formattedCurrentTime/$song.formattedDuration]\`\n` +
                    `$bot.distube.isPaused(message) === true ? "⏸" : "▶" $time === 1 ? "" : `| **Time Remaining:**\`` + new Date()`$toColonNotation(remaining)\```)
                    .addField('In Channel:',  `**$message.member.voice.channel.name**`,  inline: true )
                    .addField('From Playlist:', song.fromPlaylist ? "✅" : "❌",  inline: true )
                    .setColor('#c1abff')
                    .setThumbnail(`$tn`)
                    message.edit(embed)
                , 7000)
            )
         catch (e) 
            message.channel.send(`There was an issue: \n\`$e\``)
        
    

【问题讨论】:

【参考方案1】:

不断更新嵌入可能不是解决此问题的理想方式。

相反,您可以尝试我在“正在播放”命令中的内容,并在您的嵌入中使用“时尚文本”。

const bar = require(`stylish-text`)

    function toReadableTime(given)
        var time = given;
        var minutes = "0" + Math.floor(time / 60);
        var seconds = "0" + (time - minutes * 60);
        return minutes.substr(-2) + ":" + seconds.substr(-2);
    

    const current = Math.floor(serverQueue.connection.dispatcher.streamTime / 1000) //ms --> seconds
    const end = serverQueue.songs[0].length //video in seconds
    
    const value = (current * (100 / end) / 5)
    
    bar.default.full = "█";
    bar.default.empty = " - ";
    bar.default.start = "";
    bar.default.end = "";
    bar.default.text = "bar";

`$toReadableTime(current) - [$bar.progress(20, value)] - $toReadableTime(end)` //This would go in your embed

输出:

【讨论】:

以上是关于编辑嵌入 Discord Music Bot 的“正在播放”的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 NPM 安装“discord-music-bot”

Discord Bot JS:编辑来自先前斜杠命令交互的回复

Music discord.js bot:“url”参数必须是字符串类型

使用 Discord.J 编辑嵌入字段

Bot 上线,但嵌入不会发布到 discord

如何使用 discord.py 使用我的不和谐机器人编辑嵌入颜色