Back

Playing around with Discord

Over the past few days, I’ve been working on building and deploying a Discord bot. It's been fun, and surprisingly approachable. I hadn't use Discord much, and I've never touched the api until this week, but still managed to get a bunch done.

  1. Creating the Bot with Discord.js

The biggest unlock in this process for me was understanding what a bot actually is in Discord. One way to think about it is basically as an access token. You create a bot in the developers portal and give it permissions (read messages, post messages, stuff like that). After that, you install it in your server, kinda like adding a new user. Once it's in your server, it doesn't do anything — you need to tell it what to do. You do this by running a server (locally to start) that connects to the bot instructing it to do things.

The unlock part: the bot doesn't do anything except provide access for your local server. Your server is where all the stuff happens—where you register commands, scrape channels, listen for events, etc. Eventually, you'll move your bot from your local machine to something hosted, like I did with fly.io.

I built the bot using Discord.js. The main goal was to have a bot that could handle multiple commands efficiently in one process. Maybe I should split these into different processes? Not sure yet, we'll see in time.

Commands so far:

Slash command: A structured command that takes input via Discord’s slash command interface. It passes a message from Discord to a Slack channel via a webhook. On the Slack side, you can decide what room to put the message in. Useful for letting a community team in Discord alert the company team in Slack that something is wrong.

Backup command: A utility to handle data backups and scraping. A ton of product feedback ends up buried in Discord: common complaints and pain points, as well as knowledge from answered questions. The bot downloads all of the threads from a help forum in a structured format to be fed into our AI pipeline. The pipeline part is tbd at the moment, but getting the info was pretty straightforward. This process will probably be hooked up to a cron job and run at regular intervals.

AI Help / auto-reply command: A feature that uses AI to help with questions. A lot of the questions in Discord could be answered just by reading the docs. Luckily, our Docs search already has a rag system setup for this purpose. It combines OpenAI responses with docs content, Github Discussions and more. So far this bot only asks you once if you want help with your question — if you say yes, it will output a single response from our rag system, it isn't a true chat experience. There's a balance here between being annoying and actually providing useful help. We'll figure out where that is over time.

Analytics: A feature to get analytics on the server: thread topics, thread counts, who's helping, who's hindering, etc. Just getting started on this one, but also pretty approachable with discordjs.

  1. Running Multiple Commands in One Process

This is all running from a single bot, loading commands from separate files. I'm using a simple event handler to route commands—maybe a bad idea? Not sure, we'll see!

  1. Dockerizing the Bot

To make the bot portable, I set up a simple Docker container. Pretty cool that I can know the bot will run the exact same way locally and in production. Also cool that I can move it anywhere, tear-down and re-construct machines anytime and any place I want. I've only really been a consumer of Docker up until now, so this has been new and quite fun.

  1. Deploying to Fly.io

Once the bot was containerized, I deployed it to Fly.io, a platform designed for hosting Dockerized applications. Fly has an exceptional ux — all done via the command line with tidy, well-documented commands. You can start/stop/restart machines, add secrets (env vars), and redeploy your container very quickly (fly deploy). I can check live logs in fly to see everything running as expected.

If you’re working on a similar project or thinking about deploying a Discord bot, I hope this overview is helpful. Lmk if you'd like to see any of this code.

Follow along →@saltcod