Page 4: Database (SQLite/JSON) Integration

4. Implementing Permanent Data Storage

To ensure data persists even after the bot restarts, we use a simple database. We will use a file-based JSON database as an example.

4.1. Installing a Database Library

Install a library to simplify database connection and operations (e.g., file-based DBs like enmap or quick.db).

npm install enmap

4.2. Logic for Reading and Writing Data

Write functions to store and retrieve data such as scores or warning counts, using the user ID as the key.

// Enmap Example Code
const Enmap = require('enmap');
const settings = new Enmap({ name: "settings" });

// Save data
settings.set("server_id", "welcome_channel", "general");

// Read data
const channel = settings.get("server_id", "welcome_channel");

This functionality allows the bot to permanently remember server configurations or user states.