0
votes

Using Discord.js in an Express/Node.js app, I'm trying to build a bot that grabs external data periodically and updates Discord with an embed msg containing some elements of that data. I'm trying to add a feature that will check if that data was deleted from the external source(no longer existing upon the next grab), then delete the specific msg in Discord that contains that data that was sent.

Some of the msgs posted in Discord may have duplicate data items, so I want to delete by specific msg ID, but it seems that msg ID is assigned when posted to Discord.

Is there a way to programmatically grab or return this msg ID when sending from Discord.js, rather than manually copy/pasting the msg ID from the Discord GUI? In other words, I need my bot to know which message to delete if it sees that msg's source data is no longer being grabbed.

// FOR-LOOP TO POST DATA TO DISCORD
// see if those IDs are found in persistent array
        for (var i = 0; i < newIDs.length; i++) {
            if (currentIDs.indexOf(newIDs[i]) == -1) {
                currentIDs.push(newIDs[i]); // add to persistent array
                TD.getTicket(33, newIDs[i]) // get ticket object
                .then(ticket => { postDiscord(ticket); }) // post to DISCORD!
            }
        }

        // if an ID in the persistent array is not in temp array,
        // delete from persistent array & existing DISCORD msg.
        // message.delete() - need message ID to get msg object...
        // var msg = channel.fetchMessage(messageID) ?
1

1 Answers

0
votes

Let me refer you to:

https://discord.js.org/#/docs/main/stable/class/Message

Assuming you are using async/await, you would have something like:

async () => {
 let message = await channel.send(some message);
 //now you can grab the ID from it like
 console.log(message.id)
}

If you are going to use .then for promises, it is the same idea:

channel.send(some message)
    .then(message => {
     console.log(message.id)
     });

ID is a property of messages, and you will only get the ID after you receive a response from the Discord API. This means you have to handle them asynchronously.