Reputation: 111
I would like to fetch x amount of messages to later send them a rich embed. How can I fetch that amount?
Upvotes: 1
Views: 6427
Reputation: 6796
When you call TectChannel.fetchMessages()
it returns a Promise that is resolved with a Collection of messages.
To send them in a RichEmbed, you have to either use .array()
and transform the Collection into an Array or use .forEach()
. I'll show you how to use the Array.
let x = 10, // x should be form 0 to 25
embed = new Discord.RichEmbed().setTitle('Fecthed messages');
channel.fetchMessages({ limit: x }).then(messages => {
let arr = messages.array(); // you get the array of messages
for (let i = 0; i < arr.length; i++) { // you loop through them
let curr = arr[i],
str = curr.content.trim();
if (str.length > 2048) str = str.substring(0, 2045) + '...';
// if the content is over the limit, you cut it
embed.addField(curr.author, str); // then you add it to the embed
}
}).catch(console.error);
Upvotes: 2
Reputation: 222
The following is the example given in the discord.js docs:
// Get messages
channel.fetchMessages({ limit: 10 })
.then(messages => console.log(`Received ${messages.size} messages`))
.catch(console.error);
This will retrieve the latest 10 messages from the text channel.
You can read more about the method and its options here: https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=fetchMessages
Upvotes: 1