Refactoring is important, but also difficult
You ever create something with the best intentions of it being to standard and end up just winging it after you get started? Yeah, I did that with Torc Bot, the discord bot I created for the Torc.dev community. Originally slotted for just a summarization bot to help the community catch up with chats turned into a randomized grouping of features all jumbled into a 2k line single file bot. Originally I built out the summarization to use Groq AI llama model just to pull the last 100 messages from a channel and pass them to the AI model for summarization. Worked pretty well and was useful.
Then came requests to add a weekly server summary so we could see what was discussed across all channels, then reminder functionality and some internal tools to help the Torc team. What started out as a simple single file discord bot started to morph into a giant mess of commands and services functions and utility functions that was getting hard to maintain and even harder to add new features to.
Why can't I just do this thing?
This became evident when I went to add a new feature to have translation services for some of our streamed events in the discord community. As Torc is a global team and we have multiple languages and our live events usually have a Spanish speaking alternative for our LATAM community. These are awesome events and sometimes the LATAM side gets different events than our regular english speaking side and if you don't speak Spanish those events may not make a lot of sense. So I set out like the good little botman that I am to add the ability to have live translation to the discord bot. That is when I realized that I should have done things correctly from the start.
I was attempting to add in multiple commands, utilities and services to this discord bot that would all be living inside an already jam packed file and things were getting lost easily. If I had an issue with something not working properly, it was a seek and find to try and track down where the function was that controlled that logic and ensuring that the dependencies were all loaded correctly and it was maddening, like Why can't I just do this thing?
The Great Refactor
Now I won't lie to you and say I spent many nights working the refactor of this discord bot. I took a couple days to sort out the basic project layout with file structure, and then tossed it over to Claude to handle moving functionality and code into specific files for utility functions and services as well as commands. Once Claude was done migrating and setting things up, I spent a couple days going through each new file created to ensure commands were correct and referencing the right environment variables for both local hosting and live server hosting. What would have taken me probably a month of going line by line to sort through everything into the correct files and make sure that cross functionality was working only took Claude like 45 minutes total with a few re-prompts for clarification.
The leg work of just verifying files was a lot easier utilizing AI in this fashion. I knew that all the commands worked, and that everything that was currently implemented was functional on the server hosted version (outside of coffee pairing... stupid bot). So passing off just the reorganization of files to AI was the easiest decision to make.
What is the result
The end goal was a user friendly code base that was easy to navigate, edit and add new features to. Overall I feel like that has been accomplished by doing this refactor of everything. I now have a code base that someone can fork and run their own discord bot for their own server or whatever without needing to decipher hieroglyphics or hunt through numerous files trying to diagnose any issues that they may have during setup or development.
The bot went from a 2k+ line index.js file to that file being 154 lines in total currently. Obviously there are more files, but overall the structure is easier to understand given the standard file structure.
Hopefully going forward I have an easier time adding in new features and diagnosing issues when there are changes to the Discord API or way of doing things and it makes it easier to add the fixes without hunting for hours and hating myself for past decisions.
Why am I writing about this?
To provide the learned lesson to any other developer starting a new project to encourage you, even if the project seems like it will be something small and you don't think you will be iterating on it. Make sure that you set up your project structure according to the normal standars for a project that scales to avoid the headache that I had. Although not critical as this was just a discord bot, the extra work to refactor and verify could have taken a lot longer to get done. This would have meant new features and requests would have had a longer ramp up time to be introduced. Versus if I had just done it correctly from the start we may be further ahead than we are now.
Disclaimer: First image was generated using AI ChatGPT other images and all writing was done without AI assistance.



Top comments (0)