Very lightweight Discord bot that automatically reacts to messages. Written in TypeScript, based on discord.js.
Setting up this project entails two main parts:
- 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
- setting up the integration with Discord.
We'll tackle each in turn and guide you through the whole ordeal.
First, we'll install direnv
, then we'll set VSCode up to understand Yarn PnP Installs.
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.
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.
Integrating with Discord will entail generating / finding two specific values:
- the Discord Bot Token, and
- the Discord Guild ID.
We'll treat each in turn.
The first thing you need to do is create a new application on the Discord Developer Portal:
Give your new application a flashy new name:
Be sure to copy the application ID, we'll need it later:
Now let's add a bot to your newly-created application:
And confirm our choice:
Give your new boy a flashy new name:
Finally, let's give the bot the Message Content Intent Privileged Gateway Intent:
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:
After all this fooling around, let's wrap up the bot integration by resetting the bot token:
Don't mind the FUD:
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.
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":
Paste this value somewhere safe, we'll need it further down.
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:
Finally, confirm the permissions set above:
And... you're done! Congratulations!
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:
- replace
YOUR_DISCORD_BOT_TOKEN
with the value we copied here, - 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
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.