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:
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
- 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 OPDo you need the first (oldest) message or all messages?
I need all messages
So fetch while
pack
is not empty
Set the limit to 100 btw
And don't forgetThank 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?
(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:
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
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?
Yes that makes sense! Thank you :)
And no type declarations !
@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