I suppose this is my first tech-related post. I reckon I'll make a blog post for each of my personal projects, documenting the process and my takeaways.
Table of Contents:
- Context
- The Problem
- My Solution
- Where Do I Even Start
- Stuck Already
- A Foundation
- In The Zone
- A Critical Bug
- An Unexpected Saviour
- Takeaways
- Testing and Deployment
- Conclusion
Context
If you didn't know already, I love playing Destiny 2. I play the game with my friend group, also known as pp clan. We typically schedule weekly sessions to play raids, which are 6-man activities.
The Problem
We would input our available timings in a Google Sheet, which we will then read off to determine which timeslot has enough people to play.
However, as our clan grew, it became inconvenient to read the increasingly cluttered Google Sheets. It was a mess of red and green cells with timings within them.
And so I was inspired.
My Solution
I would create a discord bot that allows people to input their timings for the week, which will then check the Google Sheets if there are 6 or more people available to play at any timeslot. It will then return the timeslot that has enough people, as well as the users in that timeslot.
- Why a discord bot?
Discord is our primary means of communication within the group, so it made sense to create a bot directly within our discord server.
- Why still use Google Sheets?
At first, I wanted to make the bot communicate with Firestore or MongoDB's APIs. However, I wanted to make minimal disruptions to our current workflow. Besides, in the unlikely event that the bot catastrophically fails, we can always revert back to manually updating the Google Sheets.
Where Do I Even Start
To begin with, I have zero experience coding a bot. I did do a web application for my software engineering project the previous semester, but I was completely on my own for making a bot. Nevertheless, I got to work.
I started with the good old Googling "how to make a discord bot", and I was introduced to discord.js and discord.py. Both options seemed good, but I decided to go with discord.js as I wanted to brush up on my Javascript skills.
I also pored over the Google Sheets API documentation, it was quite a lot of reading, but nothing I couldn't handle.
Stuck Already
Okay, so I went through the discord.js tutorial, and did a basic bot that responds to my /ping
command with "Pong!". World class programmer in the making here.
I ran into a problem real fast. There are barely any actual apps that uses the Google Sheets API with Discord's Bot API. This meant that I was on my own most of the time. Consulting Stack Overflow, the Holy Grail of programmers, wasn't even that helpful, as most of the questions I had regarding the Google Sheets API were very outdated. Most of the posts were least 5 years old, using outdated versions of the Google Sheets API.
I had to read every page of the Google Sheets API documentation to get some semblance of what I was doing. And even then I got stuck a good portion of the time.
It was definitely frustrating, but at the same time, I felt like I learnt a lot wrangling with an API for the first time.
A Foundation
Just when I thought I was ready to throw in the towel, I came across a Github repo that contained a discord bot interacting with the Google Sheets API. The repo was 3 years old, but it worked! I finally had something to work with. Huge, huge thanks to Simpleboy353's SheetsBot for the foundation of this bot. I wouldn't have known how to start using the Google Sheets API without building upon his bot. Please buy him a coffee if you liked my bot.
Disclaimer: The repo was under GPL 3.0 license, so it was fair game. My bot is also under GPL 3.0 license.
In The Zone
Before I knew it, I was deeply engrossed in the code. I spent a few good hours straight working on the bot, implementing the features I wanted into the bot, and debugging it.
I'm particularly proud of what I implemented for the /check
function. I spawned 3 arrays, one for each timeslot. The first element of each array is the number of people available in that timeslot, the second element would be the timeslot itself, and every element afterwards would be the list of users available on that timeslot. I would then slice the user list from the timeslot array, in order to extract the users available to play.
A Critical Bug
After a few days, the bot was almost done. However, I had one critical bug. For some reason, the /check
function would inexplicably return an error message, and I was unable to replicate the error. The console log errors were incredibly unhelpful, only returning an "Unknown Interaction" message. I was stuck on this bug for hours. I desperately threw everything I knew against the wall, but none of them stuck. I was this close to giving up on this bot forever.
An Unexpected Saviour
I complained about the bug to my friend, and he said "Why not just ask ChatGPT?"
Huh, I never thought of that.
I'm what you would consider a purist. As much as possible, I refused to rely on LLMs like ChatGPT and Claude ever since their inception. I believed that an overreliance on them would genuinely cause society to fall. I still believe it to this day, but right now, I was desperate. I've consulted Stack Overflow and obscure forums looking for a solution to no avail. I was ready to give myself in to the AI.
I begrudgingly showed it my code, as well as the error message, and asked for a fix. Within a few seconds, not only did it return the fixed code, but also an explanation of what was the error was. I copied pasted its code into my bot, and it worked flawlessly!. All of the problems I had were gone! I was absolutely bewildered.
After I got over my shock, I went back to work and finished the bot within the next hour. It now works perfectly well and to my expectations
Takeaways
From this experience, I learnt a few things.
- Read documentation properly
At the start of the project, I was naively confident that I could do this on my own without reading the Google Sheets API documentation, how wrong was I. Knowing is half the battle won, and reading the documentation allows you to achieve that knowledge. There's also an art in gaining the relevant knowledge from the documentation, a painful lesson I learnt browsing useless parts of the documentation pages.
- Write good code at the start, but don't let it consume you
My code was initially hacky and messy. At first I didn't deem it to be a big issue since it was just a "small" project, but I realised how much I shot myself in the foot when I had to debug the bot. Please write good and readable code, your future self will thank you later.
However, perfection is the enemy of progress. I was stuck in a phase where I wanted to make my code extremely modular and cohesive, and I spent hours being sidetracked from actually coding the bot. In the end I gave up on it. Modular code is great, but sometimes you can just bodge it.
- Don't be afraid to consult AI for help if you're really stuck
Before this experience, I would have scoffed at other people using AI for their work, thinking that my own expertise and experience was enough. However, this personal project taught me that in the end, AI is a tool at my disposal, an amazing one at that. I shouldn't discount its use just because I didn't want to. Utilising AI properly can bring great benefits to your work and life.
Testing and Deployment
After finishing the bot, I invited my friends to do testing on the bot. It worked just fine, and I deployed my discord bot on Cybrancee. The bot is now fully functional and deployed 24/7.
Conclusion
Overall, I learnt a lot from this personal project. I had a lot of fun coding a discord bot, or any bot really, for the first time. I have future plans to make a Telegram bot using OpenAI's API, or maybe another Discord bot. I hope reading this post will help you in your learning journey, or perhaps even give you inspiration for a bot. So go ahead, create something new. A world of possibilities awaits.