Discord Greeter Bot

When discussing client needs on Discord, you may want to set up a private channel for each client, so they can discuss their needs with staff without other clients snooping on them. Towards this end, I wrote a bot that adds a consultation room when anyone joins the server.


const { Client, Intents } = require("discord.js");
const client = new Client({
  intents: ['GUILD_PRESENCES', 'GUILD_MEMBERS', "GUILDS", "GUILD_MESSAGES"],
  partials: ['CHANNEL', 'MESSAGE']
});
var nStore = require('nstore');
var lasttag="";
const token = process.env.DISCORD_BOT_SECRET;
client.on('ready', () => {
  console.log("I'm in");
  console.log(client.user.username);
});
function setPerm(room, member) {
  room.permissionOverwrites.edit(member, {
    VIEW_CHANNEL: true,
    SEND_MESSAGES: true,
    READ_MESSAGE_HISTORY: true,
  });
}
client.on('guildMemberAdd', member => {
  let tag=member.user.tag;
  console.log('User @' + tag + ' has joined the server!');
  if ( tag === lasttag ) {
    //Stop discord spamming the same event twice.
    return;
  }
  lasttag=tag;
  let category = member.guild.channels.cache.find(c => c.type === "GUILD_CATEGORY" && c.name.toUpperCase().includes("CONSULTATION ROOMS"));
  let reception = member.guild.channels.cache.find(c => c.name.toUpperCase() === "RECEPTION");
  let shortname=tag.replace(/#.*/g, '');
  let shortnamelc=shortname.toLowerCase();
  tag = tag.replace(/#/g, '').toLowerCase();
  users.get(shortnamelc, function (err, doc, key) {
    try {
      let room = "";
      if (err) { 
        users.save(shortnamelc, tag, function (err) {
        if (err) { throw err; }
	  // The save is finished and written to disk safely
          // Maybe should rewrite so the rest of the code goes in here.
	});
        tag=shortnamelc;
      } else {
        if (tag===doc) {
          tag=shortnamelc;
	}
        room = member.guild.channels.cache.find(c => c.name === tag);
      }	
      if (room) {
        setPerm(room, member);
        room.send(`<@${member.id}>`+" Welcome Back!");
      } else {
        member.guild.channels.create(tag, {
          type: "GUILD_TEXT",
          parent: category,
        }).then(room => {
          setPerm(room, member);
          room.send(`<@${member.id}>`+"Welcome to USIA! \n\nThis is your private channel where you can discuss your needs with our staff. They should be along shortly. While you wait, our bots can provide information! \n\nType the following commands into chat and press enter (! followed by a word). Choose whichever looks closest to what you are seeking. \n\n    !process         -  The bot will explain how our service works, and the steps involved. \n    !skillboard      -  The bot will explain how to share your skillboard with us, a vital step in us raising any standings. \n    !broker            -  The bot will explain more about broker fees and reducing them. \n    !agents           -   The bot will explain about raising corporation standing, or unlocking agents (for example, Level 4 Security Missions for Caldari Navy) \n    !faction:          - The bot will explain more about how you can, or how we can, raise your faction standings.\n    !jobs                - If you are interested in working for USIA.\n\nIf none of these look helpful to you, please just @ Imiarr. They will respond as soon as they are available.");
          reception.send("Hi "+ shortname + ". I have created a private Consultation Room for you.");
        });
      }
    } catch(error) {
      console.log("Could not create channel:" + error);
      reception.send("I failed to create a consultation room for " + member.user.tag + "! Halp! Halp!").catch(error => {
        console.log("Could not post to reception:" + error);
      });
    }
  });
});
console.log("Starting...");
var users = nStore.new('users.db', function () {
  client.login(token);
});

Because the client now has a private room, you can get them chatting with another bot, like Dyno

. You can try out this bot at https://discord.gg/vhCDd9kj. Here is the package.json file you will need.


{
  "name": "nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^18.0.6",
    "discord.js": "^13.9",
    "node-fetch": "^3.2.6",
    "nstore": "^0.5.2"
  }
}

Comments

Leave a Reply

Your email address will not be published.