Discord-Scraper
Discord-Scraper copied to clipboard
howto
Hi there, not good at python. I just want to use this script to grab the last message in a channel and end. Is that possible? Any help appreciated.
Grabbing the most recent message in the channel is easy since the official Discord API offers a function where you can ask for anywhere between the most recent message to the most recent 100 messages (nothing more though, hence why I had to opt for the undocumented API endpoints in this script to scrape and archive entire channels).
In fact the script does rely on that very documented API endpoint to save time on having to iterate through all of the days decrementing from the day the script was executed to the day that the most recent message in the channel was posted: https://github.com/Dracovian/Discord-Scraper/blob/156a1ce2276144629f318e97f6492c361a6fefa4/discord.py#L28-L61
But when it comes to getting the oldest message in a channel, that's where the undocumented search API is required since Discord does offer the option to sort messages in a channel in chronological order from newest-to-oldest or from oldest-to-newest.
The API endpoint for retrieving the oldest message in a server is:
/api/v{version}/guilds/{guild.id}/messages/search?channel_id={channel.id}&sort_by=timestamp&sort_order=asc&offset=0
The current Discord API version as of me writing this response is 9 so it would be /api/v9
and the rest you would have to supply on your own based on the ID of the guild/server that has the channel you're wanting to retrieve the oldest and newest message from.
This script's functionality is to essentially start at the most recent message in a channel and then traversing backwards day-by-day until you reach the Discord epoch (set to January 1, 2015 despite Discord not being publicly accessible until May of 2015).
I realize that this script is saving messages in cached folder. How can I have the script run once, grab the last message, and end without continuing to run and hit the 429 errors?
HTTP 429 errors are caused by Discord's rate limiter.
So far the best solution for that is to slow down the number of requests sent to the server, in the meantime I'll have to figure out a method of detecting and implementing a way to alter the number of requests to be sent at a time to keep from hitting their rate limiter so often.