Mathias AC
Mathias AC

Reputation: 17

how would i get embed messages from a specific channel? [Discord.js V12]

I am trying to get a number from an embed field value from all the embeds in a specific channel and combine those numbers into one big number

Currently fetching messages from a specific channel like this

async function fetchMessagesUntil(channel, endDate, lastID) {
        let messages = [];
        try {
            messages = (await channel.messages.fetch({ limit: 100, before: lastID })).array();
            for (let i = 0; i < messages.length; i++) {
                if (messages[i].createdAt.getTime() < endDate.getTime()) {
                    return messages.slice(0, i);
                }
            }
            return messages.concat(await fetchMessagesUntil(channel, endDate, messages[messages.length - 1].id));
        } catch {
            return messages;
        }
    }

    let end = new Date();
    end.setDate(end.getDate() - 2); // Subtract two days from now
    (await fetchMessagesUntil(channel, end)).forEach(x => console.log(x.content));
    message.delete()

but embeds are empty when logging it to console

any ideas?

Upvotes: 1

Views: 271

Answers (1)

Skulaurun Mrusal
Skulaurun Mrusal

Reputation: 2847

You can look here at the docs. The Message class has many properties. Message.content can be empty, but the message object may contain other properties like Message.embeds, Message.attachments, etc.

So in your code you would access the embeds like this:

let result = 0;
(await fetchMessagesUntil(channel, end)).forEach((message) => {
    message.embeds.forEach((embed) => {
        console.log(embed);
        // result += ...
    });
});

Also take a look at MessageEmbed at the docs. There you can see all the methods and properties it has, access them and get what you want from the embeds.

When getting a value from embed's field, I would check if it is the field you want to extract a value from, for example by checking its name. If you know that it is the wanted field, you can get the value from there. First use !isNaN(fieldValue), so you know that the field value is a number. Then I would use parseInt(fieldValue) to parse the string into an integer. And finally using the += operator, add the field's numeric value to the final result variable.

Upvotes: 1

Related Questions