Guide To Create Custom Guild Prefix Command For Discord Bot Very Easily 1

Guide To Create Custom Guild Prefix Command For Discord Bot Very Easily

Every famous and good bot have its custom guild prefix and if you want your bot to have this cool feature then follow this guide. I will be using quick.db to store server guild prefix.

First go to main file which is server.js in my case and define quick.db package as we are going to use it to store the guild prefix. Before doing anything go to config file change prefix to default prefix and do same in server.js.

{
  "token": "TOKEN",
  "default_prefix": "!"
}
const { token, default_prefix } = require("./config.json")
const db = require("quick.db")

I will be using my command handle if you do not know how to create it then check out this post – HERE

Now head to message event

 
client.on("message", async message => {
if(!message.guild) return;
  let prefix = db.get(`prefix_${message.guild.id}`)
  if(prefix === null) prefix = default_prefix;
  
  if(!message.content.startsWith(prefix)) return;
  
  //YOUR CODE
})

After this we are done with setup and it’s time to create command to set prefix.

Create command with name prefix, We will make sure while using prefix command author should have admin permissions to change the bot guild prefix.

   if(!message.member.hasPermission("ADMINISTRATOR")) {
      return message.channel.send("You are not allowed or do not have permission to change prefix")
    }

We will see if the author send anything or not to set prefix

  if(!args[0]) {
      return message.channel.send("Please give the prefix that you want to set")
    }

Now we will also make sure author do not give argument more than 1

 if(args[1]) {
      return message.channel.send("You can not set prefix a double argument")
    }

We have to make sure that author does not give prefix, so we will be adding if statement with args[0]

 if(args[0].length > 3) {
      return message.channel.send("You can not send prefix more than 3 characters")
    }

What if author set prefix to default prefix, and we do not want our bot storage to fill out, so we will be deleting prefix if author give default prefix while setting prefix.

 if(args.join("") === default_prefix) {
      db.delete(`prefix_${message.guild.id}`)
     return await message.channel.send("Reseted Prefix ✅")
    }

Now time to send success message.

    db.set(`prefix_${message.guild.id}`, args[0])
  await message.channel.send(`Seted Bot Prefix to ${args[0]}`)

Table of Contents

Result Code

Here is the result code – HERE

One Comment

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *