.setTimestamp不适用于正在通过循环编辑的嵌入

时间:2020-10-19 00:17:00

标签: discord.js

我有这个嵌入物:

var msgArr = [];
var embed = new Discord.MessageEmbed()
    .setTitle('Foo')
    .setDescription('Bar')
    .setTimestamp();

message.channel.send(embed).then(sentMessage => {
    msgArr.push(sentMessage);
});

然后对其进行编辑:

embed = new Discord.MessageEmbed()
    .setTitle('Bar')
    .setDescription('Foo')
    .setTimestamp();

msgArr[0].edit(embed);

此编辑过程在循环中重复多次:

var counter = 0;
const frequency = 30000;
setInterval(function () {
    if (counter++ % 2 == 0) {
        embed = new Discord.MessageEmbed()
            .setTitle('Bar')
            .setDescription('Foo')
            .setTimestamp();
        msgArr[0].edit(embed);
    } else {
        embed = new Discord.MessageEmbed()
            .setTitle('Foo')
            .setDescription('Bar')
            .setTimestamp();
        msgArr[0].edit(embed);
    }
}, frequency);

(基本上,标题和说明每30秒在'Foo''Bar'之间切换,其中包括.setTimestamp

现在,当我运行此命令时,发生的情况是.setTimestamp在第一分钟或第二分钟会正常工作,然后开始“滞后”实际时间。有人可以帮我找出原因吗?

完整代码(是的,有些部分被删去了,但是与API相关的部分对嵌入部分没有影响):

// Some code is commented out due to requests you have made, it should be obvious which code this is. I haven't cleaned up the commented out code, therefore be warned there will be some variables that aren't in this.

const Discord = require('discord.js');
// Below is normally not constant
const itemAmount = 6;
module.exports = {
    name: 'command',
    cooldown: 1,
    description: 'Test Description',
    execute(message) {
        function sendEmbeds(channel1, channel2) {
            message.client.channels.cache.get(channel1).bulkDelete(8);
            for (let i = 0; i < 1; i++) {
                let now = new Date();
                let embed = new Discord.MessageEmbed()
                    .setColor('#141414')
                    .setTitle(i + 1 + '. Item')
                    .setDescription(now.toString())
                    //  .setDescription('This is an item you can **buy** with `Bits` and then **sell** as a `BIN` for easy Skyblock Coins \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━')
                    .setThumbnail('some url')
                    .addFields(
                        { name: 'Information:', value: 'Buy Price: `1500 Bits`\n Sell Price: `250000 Coins` \n Coins per Bit Spent: `250 Coins`', inline: true },
                    )
                    .setTimestamp(now)
                    .setFooter('Powered by - SKY | Brokers', 'https://cdn.discordapp.com/icons/715801930877894706/a_95042b5d9aee771fdddec12191fb6092.webp?size=128');
                // Next if statement is api related, but since it's important to the embed it'll stay
                if (itemAmount >= 5) {
                    embed.setTitle(i + 1 + '. **HARD TO SELL**: Item');
                }
                message.client.channels.cache.get(channel1).send(embed).then(sentMessage => {
                    msgArr.push(sentMessage);
                });
            }
            message.client.channels.cache.get(channel2).bulkDelete(5);
            for (let i = 0; i < 1; i++) {
                let now = new Date();
                var craftableEmbed = new Discord.MessageEmbed()
                    .setColor('#141414')
                    .setTitle(i + 1 + '. Item')
                    .setDescription(now.toString())
                    //  .setDescription('This is an item you can craft by **buying** `' + craftables[craft[i][0]]["amountNeeded"] + ' ' + craftables[craft[i][0]]["itemNeeded"]["name"] + '` from the lowest BIN, buying `' + craftables[craft[i][0]]["bitsItem"] + '` with `Bits` and then **selling** as a `BIN` for easy Skyblock Coins \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━')
                    .setThumbnail('some thumbnail')
                    .addFields(
                        { name: 'Information:', value: 'Bits Buy Price: `1200 Bits` \n Item Buy Price: `500000 Coins` \n Sell Price Per Crafted Item: `600000 Coins` \n Profit Per Craft: `746889 Coins` \n Coins per Bit Spent: `153.6 Coins`', inline: true },
                    )
                    .setTimestamp(now)
                    .setFooter('Powered by - SKY | Brokers', 'https://cdn.discordapp.com/icons/715801930877894706/a_95042b5d9aee771fdddec12191fb6092.webp?size=128');
                message.client.channels.cache.get(channel2).send(craftableEmbed).then(sentMessage => {
                    msgArrCraft.push(sentMessage);
                });
            }
        }

        function editEmbeds() {
            for (let i = 0; i < 1; i++) {
                let now = new Date();
                console.log(now);
                let embedNew = new Discord.MessageEmbed()
                    .setColor('#141414')
                    .setTitle(i + 1 + '. Item')
                    .setDescription(now.toString())
                    //  .setDescription('This is an item you can **buy** with `Bits` and then **sell** as a `BIN` for easy Skyblock Coins \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━')
                    .setThumbnail('some thumbnail')
                    .addFields(
                        { name: 'Information:', value: 'Buy Price: `1500 Bits`\n Sell Price: `250000 Coins` \n Coins per Bit Spent: `250 Coins`', inline: true },
                    )
                    .setTimestamp(now)
                    .setFooter('Powered by - SKY | Brokers', 'https://cdn.discordapp.com/icons/715801930877894706/a_95042b5d9aee771fdddec12191fb6092.webp?size=128');
                if (itemAmount >= 5) {
                    embedNew.setTitle(i + 1 + '. **HARD TO SELL**: Item');
                }
                msgArr[i].edit(embedNew).then(newEmbed => {
                    msgArr[i] = newEmbed;
                    console.log(now, 'edited');
                });
            }
            for (let i = 0; i < 1; i++) {
                let now = new Date();
                console.log(now);
                let craftableEmbedNew = new Discord.MessageEmbed()
                    .setColor('#141414')
                    .setTitle(i + 1 + '. Item')
                    .setDescription(now.toString())
                    //  .setDescription('This is an item you can craft by **buying** `' + craftable[craft[i][0]]["amountNeeded"] + ' ' + craftable[craft[i][0]]["itemNeeded"]["name"] + '` from the lowest BIN, buying `' + craftable[craft[i][0]]["bitsItem"] + '` with `Bits` and then **selling** as a `BIN` for easy Skyblock Coins \n ━━━━━━━━━━━━━━━━━━━━━━━━━━━━')
                    .setThumbnail('some thumbnail')
                    .addFields(
                        { name: 'Information:', value: 'Bits Buy Price: `1200 Bits` \n Item Buy Price: `500000 Coins` \n Sell Price Per Crafted Item: `600000 Coins` \n Profit Per Craft: `746889 Coins` \n Coins per Bit Spent: `153.6 Coins`', inline: true },
                    )
                    .setTimestamp(now)
                    .setFooter('Powered by - SKY | Brokers', 'https://cdn.discordapp.com/icons/715801930877894706/a_95042b5d9aee771fdddec12191fb6092.webp?size=128');
                msgArrCraft[i].edit(craftableEmbedNew).then(newEmbed => {
                    msgArr[i] = newEmbed;
                    console.log(now, 'edited');
                });
            }
        }

        var msgArr = [];
        var msgArrCraft = [];
        setTimeout(() => {
            // Embed
            // The bot uses 2 channels, if you are testing this be warned that it is coded to delete 8 messages in the first channel, and 5 messages in the second. (Replace the channel IDs with the ones you are testing in)
            sendEmbeds('ChannelID1', 'ChannelID2');
        }, 15000);

        var counter = 0;
        const frequency = 30000;
        setInterval(function () {
            // Normally this if statement has API related code, this is omitted here therefore it seems useless.
            if (counter++ % 2 == 0) {
                editEmbeds();
            }
            else {
                editEmbeds();
            }
        }, frequency);
    },
};

如果您想获取完整的完整代码(包括大量与API相关的内容),请随时询问。

0 个答案:

没有答案