Prefix
Example of creating a command to assign a new prefix to the bot for each guild.
Creating the table
First, we will generate a blank table file that will represent the configuration of each guild.
Your new file is located in src/tables/guild.ts
. You must now open the file and add all the properties you want to have in this table in order to configure each guild independently.
For our example we are only going to add the _id
column for incrementation and references, the id
column for correspondence with the guild snowflake on Discord, and finally the prefix
column.
Creating the prefix getter in a namespace
To access the prefix of each guild easily without having to repeat the same operations several times, you must create a namespace which will contain the getGuildPrefix
function. Call the namespace whatever you want, in our example we will call it tools.ts
.
Your new file is located in src/namespaces/tools.ts
. You must now open the file, import the created table and create the getGuildPrefix
function. The function should return the bot's default prefix if the guild does not have a custom prefix.
Tell the system how to access the guild prefix
In the src/config.ts
file, add the getPrefix
option and make it use the getGuildPrefix
function of the namespace you just created.
Now the system will know the individual guild prefix, it will be able to use it in the help menu and for the use of text commands.
Creating the command
Next, we will generate a blank command file with the name "prefix".
Your new file is located in src/commands/prefix.ts
. Now you can open the file and import the previously created table into it.
You will then define the properties of the command to prevent anyone from being able to change the prefix without authorization and add a small descriptionm.
Now you can ask the user to attach a prefix if they want to change it. We validate it using a regex to avoid unpleasant surprises.
We can finally write the body of the command in order to make it work by changing the value in the database if a valid prefix has been transmitted. Otherwise, we display the current bot prefix for this guild.
Usage
It's done! you can now use your prefix command this way.
Last updated