Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  182] [ 7]  / answers: 1 / hits: 5387  / 3 Years ago, fri, june 25, 2021, 12:00:00

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


More From » discord

 Answers
10

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);
}
});

[#1184] Friday, June 18, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
Thu, May 14, 20, 00:00, 4 Years ago
;