This is a full-stack Discord.js Bot template that includes code for the frontend, backend, and the database of your choice—which in this case is MongoDB. I created this project as a means to learn basic web development concepts such as containerization, database management, and backend development. Additionally, I learned how to write in Typescript for this project!
| Table of Contents |
|---|
| 💻 Tech Stack |
| ⚡ Features |
| 🤔 Prerequisites |
| ⚙ Getting Started |
My Ethereum Wallet0x5C429b3fdc7E6F7a692C234358ba31492Feb651C![]() |
My Bitcoin Walletbc1qw80kkgu8yp4mwzuzddygmnyamcjesfavwmer8a![]() |
or...
Sponsoring me is not a must but will be immensely appreciated!
A lot of the Discord.js bot templates I've seen online are mainly designed for beginners. This project however, aims to accomodate intermediate to pro-level programmers who are more equipped to handle backend code. Compared to others, this project does not run by a monolithic architecture; it's built to be robust, modular, and scalable. I made it as generic as possible so that you can easily build on top of it with your own features in mind. NeKoRoBOT.js is built to be lean and unopinionated because every possible API interaction using the Discord bot counts and must be processed fast!
NeKoRoBOT.js is a stripped-down version of a proprietary Discord bot me and my friend made for a custom matchmaking platform for a shooter game. We chose the Python interpreting language as the bot's backend instead of putting everything on Node.js for the following reasons:
- To have clean separation of concerns. We want the frontend to only handle Discord.js and not mix database/connection logic with it too much. Everything has an abstraction layer to avoid spaghetti code. There are only like two or three instances where we need to pull back from the db using JavaScript, the rest is just the frontend pushing payloads from bot interactions.
- Abstraction, Scalability, and Maintainability. Everything can be replaced and reimplemented using different methods; we really just used Python and MongoDB for this case. Why? Because it's easy to read, inherently making it easier to maintain. With how we designed the abstraction layers and decoupled logic, the code is easier to understand and scale. If you really want to port the backend to a different language, you just have to follow and translate the current architecture over to the language you want.
- Separation of Concerns - Node.js only handles Discord.js, your frontend; on the other hand, the project uses a separate backend using Python and FastAPI.
- Database-agnostic Backend - Done via abstraction layer between the active DB implementation (currently MongoDB) and the data handlers. This makes it easier to swap from one database to another if necessary.
- Stateless and Scalable
- Uses
valkey, an open-source version of Redis to manage concurrency. - Uses FastAPI to handle WebSocket and REST endpoints. This makes it easier to integrate a dashboard later on.
- Uses
- Hardened Security
- Uses
.envto hide secret variables and requires an API Token to securely establish a WebSocket connection from the bot to the database. - Uses
pydanticto enforce data formats and avoid SQL injections and the like. - Has a server-side rate limiter.
- Has a self-healing websocket bridge that uses ping hearbeats to wipe off dead connections and an exponential backoff to avoid spamming when attempting to reconnect.
- Has robust error-handling to make sure the bot doesn't crash when an error occurs across the entire stack.
- Uses
- Tag/Category-based Pagination for
/help- Commands can be configured to be organized according to their tag which is then handled automatically by the/helpcommand by creating navigable pages via button components. - Example CRUD Commands - Comes with generic Create, Read, Update, and Delete slash commands out of the box to demo how the Discord bot interacts with the Python database layer.
- Deployable and Fully Containerized - Install Docker on your remote server, build the project as images, and run.
- The codebase is hardened and production-ready but it is not secure over the internet. To ensure security, add another layer of protection by setting up
nginx(webserver) andcertbot(SSL certificates) on your Linux VPS if you intend to deploy this bot on remote servers. - Make sure to sanitize the DB by using
motorand passing structured dictionaries as data. - Use
asynciofor the Python backend so that heavy-duty processing won't block bot function.
- Autogen database schemaas via OpenAPI for the frontend to use, to reduce occurences of bugs and malformed/mismatched payloads
- GoLang reimplementation on another branch
- Dashboard powered by React.js w/ Tailwind CSS
- The man, the myth, the legend—Documentation!
This template assumes you already...
- Know how to use Docker and Docker Compose.
- Know how to use MongoDB.
- Have registered an App at the Discord Developer Portal.
- Have Node.js and
npmto installpackage-lock.jsondependencies. - Have a virutual environment for Python and
pipto install dependencies fromrequirements.txt.- I recommend making a virtual environment within the
corefolder for there you can install the packages locally and run the database so that it won't mess with your global environment.
- I recommend making a virtual environment within the
Before running the project, you must configure your environment variables.
- Rename the provided
ENVEXAMPLEfile to.envin the root directory.
Fill in the required credentials:
TOKEN,CLIENTID,GUILDID(From the Discord Developer Portal)DBURI(Your MongoDB connection string)APITOKEN(Create a random, secure string for the WebSocket handshake between the bot and backend)
Because the bot is fully containerized, the fastest way to get both the frontend and backend running together is via Docker Compose.
# Build the images and spin up the containers in the background
docker-compose up --build -dTo view the live logs and verify that the WebSocket connection was established successfully:
docker-compose logs -fIf you need to edit code and debug locally without rebuilding containers, run the services in two separate terminals.
Terminal 1: Start the Python Backend
cd core
# Create and activate a virtual environment
python -m venv venv
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
# Install dependencies and run
pip install -r requirements.txt
python main.pyTerminal 2: Start the Discord Bot Frontend
cd bot
# Install Node dependencies
npm install
# Build and run the bot
npm run build
npm start


