Discord Bots
Discord bots for my friends and my robotics team.
Overview
One big reason why I fell in love with computer science is because I get to build things that my friends and I can have fun with and use. An early example of this is the set of Discord bots I created in high school, two for a server with my friends and one for my robotics team.
The bots’ dozens of commands were triggered thousands of times over the years and led to much laughter, competition, and everything in between. While these Discord bots are no longer online today, they still stand out to me as both a source of joy I once shared with friends and a meaningful mark in my programming journey. The source code of Messenger, one of the bots, eventually grew to be over 1,000 lines long, one of the longest programs I had written at the time. Collectively, the 3 bot programs consist of nearly 2,000 lines of code. I learned many things in the process of crafting these bots: iterative development and improvements, interacting with systems designed by others via over a dozen APIs, handling a wide variety of unexpected user behaviors (my friends greatly enjoyed attempting to break my programs), etc. I experimented with techniques like caching, role-based access control, feature flags, logging, and many more. These learnings have remained valuable to me in the years since as I continued applying these approaches, often at much greater scales in production-level systems.
Motivation
I started working on my first Discord bot in early 2021 during the COVID-19 pandemic. School was online, and my friends adopted Discord as the default channel for communications. During this time, one of my friends made a casual comment about Discord bots. I thought: why not build one myself to add some excitement to socializing? About a week later, I made a new server with the bot and invited my friends. As we all played around with the bot’s functionalities, I continued to add new commands and features to the program. A few months later, I also made a second Discord bot that plays music in voice channels.
The third bot I made was for my robotics team. Oftentimes in the team server, a team name or number would get mentioned in conversation that wasn’t familiar to everyone. Instead of everyone individually looking up teams, I thought it would be nice to have an easy way to look up a team so that the information is visible to all. Once I thought of a few other neat features to include, I invited the bot into the team server.
Messenger
My first and most extensive Discord bot, Messenger, is a fun-focused text-based chatbot that also provides some administrative capabilities. It contains a few dozen commands for a variety of purposes. Here are some of the most notable:
- Trivia: Probably the most popular among Messenger’s commands. There is a general
$triviacommand that fetches trivia questions across all categories. By popular request, the$triviacommand also accepts-gand-sflags to fetch trivia questions only from the geography and science categories, respectively. Everyone’s scores were tracked over time, and Messenger also shared leaderboards and individual scoring statistics when requested. - Images: Messenger has several commands that display images fetched from various APIs. For cute animal pictures, the
$dog,$cat, and$foxcommands display images of dogs, cats, and foxes, respectively. The$astrocommand shows astronomy photos, with image descriptions when asked. Last but not least, the$photocommand fetches pictures from the Unsplash API: either a random image or one based on the user’s query. - Math: Messenger has a
$mathcommand to evaluate mathematical expressions likesin(pi / 2)and a$graphcommand to visualize functions likey = x^3 - 2*x + 1. - Knowledge:
$wikifetches content from Wikipedia.$defdefines words.$newsgets the latest headlines from Google News.$weathergets weather information. - Entertainment:
$joketells jokes.$inspirefetches inspirational quotes.$boredsuggests activities when someone is bored. - Administrative: Of course, I programmed a few admin commands, some restricted to me, some also accessible to the other admins (who were usually “elected” from the top of the trivia leaderboard). These commands include muting server members, turning the bot on/off, restarting the bot, and more.
Musician
In addition to Messenger, Musician is the other bot in the server I made for my friends. It allowed my friends and me to play the music/audio of YouTube videos in the server’s voice channels. The bot takes a link or a search query from the user, and handles the rest in the background. I also implemented audio playback controls like pause, resume, and stop.
Tobot
Named after my FIRST Tech Challenge robotics team ToborTech, Tobot is the resident Discord bot of the team’s Discord server, our primary channel of communications.
Tobot includes 4 suites of features: team lookups, competition resources, team-specific shortcuts, and scheduled tasks.
Team Lookups
Discussions in the team Discord server often involved other teams, whether evaluating compatible alliance partners (FIRST Tech Challenge games are played between alliances), watching match videos of other teams, or processing outreach messages we received from teams around the world. As a result, there was a constant need to quickly find information about other teams. This need led to some of the main features I included in Tobot.
Tobot pulled team information from the API of The Orange Alliance, a hosting platform for all sorts of data related to FIRST Tech Challenge. Various commands allowed my teammates and me to look up a range of information about teams. Basic information included location, rookie year, website, and last active season. Additionally, the bot fetched teams’ scoring statistics: win/loss/tie record at events, Offensive Power Rating (OPR) across events, and scoring breakdown by game tasks. Lastly, Tobot was able to pull the awards that a team won in any particular season.
Some of these API calls took a while to run. For example, to display the history of a team’s scoring capabilities, an API call had to be made for every event the team participated in. To optimize for responsiveness when commands were run, I implemented caching for all of these data downloads to avoid redundant API calls.
Competition Resources
Tobot includes two commands for accessing competition resources. One posts links to the official game manuals and game animation. The other reads the RSS feed of the official Q&A forum and posts specific entries on demand.
Team-Specific Shortcuts
Three of Tobot’s commands display shortcuts specific to our team. One sends a list of the key dates in the current season and countdowns to them. The other two provide quick links to team social media profiles and team pages (Google Drive folders, meeting links, calendar).
Scheduled Tasks
Tobot has two scheduled tasks: social media reminders and birthday celebrations.
The outreach subteam planned to post on the team Instagram account twice a week. I set up Tobot to send a reminder message every Tuesday and Friday to ensure that the schedule was followed.
The other scheduled task sent birthday messages. I stored the birthdays of all team members in a JSON file. Tobot periodically checked the file and sent birthday messages on birthdays.
Tech Stack
The programs for the Discord bots are written in Python. The discord.py library is used to interface between Discord’s API and users. Packages like Requests and Beautiful Soup are used for web scraping. NumPy and Matplotlib are used for mathematics and graphing. pandas is used for some data processing functionalities. A few other modules and packages are used for miscellaneous purposes.