Skip to content

Variables

Understanding Variables

Firstly, we need to understand what variables are and what purpose they have.

What are Variables?

Variables are used to store data that can be used later in the code. They are like containers that store data. Variables can store different types of data such as numbers, strings, objects, arrays, etc.

We use functions like, $setUserVar, $setGlobalUserVar and so on.

The default database is a JSON database, called aoi.db. Currently there’s no way to change the database type, except using community packages.

Types of Variables

Variables have different types.

  • Global Variables: These variables are accessible from anywhere in the discord server.
  • Guild Variables & User Variables: These variables are accessible only in the guild where they were set.
  • Message Variables: These variables are accessible only in the message where they were set.
  • Channel Variables: These variables are accessible only in the channel where they were set.

Why use Variables?

Variables are handy in storing data directly within your project. This way you can build more complex commands and store data that can be used later in the code.

If you do not wish to use variables you can always disable the database using the disableAoiDB option in the constructor.


Using Variables

To use the database we first need to setup some things.

  1. Edit your index.js, if you haven’t already. It should look like the following, and have a database option in the constructor.

    1
    const { AoiClient } = require("aoi.js");
    2
    3
    const client = new AoiClient({
    4
    token: "Discord Bot Token",
    5
    prefix: "Discord Bot Prefix",
    6
    intents: ["MessageContent", "Guilds", "GuildMessages"],
    7
    events: ["onMessage", "onInteractionCreate"],
    8
    database: {
    9
    type: "aoi.db",
    10
    db: require("@aoijs/aoi.db"),
    11
    dbType: "KeyValue",
    12
    tables: ["main"],
    13
    securityKey: "a-32-characters-long-string-here"
    14
    }
    15
    });
  2. (optional) Change the securityKey to a 32 character long string. This isn’t exactly needed, but if you want to change it ensure it’s 32 characters long, and don’t change it again otherwise your data will be lost!

  3. The important part; adding variables. To add variables we use <client>.variables(), the <client> stands for your defined AoiClient.

    In our case we defined it as client, so we would use client.variables().

    1
    const { AoiClient } = require("aoi.js");
    2
    3
    const client = new AoiClient({
    4
    token: "Discord Bot Token",
    5
    prefix: "Discord Bot Prefix",
    6
    intents: ["MessageContent", "Guilds", "GuildMessages"],
    7
    events: ["onMessage", "onInteractionCreate"],
    8
    database: {
    9
    type: "aoi.db",
    10
    db: require("@aoijs/aoi.db"),
    11
    dbType: "KeyValue",
    12
    tables: ["main"],
    13
    securityKey: "a-32-characters-long-string-here"
    14
    }
    15
    });
    16
    17
    client.variables({
    18
    warnings: 0
    19
    });

    In this example we added a variable called warnings with a value of 0. You can add more variables by separating them with a comma.

    17
    client.variables({
    18
    warnings: 0,
    19
    kicks: 0,
    20
    bans: 0
    21
    });
  4. After doing this you should see a startup message including the database type. If you don’t see this message, and you see an error, ensure you’ve followed the steps correctly.

    Picture showing startup message of aoi.db
    Picture showing startup message of aoi.db

Using different Tables

You can use different tables to sort your data even more.

For this we would have to edit your database configuration in our index.js.

  1. Adding more tables, currently you should have only one table called main. To add more tables you can add them as an array in the tables option.

    1
    const { AoiClient } = require("aoi.js");
    2
    3
    const client = new AoiClient({
    4
    token: "Discord Bot Token",
    5
    prefix: "Discord Bot Prefix",
    6
    intents: ["MessageContent", "Guilds", "GuildMessages"],
    7
    events: ["onMessage", "onInteractionCreate"],
    8
    database: {
    9
    type: "aoi.db",
    10
    db: require("@aoijs/aoi.db"),
    11
    dbType: "KeyValue",
    12
    tables: ["main", "moderation"],
    13
    securityKey: "a-32-characters-long-string-here"
    14
    }
    15
    });

    In this example we added a table called moderation. You can add more tables by separating them with a comma.

    12
    tables: ["main", "moderation", "economy"],
  2. Adding Variables to the tables. To assign variables to a specific table you can use the table option in the variables function.

    17
    client.variables({
    18
    warnings: 0,
    19
    kicks: 0,
    20
    bans: 0,
    21
    }, "moderation");

    This would add the variables warnings, kicks and bans to the table moderation.