Creating A Moderator Bot using Node.js

thejavascripter

SauhardoSengupta

Posted on April 23, 2022

Creating A Moderator Bot using Node.js

Whether it's creating a discord gaming server or a community server you need some type of moderator to maintain and keep control of the chaos. This might be in the form of blocking inappropriate text or images which can be a big problem if the server is for children.

so, the question is how do we do it? there are two ways to do this

1) hire a moderator and pay them
2) create a bot that can do all of the same things if the better and the cost is $0

let's do option 2 it's much more effective and cost-efficient

Introduction

so we are going to use node.js with the discord.js library to interact with discord API

so let's first initialize a package.json

npm init

running this command will ask a bunch of questions for the project, so enter the correct details for the project and let's get into the next step

create a javascript file called index.js and in the package.json add the following to the scripts

   "scripts": {
      start: 'node index.js'
    },
Enter fullscreen mode Exit fullscreen mode

now, let's install the necessary libraries


npm i discord.js --save

this is going to be for development so install it as a dev dependency.


npm i nodemon --save-dev

now let's add another development script to the scripts in package.json

"scripts": {
    dev: 'nodemon index.js'
 },
Enter fullscreen mode Exit fullscreen mode

Let's Get Started

in the index.js file import the discord library to use it in our codebase

const discord = require('discord.js');
Enter fullscreen mode Exit fullscreen mode

then let's create a constant called client which will contain the discord client object which represents the bot.

const client = new discord.Client({
  intents: [
    Intents.FLAGS.GUILDS, 
    Intents.FLAGS.GUILD_MESSAGES
  ]
})
Enter fullscreen mode Exit fullscreen mode

whenever someone sends a message or when the bot joins it's called an event.

user1 ==> sends message ==> event sent
each event has a name that represents its function like onMesssage and ready which only runs when a user send's a message or when the bot joins!.

We can also give a callback function that runs when the event is sent.

We can look if someone has posted something by using the onMessage event which will run when someone sends a message with all the details of the message sent

//logging when ready
client.on('ready', () => {
  console.log('🤖🤖 Bot is ready to login');
  console.log(`Logged in as ${client.user.tag}!`);
});
Enter fullscreen mode Exit fullscreen mode

we, need to install the bad-words package which will detect bad words in the messages

npm i bad-words

const Filter = require('bad-words')

client.on('messageCreate', async (message) => {
  const filter = new Filter();

  if (filter.isProfane(message.content)) {
    message.delete();
    message.channel.send(
      `${message.author.username} you are not allowed to use that 
       word.`
    );
  }
})
Enter fullscreen mode Exit fullscreen mode

now we have the get the discord token and use this line of code to initialize our bot

client.login(process.env.DISCORD_TOKEN);

to add nudity detection in images we can use this API.

Content Moderation and Image Analysis

Powerful APIs to assess, filter and moderate photos, videos and texts. Instantly detect nudity, violence, offensive content with our easy-to-use API, at a fraction of the cost of human moderation

favicon sightengine.com

click on login if you have an account or get started if you don't have an account enter your email and password

go to the API key's section and copy the API use token and the API secret

also, install Axios to send and fetch data for the NSFW verification
npm i axios

after that's done let's do a request...
now here we need to pass in a URL to send it for review

axios.get(
    'https://api.sightengine.com/1.0/check.json',
    {
      params: {
        url,
        models: 'nudity,wad,gore',
        api_user: API_USER,
        api_secret: API_SECRET,
      },
    }
  );
Enter fullscreen mode Exit fullscreen mode

to get the user's attachment file let's see if the user has one
and then extract the URL parameter from the message which contains the image sent by the user

client.on('messageCreate', async (message) => {
  //checking if the user has a attachmnet
  if (message.attachments.size > 0) {
     //send the picture url for review using the api
    //message.attachments.first().url --> url of the image sent by the user

    const { data } = await axios.get(
    'https://api.sightengine.com/1.0/check.json',
    {
      params: {
        message.attachments.first().url,
        models: 'nudity,wad,gore',
        api_user: API_USER,
        api_secret: API_SECRET,
      },
    }
  );
  }
})
Enter fullscreen mode Exit fullscreen mode

insert this code inside the if condition to delete the message and send an alert message

  if (
        data.weapon > 0.01 ||
        data.alcohol > 0.1 ||
        data.gore.prob > 0.1 ||
        data.nudity.safe < 0.9
      ) {
        message.channel.send(
          `${message.author} Please do not post nudity or gore content.`
        );

        message.delete()
       }
Enter fullscreen mode Exit fullscreen mode

finally its ready! run npm run dev to see the results and all the messages that are inappropriate video messages etc.

signing off...

PROMOTION
want to support me in making these blogs?

subscribe to my channel
https://www.youtube.com/channel/UCVN9qpxbrJ9qMPjpZf3ywpA

follow me on GitHub
https://github.com/sauhardo2020

💖 💪 🙅 🚩
thejavascripter
SauhardoSengupta

Posted on April 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Creating A Moderator Bot using Node.js
javascript Creating A Moderator Bot using Node.js

April 23, 2022