Warn System For Your Discord Bot 1

Warn System For Your Discord Bot

Warn System For Your Discord Bot – Sometime you get pissed off because of some people who broke rules after telling them not to, so this warn system come in handy and make your server good and nice server because when everyone obeys rules then nothing will get worst. Creating a warn system is a piece of a cake if you have some knowledge of discord.js and if not then offcourse you will think its hard. Thats why i am creating this post or you can say guide to make you guys understand how to create a Warn System For Your Discord Bot.

Let’s Create Warn System For Your Discord Bot

We will be using quick.db to store warnings of the members, You can use any other database if you understand the working of commands.

First we will create a warn command and add permission requirement of ADMINISTRATOR perms. After that let the user as mentioned user and check if he provided user or not

if(!message.member.hasPermission("ADMINISTRATOR")) {
      return message.channel.send("You should have admin perms to use this command!")
    }
    
    const user = message.mentions.members.first()
    
     if(!user) {
      return message.channel.send("Please Mention the person to who you want to warn - warn @mention <reaosn>")
    }

Now we will make sure author do not mention any bot to warn because it’s useless to warn a bot because they are not human

   if(message.mentions.users.first().bot) {
      return message.channel.send("You can not warn bots")
    }

What if author mentioned himself then what?, Thats why we will add if statement to check user and author id

 if(message.author.id === user.id) {
      return message.channel.send("You can not warn yourself")
    }

It will not be fair if a staff warn the server owner, so we will make sure that no one can warn server owner.

    if(message.author.id === message.guild.owner.id) {
      return message.channel.send("You jerk, how you can warn server owner -_-")
    }

Now define the reason using argument and make sure you slice first argument because author will mention member in first argument. After defining reason make sure author gives reason for warn so add if statement.

const reason = args.slice(1).join(" ")

  if(!reason) {
      return message.channel.send("Please provide reason to warn - warn @mention <reason>")
    }

We need to check the warnings of mentioned member and for that we need to define warnings

 let warnings = db.get(`warnings_${message.guild.id}_${user.id}`)

After defining, check if the warnings count is equal to 3 then return message to channel that he reached his limit of warns

if(warnings === 3) {
      return message.channel.send(`${message.mentions.users.first().username} already reached his/her limit with 3 warnings`)
    }

Now we will check the warnings and if they equal to null then use db.set and set warning to 1 and send message to channel and mentioned member about his warn.

 if(warnings === null) {
      db.set(`warnings_${message.guild.id}_${user.id}`, 1)
      user.send(`You have been warned in **${message.guild.name}** for ${reason}`)
      await message.channel.send(`You warned **${message.mentions.users.first().username}** for ${reason}`)//DO NOT FORGET TO USE ASYNC FUNCTION
    }

Else if warnings is not equal to null and if warnings is not equal to null then use db.add to add 1

else if(warnings !== null) {
        db.add(`warnings_${message.guild.id}_${user.id}`, 1)
       user.send(`You have been warned in **${message.guild.name}** for ${reason}`)
      await message.channel.send(`You warned **${message.mentions.users.first().username}** for ${reason}`) //DO NOT FORGET TO USE ASYNC FUNCTION
    }

Now we are done with warn command

Warn System For Your Discord Bot

Warn Count Command

What is warn Count? – It is the part of warn system, it will give count of anyone.

First let the user as mentioned person or author and let the warnings also.

const user = message.mentions.members.first() || message.author
let warnings = db.get(`warnings_${message.guild.id}_${user.id}`)

Check if warnings is null then change warnings variable to 0

 if(warnings === null) warnings = 0;

Now send the warning count with name of user

 message.channel.send(`${user} have **${warnings}** warning(s)`)

We are done with warn count command

Warn System For Your Discord Bot

Reset Warn Command

this command will be used to reset all the warnings of mentioned member.

Add required Permission to use this command which will ADMINISTRATOR and define the user. For making sure author mention user, we will add if statement. Also make sure mentioned user should not be a bot.

    if(!message.member.hasPermission("ADMINISTRATOR")) {
      return message.channel.send("Yopu should have admin perms to use this command")
    }
    
    const user = message.mentions.members.first()
    
    if(!user) {
    return message.channel.send("Please mention the person whose warning you want to reset")
    }
    
    if(message.mentions.users.first().bot) {
      return message.channel.send("Bot are not allowed to have warnings")
    }

Again make sure that author does not mention himself and After that define warnings

 if(message.author.id === user.id) {
      return message.channel.send("You are not allowed to reset your warnings")
    }
    
    let warnings = db.get(`warnings_${message.guild.id}_${user.id}`)
    

If warnings is equal to null then return message that mentioned member not really have any warnings.

 if(warnings === null) {
      return message.channel.send(`${message.mentions.users.first().username} do not have any warnings`)
    }

Now use db.delete to remove all the warnings, actually it will delete whole key so it is same as reset and after return message to user and channel.

 db.delete(`warnings_${message.guild.id}_${user.id}`)
    user.send(`Your all warnings are reseted by ${message.author.username} from ${message.guild.name}`)
    await message.channel.send(`Reseted all warnings of ${message.mentions.users.first().username}`) //DO NOT FORGET TO USE ASYNC FUNCTION
    

You are done with warn reset command and warn system

Warn System For Your Discord Bot 2

Video Tutorial For Warn System

Source Code

Here is the resulting code of whole post – https://github.com/CTK-WARRIOR/Guide-discord.js-v12/tree/master/Day-18/commands

OwO We have created Warn System For Your Discord Bot

Also Read: Guide To Create Anti Links System For a Discord Bot

2 Comments

Leave a Reply

Leave a Reply

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