Discord History Bot
Friday, 04 June, 2021 All posts
I have a discord server with some buddies from school. We would use it a lot to talk and hangout virtually during Covid quarentine and use it to communicate with each other like a groupchat. Since we all check the discord server multiple times a day I thought it would be fun to program a Discord bot to spice up the server a little bit. Since python's discord library deals with asynchronous events I could also use the development process to learn more about async events. I decided to create a history bot that would send an embedded message of some cool events that took place on that day every morning as I could be fun to check with a cup of coffee.
Web Scraping
I decided to create the bot with all of the data on board already. This way I could limit the amount of requests it would have to make everyday and
keep the price of hosting my bot free. I created a webscraping script that could scrape events from a history website (I checked the robots.txt file and followed their rules).
I decided to go with a specific website that had easy URLs to work with. their site used URLs like: website.com/month/day
.
With that I could create a simple URL builder function that could loop through all the months of the year and days per month and scrape all the data.
With this accomplished, my discord bot could access the file generated by my webscraper instead of having to scrape information off a webpage every morning.
Data Formatting
I went with a JSON file to write the data to since it would be easy to access the information given the current month and day. The JSON file I built from the webscraped data was nested and looked like this:
{
"January": {
"1": [list of events],
"2": [list of events],
.
.
},
"February": {
"1": [list of events],
.
.
},
.
.
}
This allowed me to access the list of scraped events by simply taking the month and date and kept my file clean and organized.
How the program works
My Discord bot stays on 24/7. It works by checking the time every 10 seconds, if it is 5:00 AM it will grab the month and day using the DateTime module and pull the list of events from the JSON file generated by the webscraping script. It will then randomly select 5 events from that list of events and create a discord message and send it to the "history" text channel of the server. The bot will then sleep for 90 seconds to ensure there it does not send multiple messages the same day then continues the process of waiting for it to be 5:00 AM again.
An example event
Conclusion
This Discord bot was a lot of fun for me to make. I got to practice more webscraping using python with requests and the beautifulsoup library. I also got more expierence in async events in web applications. I am happy with how the bot turned out and it appears my friends enjoy the bot too. I notice that it creates some intereaction as people will occasionally message in the channel to talk about cool events that took place. It was a fun Bot to build and has given me more ideas of different utility bots I could make for my servers and others.