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 nvm
, Node Version Manager.
Run the following script or check the complete instructions here.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
Install Node version:
nvm install
Use the version required in the project:
nvm use
Install PNPM:
npm install -g pnpm
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,
To run the bot in development mode type pnpm dev
.
This should only be used during the development process, use the production mode for deployments.
To run the bot in production mode type pnpm prod
, this will compile the typescript code and execute the generated javascript code with Node.js.