Writing your first discord bot for your indie game

So this week, Pyramid Plunge got featured on alphabetagamer.com and this caused quite a number of people to register with the game’s discord server.
From just 4 members, it quickly jumped to around 60.
I wanted to keep the new players engaged somehow.
I had heard a lot about how discord can be used to create a community around the game, especially from No More Robots’ Mike Rose’s articles/videos. So what could I do to quickly add something for Pyramid Plunge new community?

Pyramid Plunge generates the levels procedurally, typical of roguelike. It also has a special mode called Pyramid of the Day where the levels are kept the same for 24 hours. Its leaderboard will reset as well after 24 hours. So I thought this could lend well for the new community on discord.

I thought whoever gets at the top of the leaderboard he would be crowned as Pharaoh of the Day. Perhaps even display a crown near the username too.
I also wanted to let them see the leaderboard directly in discord. And notify the current Pharaoh that he has been dethrowned.

The only problem is I had never created a discord bot before. Luckily there are a lot of resources. And here is a quick rundown to get you started.

Getting started guide

Just visit discord.js guide and it will guide you on how to get started.
I had never used node js before, but the guide is very straight forward to use.
You’ve already coded your game, so this will be easy comparativily 🙂

Do not use any other unofficial guide on youtube or some old article, because the api changes. I’ve wasted 2 hours because I had started off an old tutorial, and then when I tried using the the official documentation, things were not matching up. The helpful official DJS discord channel immediately spotted my problem. I had to restart off the official discord.js guide. This is the reason why this article will not give code examples because the API changes.

I ended up using the following tools on Mac, but it will similar for Windows

  • terminal (to install node js and any packages used. Also to start the bot)
  • Sublime text editor to edit the files
  • Discord itself to test it

In the Discord server I created a bot-test channel and set it private. I also created a Bot Tester role and assigned it to whoever was interested to test it out

If you don’t want to use your local machine, you might be interested in trying out Glitch. As you type it will save and recompile your bot automatically. Once you’re happy with the bot, you can buy their premium service which keeps your bot alive too. (I know there are ways how to keep your free Glitch bot alive, but that’s not cool.)

Hosting on your existing cPanel VPS

I already had a server for PyramidPlunge.com and I wanted to use the same server. Actually it’s a VPS, so it uses cPanel to control the server. But it was relatively easy to set it up. My VPS supports installing node.js. They just have a Setup Node JS icon in cPanel. You just copy your json and js files, and it will automatically download all the packages you’re using. Just make sure to create a subdomain for your bot. That eliminated some weird issue in my case where I couldn’t effectively use the express package.

The only caveat using a cPanel VPS is that after a few minutes of inactivity, the bot would not be responsive anymore. I tried asking customer support for a solution but they were clueless. In some forum online, someone suggested to make a cronjob to ping it every minute or so. But the cPanel cron job setting doesn’t allow fewer than 5minutes interval. So I had to create several cron jobs manually, all doing the same thing but spaced out by a few minutes. That seems to have worked.

The bot so far

So for Pharaoh of the Day, I’ve scheduled a job every 5 minutes (using node-schedule) to check if there were any changes in the Pyramid of the Day leaderboard. If there was any dethroning, it would notify the existing Pharaoh and also post on the pyramid-of-the-day channel.

The Pharaoh is placed in a specific role, so he gets highlighted from the rest of the members of the server. I also change his nickname. Nicknames are like usernames but set for a discord server. So I set the nickname to his username and append a crown universal emoji. Of course, when Pharaohs change, the role is removed and nickname is reset to the username.

Players can also query the current leaderboards for each difficulty in the game. Since the leaderboard system uses a webserver, it already was returning a json string and so it was pretty easy to parse and format the data into a Discord Embed message. An Embed message makes a response from a bot look nicer where you have more options to format the message. Unfortunately there are no tables yet which would be ideal for a leaderboard.

Linking the game with discord

This was kinda hacky. I’m using GameMaker and I couldn’t find a quick way how to make the user “login” with discord. If you have any ideas on this, please fill me in. So what I’ve done is let the player ask the bot to give him his discord id. He copies it and pastes it in the game. His scores will now be linked to his discord id.

What’s next? Teams and Weekly Challenges

I’m hoping I will find some time to add weekly challenges. The discord members will need to choose a team – The Giorgios vs The Felicies. I will make the game let the player select with whom he can play. Currently you can only play with Felicie if you find the Switcheroo curse in some crate. A weekly challenge will tell them on discord what they need to do (e.g. The team who finds most secrets wins). I will be tracking some stats in the game. And then aggregate all stats to know who is the winner. Sounds like a lot of fun (and work 🙂 )