CupcakeDaLearner
CupcakeDaLearner

Reputation: 13

How to fetch a message from the entire guild

I would like to know how to find a message with its id from the entire guild and not just the current channel.

I have tried

I want it to look through the entire guild and find the message by ID then send what the message has said. Also, I have tried to have it look for the channel that I want the message from but that didn't work either.

If it's possible to get more specific with the code, it should look into a channel called #qotd-suggestions with the ID of 479658126858256406 and then find the message there and then send the message's content.

Also, the message_ID part of fetchMessages gets replaced with what I say after the command so if I say !send 485088208053469204 it should send Message content: "Wow, the admins here are so lame."

Upvotes: 1

Views: 4784

Answers (1)

Federico Grandi
Federico Grandi

Reputation: 6796

There's no way to do that with a Guild method, but you can simply loop through the channels and call TextChannel.fetchMessage() for each of them.
In order to do that, you could use Collection.forEach() but that would be a little more complex if you want to work with vars, so I suggest you to convert Guild.channels to an Array with Collection.array().

Here's an example:

async function findMessage(message, ID) {
  let channels = message.guild.channels.filter(c => c.type == 'text').array();
  for (let current of channels) {
    let target = await current.fetchMessage(ID);
    if (target) return target;
  }
}

You can then use this function by passing the message that triggered the command (from which you get the guild) and the ID you're looking for. It returns a Promise<Message> or, if the message is not found, Promise<undefined>. You can get the message by either using await or Promise.then():

let m = await findMessage(message, message_ID); // Message or undefined
// OR
findMessage(message, message_ID).then(m => {/* your stuff */});

Upvotes: 3

Related Questions