Reputation: 37
I'm trying to fetch all messages from a channel, then log the content from those messages, is there a way to do this?
I've tried this, but it doesn't work:
const fetched = await client.channels.get("505989241600213012")
.fetchMessages({limit: 1})
.then(message => console.log(`[${message.author.name}]${message.content}`));
This is the result I get:
Undefined
,
and [${message.author.name}]
It doesn't even return anything, since you can't read anything from undefined
.
Upvotes: 1
Views: 2080
Reputation: 977
fetchMessages
will always return a Collection, even if you use limit: 1
. So, if you want to access the first element of the Collection you need
const fetched = await client.channels.get("505989241600213012")
.fetchMessages({limit: 1})
.then(messages => console.log(`[${messages.first().author.name}]${messages.first().content}`));
If you plan to save the messages outside of Discord you might want to consider to use cleanContent. It's also not good practise to combine await
and then
. Probably a good idea that you choose one.
Upvotes: 2