Skip to content

lacrypta/discord-badges-bot

Repository files navigation

Discord Auto-Reactor Bot

Very lightweight Discord bot that automatically reacts to messages. Written in TypeScript, based on discord.js.


  1. Setup
    1. Development Environment Setup
      1. DirEnv
      2. Yarn PnP Installs
    2. Discord Integration
      1. The Discord Bot Token
      2. The Discord Guild ID
      3. Inviting the Bot to Discord
    3. .env Setup

Setup

Setting up this project entails two main parts:

  1. setting up your development environment (we'll show detailed steps for VSCode under Linux, but you can pretty much use whatever works for you), and
  2. setting up the integration with Discord.

We'll tackle each in turn and guide you through the whole ordeal.

Development Environment Setup

First, we'll install direnv, then we'll set VSCode up to understand Yarn PnP Installs.

DirEnv

To make your life superbly easier, you'll need to install DirEnv. This mostly entails installing the direnv package in your distro and hooking DirEnv to your shell. Finally, be sure to allow DirEnv to run on this project's root:

direnv allow .

If you're using VSCode, consider installing the direnv extension, this will teach VSCode how to read your .envrc files.

Yarn PnP Installs

VSCode needs some help in dealing with Pnp Installs.

To begin, we'll need the ZipFS VSCode extension (this is required because Yarn PnP Installs keep packages compressed and navigating within them would fail otherwise).

Next, we need to update all Yarn packages:

yarn

Now we need to ask Yarn to generate the required SDKs:

yarn dlx @yarnpkg/sdks vscode

At this point, VSCode will ask us if we want to use the Workspace-provided Typescript compiler, to which we should answer yes.

If you have any troubles with this step, please see the official Yarn documentation for more details.

Discord Integration

Integrating with Discord will entail generating / finding two specific values:

  1. the Discord Bot Token, and
  2. the Discord Guild ID.

We'll treat each in turn.

The Discord Bot Token

The first thing you need to do is create a new application on the Discord Developer Portal:

New Discord Application

Give your new application a flashy new name:

Create Discord Application

Be sure to copy the application ID, we'll need it later:

Copy Discord Application ID

Now let's add a bot to your newly-created application:

New Application Bot

And confirm our choice:

Confirm Bot Creation

Give your new boy a flashy new name:

Name Discord Bot

Finally, let's give the bot the Message Content Intent Privileged Gateway Intent:

Configure Discord Bot

Although not technically required, you may verify that the permission bitmap value we'll use further down (ie. 68672) does not contain any spurious permissions by checking the Bot Permissions Calculator:

Discord Bot Permissions

After all this fooling around, let's wrap up the bot integration by resetting the bot token:

Reset Discord Bot Token

Don't mind the FUD:

Confirm Reset Token

And copy the value revealed:

Copy Discord Bot Token

Now, it's very important that you paste this value in a safe place, at least until we finish the integration and configuration steps, since we'll need it further down.

The Discord Guild ID

If you've managed this far, this is going to be a breeze. Simply go to the Discord application, look for the server you want the bot to appear, right-click on it, and select "Copy ID":

Copy Discord Guild ID

Paste this value somewhere safe, we'll need it further down.

Inviting the Bot to Discord

In order to invite the bot you just created, you'll need to build an invite URL... don't worry, it's really easy. Just copy this URL replacing YOUR_DISCORD_APPLICATION_ID with the value we saved before:

https://discord.com/oauth2/authorize?client_id=YOUR_DISCORD_APPLICATION_ID&scope=bot&permissions=68672

Navigating to it will greet you with:

Invite Bot to Discord

Finally, confirm the permissions set above:

Authorize Bot

And... you're done! Congratulations!

.env Setup

Every runtime datum in the project will be directed by the .env file. You don't have an .env file yet, let's fix that.

Copy .env.example to .env:

cp .env.example .env

This will leave you with an .env file like:

# shellcheck disable=SC2148,SC2034

# Discord
DISCORD_BOT_TOKEN="YOUR_DISCORD_BOT_TOKEN"
DISCORD_GUILD_ID="YOUR_DISCORD_GUILD_ID"

Now is the time for our hard work to bear fruit. You need to change the placeholder values (ie. YOUR_... strings) to the values we extracted before:

  1. replace YOUR_DISCORD_BOT_TOKEN with the value we copied here,
  2. replace YOUR_DISCORD_GUILD_ID with the value we copied here,

Finally, let's ask DirEnv to reload our environment to pick up the new .env changes:

direnv reload

Running the Bot

To run the bot in development mode type yarn dev. This should only be used during the development process, use the production mode for deployments.

To run the bot in production mode type yarn prod, this will compile the typescript code and execute the generated javascript code with Node.js.

About

La Crypta Badges bot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published