Discord is a pretty exciting platform. One of it’s biggest features is the ability to support bots on the platform, letting developers create some incredible tools for server moderators. In 2020, Discord introduced two major new features however, that don’t have a ton of example use cases around them yet: Slash Commands and Interactions Endpoints. These two things are incredibly powerful, and with today’s post I want to go ahead and take a deep dive in how you can use Interactions Endpoints to build an entirely serverless Discord bot.
If you don’t want to read through this and instead want to jump straight into the code, you can see the full repository for this here (with a corresponding NPM package available at https://www.npmjs.com/package/discord-bot-cdk-construct). Otherwise, read on for a full explanation of how the code works!
I’m not going to regurgitate what a serverless architecture is yet again (Google that if you are unfamiliar with it), but I do want to go over the benefits of it, especially in the context of building a Discord bot. Severless architectures generally provide two major benefits over traditional server architectures: High scalability and low costs. The low costs generally comes from the fact that you only pay for what you use, and the high scalability generally comes from the fact that you can more easily distribute your execution across multiple nodes.
In the case of a Discord bot, where the bot only needs to respond to specific commands executed at it, a serverless architecture is actually a perfect use case for such a setup. In fact, thanks to Amazon Web Services’ (AWS) generous free tier for Lambda, you can likely run your Discord bot for near free. I say “near” because you will still need to pay for API Gateway to allow for the calls into the Lambda, but this is still relatively cheap compared to hosting a continuously running server for your bot.
And again, you’re only charged for what you use. So if you get no calls to your bot for a month, then you’ll pay nothing at all!
Okay, what does the setup look like?
The setup for a serverless Discord bot is actually rather straightforward in AWS. Here’s a quick architecture…