Getting messages from a channel
Is there an example anywhere in the guide or docs for fetching messages from a channel? The only examples I can see start with channel.message.fetch() or something along those lines, but Ican't find anything that explains how channel is declared in this context. I assume it is along the lines of
const channel = new SomeConstructor(some properties)
I'd appreciate any pointers
4 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!You could retrieve the channel by id like this: https://discordjs.guide/popular-topics/faq.html#how-do-i-send-a-message-to-a-specific-channel
you can also access the channel from various structures for example <Message>.channel
that was really helpful, I was trying to fire it off using a slash command. This is what worked for anyone else in my situation
async execute(interaction) {
await interaction.reply("last 10 messages are : ");
const channel = interaction.client.channels.cache.get(
"my channel id as a string"
);
await channel.messages
.fetch({ limit: 10, cache: false })
.then((messages) =>
messages.forEach((e, i) => interaction.followUp(e.content))
);
},