Somna
Somna

Reputation: 28

Discord.js: Fetch message from ALL channels

message.channel.messages.fetch does fine at fetching from the channel the command is run in, but I need to be able to fetch messages from ANY channel in the server. For reference, I'm making a command to quote a message via message ID, but as of right now it can only quote messages from the same channel that the command is run in.

Upvotes: 0

Views: 13171

Answers (2)

user13429955
user13429955

Reputation:

let found;

message.guild.channels.cache.each(channel => {
   if(found) return;
   found = await channel.messages.fetch("ID_HERE").catch(() => undefined);
});

Upvotes: 1

Florence Winters
Florence Winters

Reputation: 471

Loop through every channel and fetch messages in them.

message.guild.channels.cache.forEach(channel => {
  channel.messages.fetch().then(messages => {
    messages.forEach(msg => console.log(msg.content));
  });
});

This example fetches as many messages as possible from every channel in the server and logs the content of each one. You could use an if statement to check if the message content is the specified quote to look for.

MessageManager.fetch()

Upvotes: 1

Related Questions