Building a Discord bot that scales
25 April 2023
Many Discord bots are developed with the goal of becoming as big as possible, but this sort of growth comes with an interesting challenge. Different elements have to be considered than when making a bot designed to be used in just a few servers. This post will cover a few of the potential challenges that could arise and how to be ready for them.
It is important to consider how you store data when developing a Discord bot, a good criteria for your data storage is:
- Can it handle thousands or even millions of user/server data?
- Can new features be easily added?
- Will it perform well under pressure?
You should always use a database, and if you want to scale infinitely not SQLite. SQLite is great for small bots however, if you get to the stage where you need to shard your bot across multiple servers, an SQLite file only accessible on one server simply won't cut it, and your shards will need to share data.
MySQL/MariaDB is known to scale well, it is a good choice for your project from when it is small all the way up to possibly handling millions of records. That's not to say it's the only or best database solution, you should do research in to the database that will work well for your project and give you a developer experience that you enjoy.
Sharding is an essential part of scaling a Discord bot, it's a mechanism provided by Discord for running your bot on multiple servers, with different connections to Discord, each shard handling it's own group of servers.
You should make your bot be able to handle running multiple shards, by using the sharing solution for the Discord library you are developing with. However, it is important to note, that up until a certain point, running more than 1 shard is actually not neccesary and may place more resource load on your server (for example Discord starts a new process for each shard). But it is good to be prepared to deploy more shards at any time.
Discord make it mandatory that you run at least 2 shards once your bot reaches 2500 servers, so you should make sure you are running more shards to be ready for that.
If you want your Discord bot to go through many iterations, and stay up to date with the latest Discord features you need to make sure your codebase can be easily worked with to add new features and to update the Discord API library you work with.
If your code isn't written in a modular, understandable way, if you need to make modifications months or years down the line, especially with many modifications made on top of your original code already, it can become harder and harder to work with.
You should make use of multiple files to organize your code, such as a folder with a seperate file for each command.
This one may seem obvious, but as your bot grows, it will reach more and more people, and that means more people who won't understand how to use your bot. You should consider even the most simple things. Leave hints to your users on what to do, be as clear as possible, don't assume they know something.
People who are setting up their first Discord server ever may use your bot, they won't know how Discord bots work, so don't make assumptions users have used bots before. For example, if your bot requires an ID of a channel or user to be provided by the user of the bot, new Discord users will have no idea how to get an ID, especially as it requires developer mode to be enabled in settings. If using an ID is your only option, you should provide a menu that explains how to get the ID.
This is just an example, you should think about things that may seem obvious to you, but may not to everyone and be sure it is clear what to do to minimize confusion.
Thanks for reading!
This article has hopefully helped you consider the challenges you may face in building a Discord bot that scales and think about how you may tackle them. Good luck on your journey of building the next big Discord bot!
Explore more posts