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.
-
Edit your index.js, if you haven’t already. It should look like the following, and have a
database
option in the constructor. -
(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! -
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 useclient.variables()
.In this example we added a variable called
warnings
with a value of0
. You can add more variables by separating them with a comma. -
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
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.
-
Adding more tables, currently you should have only one table called
main
. To add more tables you can add them as an array in thetables
option.In this example we added a table called
moderation
. You can add more tables by separating them with a comma. -
Adding Variables to the tables. To assign variables to a specific table you can use the
table
option in thevariables
function.This would add the variables
warnings
,kicks
andbans
to the tablemoderation
.