Create Verification Bot For Your Discord Server 1

Create Verification Bot For Your Discord Server

Create Verification Bot For Your Discord Server, Well creating a bot is really fun and amazing experience but sometime you sucks in small things because you do not have a single idea about that topic, Now I am here with a new post with new idea and with new bot category of bot, I will be showing how you can create a verification bot very easily from scratch.

I will be telling you guys every single thing of code to make sure you guys understand. Before we start I want to make sure you guys know how to create a Discord Application because if you do not know that, Then you will not able to create a bot, so I suggest to check this post of mine – HERE

First of all open your Code Editor or IDE, You can event use Browser IDE because they are really easy to use and some of them examples are – Glitch.com, Repl.it, Gitpod, etc. I will be using Visual Studio Code but you can use any editor you desire.

If you are not using Browser IDE then make sure to create package.json file, You can create it manually or create it by Command Prompt.

Creating Package.json File By Command Prompt.

1 > Open Command Prompt
2 > Locate your folder where you will be coding , example – cd Desktop/captcha-bot
3 > Now use npm init -y
4 > You are done 🙂

Maker sure you install discord.js package before closing command Prompt by using – npm i discord.js

Create Config.json File

We will create a file for storing prefix and token of the bot and name that file config.json. You can name it whatever you want but naming it config will be better.

Now add token and prefix in that file like this :

{
  "TOKEN" : "YOUR TOKEN",
  "PREFIX": "?"
}

Create Verification Bot For Your Discord Server

Now create file with name index.js as it will be our main file where we will do all the main coding.
After Creating the file, Define discord.js pkg, client, TOKEN, PREFIX and a very useful package to get Captcha Image.

const discord = require("discord.js")
const client = new discord.Client()
const { TOKEN, PREFIX } = require("./config.json")
const Captcha = require("haileybot/captcha-generator")

Create client ready event, so whenever bot get ready then you can perform some action, In my case I will just log message to console.

client.on("ready", () => {
  console.log("MY NAME " + client.user.username + "\n" + "MY PREFIX " + PREFIX)
})

Now we are done with ready event and i think we should move to main thing now. So we will use guildMemberAdd(its a event which callback when someone joins the server) event and give member a role by which he/she will able to only view verify channel and after that bot will send message with code to verification channel and if member give the right code then bot will take role from the member, so he will be able to see other channel. If he gave wrong code then bot will kick him/her in 3 or 5 seconds.

First of all add event and let new captcha for member.

client.on("guildMemberAdd", async (member) => {
  let captcha = new Captcha()
  })

Now Find the channel with name verify and if its not available then return message in console.

client.on("guildMemberAdd", async (member) => {
  let captcha = new Captcha()
  
  const channel = member.guild.channels.cache.find((x) => x.name === "verify")
  
  if(!channel) {
   return console.log(member.guild.name + "Please Create channel with name verify")
  }
  
  })

We will use same way to find a role for non verified member and return message to console if role is not available.

client.on("guildMemberAdd", async (member) => {
  let captcha = new Captcha()
  
  const channel = member.guild.channels.cache.find((x) => x.name === "verify")
  
  if(!channel) {
   return console.log(member.guild.name + " Please Create channel with name verify")
  }
  
  const vrole = member.guild.roles.cache.find((x) => x.name === "non-verified")
  
    if(!vrole) {
   return console.log(member.guild.name + " Please Create role with name 'non-verified'")
  }
  
  })

If everything goes as we planned then bot will give role to member and send message an code to verification channel.

client.on("guildMemberAdd", async (member) => {
  let captcha = new Captcha()
  
  const channel = member.guild.channels.cache.find((x) => x.name === "verify")
  
  if(!channel) {
   return console.log(member.guild.name + " Please Create channel with name verify")
  }
  
  const vrole = member.guild.roles.cache.find((x) => x.name === "non-verified")
  
    if(!vrole) {
   return console.log(member.guild.name + " Please Create role with name 'non-verified'")
  }
  
  member.roles.add(vrole)
  
  const verifycode = await channel.send("Please Type The Given Code For Verification",
               new discord.MessageAttachment(captcha.PNGStream, "captcha.png")
  
  })

Well, we are done with captcha part now we have to take the answer of member and check . If it got correct then delete code and member message and take role from member with sending a message to his/her DM. if it got wrong then delete all message and kick him out after 3 or 5 seconds.

client.on("guildMemberAdd", async (member) => {
  let captcha = new Captcha()
  
  const channel = member.guild.channels.cache.find((x) => x.name === "verify")
  
  if(!channel) {
   return console.log(member.guild.name + " Please Create channel with name verify")
  }
  
  const vrole = member.guild.roles.cache.find((x) => x.name === "non-verified")
  
    if(!vrole) {
   return console.log(member.guild.name + " Please Create role with name 'non-verified'")
  }
  
  member.roles.add(vrole)
  
  const verifycode = await channel.send("Please Type The Given Code For Verification",
               new discord.MessageAttachment(captcha.PNGStream, "captcha.png")
                                        
  let collector = channel.createMessageCollector(m => m.author.id === member.id)
  
  collector.on("collect", m => {
    if(m.content.toUpperCase() === captcha.value) {
     m.delete()
      verifycode.delete()
      member.roles.remove(vrole)
      return member.send("NOW YOU ARE VERIFIED MEMBER :)")
    } else if(m.content.toUpperCase() !== captcha.value) {
      member.send("You gave wrong code, so you can apply again by joining server again")
      verifycode.delete()
      m.delete()
      
      setTimeout(function() {
              member.kick()
      }, 3000)
        
    } else {
      verifycode.delete()
    }
    
  })
  
  })

Now in last

client.login(TOKEN)

Video Tutorial

One Comment

Leave a Reply

Leave a Reply

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