Discord.js how to fetch message by content

I trying to make a code that fetch messages and filter them with content but it don’t work. I try this code:

client.on('message', message => {
    if(message.content.startsWith('f-')){
        var wf = message.content.substring(2);
        message.channel.send(`message id: ${message.channel.messages.fetch().filter(mc => mc.startsWith(wf))}`);
    }
}); 

But it gives that error: message.channel.messages.fetch(…).filter is not a function so i wanna ask is there a way to fetch messages by id

Answer

message.channel.messages.fetch() returns a Promise and if you want to get the result you have await it. When you have fetched all messages you can go on and filter them by the msg.content.

The following code gives you all messages that start with “f-“:

client.on("message", async (message) => {
  if (message.content.startsWith("f-")) {
    var wf = message.content.substring(2);

    const messages = await message.channel.messages.fetch();
    let filtered = messages.filter((msg) => msg.content.startsWith(wf));
    console.log(filtered);
  }
});