1.0.3 • Published 8 months ago

lindiscord.db v1.0.3

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

Discord Data Storage

A simple bot to store and manage data on Discord using lindiscord.db.

Encryption is optional but uses AES GCM as a fast, reliable and secure encryption method.

Contact me

Discord: reallinens

Installation

  1. Install dependencies:
    npm install discord.js lindiscord.db
  2. Set up your bot on the Discord Developer Portal and get the bot token.
  3. Replace 'YOUR_BOT_TOKEN' in the code with your token.

Usage

Without Encryption

This example shows how to use the bot without encryption.

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.login('<token>');

client.on('ready', async () => {
    const { db } = require('lindiscord.db');
    const dbInstance = new db({
        bot: client, // If you don't know how to get ur guild id/channel id, search it up.
        guildid: 'your-guild-id', // Guild to look for the channelid in
        channelid: 'your-channel-id' // Channel to store data in
    });

    // Wait for data to load (recommended so you don't write/set data before it loads the old from discord)
    await dbInstance.waitForDataLoad();

    // Set data
    dbInstance.set("test.mhm.hi", 5); // data["test"]["mhm"]["hi"] = 5
    dbInstance.set("test.straight", []); // data["test"]["straight"] = []
    dbInstance.set("test.wow", 0); // data["test"]["wow"] = 0

    // Push new values to the array test.straight
    dbInstance.push("test.straight", "wow");
    dbInstance.push("test.straight", "wow2");
    dbInstance.push("test.straight", "wow3");
    dbInstance.push("test.straight", "wow4")

    // Pull (remove) values from the array
    dbInstance.pull("test.straight", "wow3");
    dbInstance.pull("test.straight", ["wow", "wow2"]);
    console.log(dbInstance.get("test.straight")) // ["wow4"] [ we pulled wow and wow2 ]

    // Get data by key
    const value = dbInstance.get("test.mhm.hi"); // 5
    console.log(value);

    // Update data
    dbInstance.set("test.mhm.hi", 7);
    const updatedValue = dbInstance.get("test.mhm.hi"); // 7
    console.log(updatedValue);

    // Delete data
    dbInstance.del("test.mhm");
    const deletedValue = dbInstance.get("test.mhm"); // undefined
    console.log(deletedValue);
    
    // Get all data
    const allData = dbInstance.getAll();
    console.log("All Data:", alldata); // <object>
});

Methods

  • load(): Load data from messages in the channel.
  • save(): Save data to the channel (automatically triggered).
  • get(key): Get stored data by key.
  • getAll(): Get all data
  • set(key, value): Set data for a specific key.
  • setAll(data): Set multiple values.
  • push(key, value): Add an item to an array.
  • pull(key, value): Remove an item from an array.
  • del(key): Delete data by key.

Encryption

To enable encryption, pass the encryption object (with key and iv) when creating the db instance. Use genconfig to generate the key and iv.

Using Encryption

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.login('<token>');

client.on('ready', async () => {
    const { db, genconfig } = require('lindiscord.db');
    const encryption = genconfig("custom_passphrase");
    /* // Example if you were trying to re-use the encryption from genconfig ->
    const encryption = {
        "key": "ee9aa5528459ca94684a5b688e8acd0165f9e425376ff66ca3a55fb0dc983ddb",
        "iv": "56841cf3d8f4527509e16b9100506648"
    };
    */
    
    console.log("Encryption table [store it in a safe place, can be stored and used from a json file]: ", encryption);

    const dbInstance = new db({
        encryption: encryption,
        bot: client,
        guildid: 'your-guild-id',
        channelid: 'your-channel-id'
    });

    // Wait for data to load (recommended so you don't write/set data before it loads the old from discord)
    await dbInstance.waitForDataLoad();
    
    // Set data with encryption
    dbInstance.set("test.mhm.hi", 5);
    dbInstance.set("test.straight", []);
    dbInstance.set("test.wow", 0);

    // Push new values to the array
    dbInstance.push("test.straight", "wow");
    dbInstance.push("test.straight", "wow2");
    dbInstance.push("test.straight", "wow3");

    // Pull (remove) values from the array
    dbInstance.pull("test.straight", "wow3");
    dbInstance.pull("test.straight", ["wow", "wow2"]);

    // Get data by key
    const value = dbInstance.get("test.mhm.hi"); // 5
    console.log(value);

    // Update data
    dbInstance.set("test.mhm.hi", 7);
    const updatedValue = dbInstance.get("test.mhm.hi"); // 7
    console.log(updatedValue);

    // Delete data
    dbInstance.del("test.mhm");
    const deletedValue = dbInstance.get("test.mhm"); // undefined
    console.log(deletedValue);
});

Generating Encryption Config

Use the genconfig function to generate the encryption key and IV:

const { db, genconfig } = require('lindiscord.db');
const encryptionConfig = db.genconfig("your-passphrase");
console.log(encryptionConfig);

License

MIT License

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago