Create Welcome Command Using guildMemberAdd event and quick.db in discord.js 1

Create Welcome Command Using guildMemberAdd event and quick.db in discord.js

we will be storing channel id by using quick.db, if you have any thoughts like why i am not using mongo and other things then i must say i just like quick.db because it is simple to use.

i will be using my command handler and if you do not know how to create command handler than you should check this post – COMMAND HANDLER

First of all we will be creating setwelcome command which will be setting welcome channel id in server variable

const Discord = require("discord.js")
const db = require("quick.db")

module.exports = {
  name: "setwelcome",
  category: "moderation",
  usage: "setwelcome <#channel>",
  description: "Set the welcome channel",
  run: (client, message, args) => {
    
    let channel = message.mentions.channels.first() //mentioned channel
    
    if(!channel) { //if channel is not mentioned
      return message.channel.send("Please Mention the channel first")
    }
    
    //Now we gonna use quick.db
    
    db.set(`welchannel_${message.guild.id}`, channel.id) //set id in var
    
    message.channel.send(`Welcome Channel is seted as ${channel}`) //send success message
  }
}

Now we will head to main file, in my case my main file name is server.js and i will be using guildMemberAdd event, when member joins it will triggered in seted channel.

const db = require("quick.db") //using quick.db package

client.on("guildMemberAdd", (member) => { //usage of welcome event
  let chx = db.get(`welchannel_${member.guild.id}`); //defining var
  
  if(chx === null) { //check if var have value or not
    return;
  }

  let wembed = new discord.MessageEmbed() //define embed
  .setAuthor(member.user.username, member.user.avatarURL())
  .setColor("#ff2050")
  .setThumbnail(member.user.avatarURL())
  .setDescription(`We are very happy to have you in our server`);
  
  client.channels.cache.get(chx).send(wembed) //get channel and send embed
})

Resulting Code

Here is the resulting code – HERE

Related Posts

One Comment

Leave a Reply

    Leave a Reply

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