Fetch all messages in a text channel

Hey there! I'm trying to fetch all messages in a channel for my /initialize command. I'm looking at the faq example:
async function fetchNine(manager) {
const result = []; // this will combine the results of each fetch call
let pivot = null;
while (result.length < 9) {
// fetch a pack
// if no pivot is available, pass undefined so the "before" parameter is ignored
const pack = await manager.fetch({limit: 3, before: pivot?.id});
// move the starting point to the current pack
pivot = pack.last();
// add the pack to the result
pack.forEach(entry => result.push(entry));
}
return result;
}
async function fetchNine(manager) {
const result = []; // this will combine the results of each fetch call
let pivot = null;
while (result.length < 9) {
// fetch a pack
// if no pivot is available, pass undefined so the "before" parameter is ignored
const pack = await manager.fetch({limit: 3, before: pivot?.id});
// move the starting point to the current pack
pivot = pack.last();
// add the pack to the result
pack.forEach(entry => result.push(entry));
}
return result;
}
I think I need to modify the while conditional. The amount of messages in the channel is dynamic, so I can't base it off that. I don't want to manually copy the id of the oldest message either. I was thinking about approaching it by timestamp or by checking if there's a previous message before running it again. Are there any examples of this / suggestions? Thanks!
8 Replies
d.js toolkit
d.js toolkit4w ago
- 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! - Marked as resolved by OP
Syjalo
Syjalo4w ago
Do you need the first (oldest) message or all messages?
vince
vinceOP4w ago
I need all messages
Syjalo
Syjalo4w ago
So fetch while pack is not empty Set the limit to 100 btw And don't forget
vince
vinceOP4w ago
Thank you! This is just for a one and done command to populate some database values. I'll work on this and update here I think I got instantly throttled lol but is this what you were thinking?
const getAllMessagesFromChannel = async (channel: TextChannel) => {
const messages: Message[] = [];
let pivot = null;

while (true) {
const pack: any = await channel.messages.fetch({
limit: 100,
before: pivot?.id,
});

if (pack.size === 0) break;

pivot = pack.last();
pack.forEach((entry: any) => messages.push(entry));
}

return messages;
};
const getAllMessagesFromChannel = async (channel: TextChannel) => {
const messages: Message[] = [];
let pivot = null;

while (true) {
const pack: any = await channel.messages.fetch({
limit: 100,
before: pivot?.id,
});

if (pack.size === 0) break;

pivot = pack.last();
pack.forEach((entry: any) => messages.push(entry));
}

return messages;
};
(btw, do you know what type I should use for pack? hacking it right now with any) I'm definitely a TS noob but it doesn't compile / editor yells at me unless I type the ones I typed (otherwise I probably would have let it infer) Very true, it's definitely a hack. For example pack shows me:
'pack' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
'pack' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
I tried to type it earlier but I didn't know what to type it to, I tried Message | null but it was yelling at me since null doesn't have .id Even though we're optional chaining it, I have no idea why it would yell at me for that 😅 Right?! Okay it must have been where I was initializing pack before since you're right that doesn't show up anymore, but now I'm getting a No overload matches this call on fetch(). typescript is hard xD
vince
vinceOP4w ago
No description
vince
vinceOP4w ago
Got it that should be simple enough. How do you figure this stuff out? Obviously you have a lot more experience than I do but I feel like I'm always guessing with TS Ah okay I see that now, thank you! I tried that at first too but it was a ton of TS errors let me try it again and I'll update haha Haha yea I was looking at it before but just had to modify it a little bit since I wanted all messages Were you thinking something like this?
const getAllMessagesFromChannel = async (channel: TextChannel) => {
const messages: Message[] = [];
let pack = await channel.messages.fetch({ limit: 100 });
let pivot = pack.last();
[...messages, ...pack];

while (pivot !== undefined) {
pack = await channel.messages.fetch({
limit: 100,
before: pivot.id,
});

pivot = pack.last();
pack.forEach((entry) => messages.push(entry));
}

return messages;
};
const getAllMessagesFromChannel = async (channel: TextChannel) => {
const messages: Message[] = [];
let pack = await channel.messages.fetch({ limit: 100 });
let pivot = pack.last();
[...messages, ...pack];

while (pivot !== undefined) {
pack = await channel.messages.fetch({
limit: 100,
before: pivot.id,
});

pivot = pack.last();
pack.forEach((entry) => messages.push(entry));
}

return messages;
};
Yes that makes sense! Thank you :) And no type declarations !
souji
souji4w ago
@vince since this has resulted in some discussion i feel inclined to post a bit of a warning here: i'm not sure what intentions you have here, but i'd urge you to re-read the disclaimer and discord dev tos and guidelines you are not permitted to persistently store user generated (read: message) content persistently at rest without reason and encryption at rest what you are doing here eerily sounds like you are trying to either query messages in bulk or otherwise persistently retain messages additionally, you are mistaken with the "one and done" idea of this, since any downtime either on your or discords end may result in you not receiving messages and thus serving wrong information - in whatever you are collecting this information for i also want to mention that a "once and done" operation may still leave the "feasible and permitted" area rather quickly consider i want to fetch the entire history of our #general chat. fetching paginated resources will not endlessly scale and may result in immense resource allocation for little to no benefit - again, i urge you to read about the requirements for storing user generated content we don't need to further discuss that, but it is worth mentioning and considering

Did you find this page helpful?