Skip to content

NeKoRoSYS/NeKoRoBOT.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

divider



Typing SVG


GitHub Repo stars Size GitHub last commit GitHub contributors Discord


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

divider

Sponsor Me!


My Ethereum Wallet

0x5C429b3fdc7E6F7a692C234358ba31492Feb651C

Ethereum QR Code
My Bitcoin Wallet

bc1qw80kkgu8yp4mwzuzddygmnyamcjesfavwmer8a

Bitcoin QR Code

or...

Donate via PayPal Donate via Ko-fi

Sponsoring me is not a must but will be immensely appreciated!


divider

Why choose NeKoRoBOT.js?


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!

Why choose Python?

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.

Tech Stack


Frontend

Node.js Discord.js TypeScript JavaScript
Backend

Python MongoDB
DevOps

Bash

Features

  • 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.
  • Hardened Security
    • Uses .env to hide secret variables and requires an API Token to securely establish a WebSocket connection from the bot to the database.
    • Uses pydantic to 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.
  • Tag/Category-based Pagination for /help - Commands can be configured to be organized according to their tag which is then handled automatically by the /help command 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.

Reminders and Limitations

  • 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) and certbot (SSL certificates) on your Linux VPS if you intend to deploy this bot on remote servers.
  • Make sure to sanitize the DB by using motor and passing structured dictionaries as data.
  • Use asyncio for the Python backend so that heavy-duty processing won't block bot function.

Upcoming Features

  • 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!

Prerequisites

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 npm to install package-lock.json dependencies.
  • Have a virutual environment for Python and pip to install dependencies from requirements.txt.
    • I recommend making a virtual environment within the core folder for there you can install the packages locally and run the database so that it won't mess with your global environment.

Getting Started

Configuration

Before running the project, you must configure your environment variables.

  • Rename the provided ENVEXAMPLE file to .env in 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)

Running with Docker

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 -d

To view the live logs and verify that the WebSocket connection was established successfully:

docker-compose logs -f

Running Manually

If 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.py

Terminal 2: Start the Discord Bot Frontend

cd bot

# Install Node dependencies
npm install

# Build and run the bot 
npm run build
npm start

divider

About

Full-stack Discord.js bot template

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Contributors