Home
Minecraft Server (AWS)

Discord Bot (Python, AWS, SQL)

This Website (HTML, CSS, JavaScript, Git)

Certifications

Discord Bot.


The original goal of this bot was to allow myself and my friends to launch and shut down the Medium EC2 instance remotely to allow us to play the same Minecraft world 24/7 while staying cost effective.
I decided to program this bot in Python since I had never used this language before and I was interested in learning it (prior experience in Java & C#)

My first step was to install VS Code along with a Python extension. I've seen a steady rise in popularity with VS Code and thought it might be interesting to give it a try. In hindsight, I'm very happy I did as I find it approachable and easily customizable, with loads of extensions. I was already comfortable with Visual Studio, but VS Code felt even more user friendly.

I started by importing 2 packages for this bot: discord, and boto3.
Boto3 is a package I found that leverages the AWS API, and is well documented. The Boto project started as a customer-contributed library, but is now the official AWS SDK.
Finally, I installed the json package to be able to read metrics returned by Cloudwatch.

The Discord API made it easy to read incoming messages from a server and react appropriately. For example, "!StartServer" triggers the launch of my Medium EC2 instance, and "!StopServer" shuts it down.
It also runs a function on loop every 5 minutes which captures the "NetworkOut" metric from AWS Cloudwatch, and shuts the server down if it's been inactive for 30 minutes.
For the full bot code, here's a link to the github page.

At this point, all that was left was storing this bot on a public server that could run 24/7... and what better than another EC2 instance??
Back in AWS, I launched a secondary EC2 instance under the "micro" tier which is free for a full year. I wrote a simple nohup script to launch this bot in the background. This allows me to start the bot and leave it running in the background if I close the shell session. To kill the bot, I use "ps aux | grep python" to find the correct process, then use "kill (process id)". This is now a cost-efficient solution which allows anyone in my Discord server to launch our Minecraft server any time they please.

As an additional challenge, I installed the SQLite3 package to keep a database of member activity, and display it. Leveraging the Discord API, I keep track of any games played we play, and display the time played when requested.