Learn to Code via Tutorials on Repl.it!

← Back to all posts
Discord.JS Set Prefix Command Example (With Replit Database)
[deleted]

Discord.JS Set Prefix Command Example

First of all, I want you to know that the structure of this example is based on this template, and that you will find the tutorial on how to do it here

Also, as it is an exclusive tutorial for replit, we will use its database, you can install it with the command npm i @replit/database from the shell, or use the table that is in the Replit IDE

Ready, now we will create a command file, called set-prefix.js, as our template allows subfolders, we will put it inside the config folder

We start our file with the structure that the template gives us for the commands, and we will import the replit database

Now we will create a new database with the name prefixes

The first thing we do in our run function, will be to check if the args in position 0 exist (remember that the args is all that is put after the command), if it exists, we will create the database from that prefix If it does not exist, it will send an error message

Notice how we use the set function, from the replit database, we put the id of the server where the message comes from as the key to access the value, which will be the prefix provided.

Now we will go to our event file, message.js, and see what there is

Our prefix variable, we will eliminate it, we will create a getPrefix variable, which will be the get function of our database and as a parameter the id of the message server, then, it will return a promise, we process it with then and return the value which gives us that function, then getPrefix will have that value. We create another variable, this time yes, it will be called prefix, and it will receive the value of getPrefix, and if that value does not exist or is undefined, it will receive the value of a default prefix.

Now let's test how it works

It works! I hope it has helped you to start using the replit database, remember well about the promises when getting the prefix.

Voters
KogutGaming