Skip to content

Instantly share code, notes, and snippets.

@kpunith8
Last active March 1, 2021 14:14
Show Gist options
  • Save kpunith8/babf619aa291fd4dec1fb2647fe56ee8 to your computer and use it in GitHub Desktop.
Save kpunith8/babf619aa291fd4dec1fb2647fe56ee8 to your computer and use it in GitHub Desktop.
NodeJS Discord Bot

Creating a BOT in Discord

  • Create an application https://discord.com/developers/applications/

  • Once an app created, go to Bot section and add one bot with a type bot

  • Once Bot created copy the Token which is a secret and please don't share it with anyone, needed to connect to the Bot using API

  • Authorize the bot to be attached to your discord server, make sure you have created a Server in https://discord.com

  • Select OAuth2 option, under Scopes section select bot, it brings another section with Bot Permissions. Make sure to select the right permissions, don't give all the permissions, check all permissions under Text Permissions excluding Send TTS Messages.

Once required permissions are selected, copy the URL above the Bot permissions section, which looks similar to this, https://discord.com/api/oauth2/authorize?client_id=&permissions=4096&scope=bot and open it in another window and select your server name and authorize it.

  • Once authorized, Bot will be listed in your server, and it will be offline, to make it work we need to create a server

Create a nodejs server in Repl.it

  • Create a NodeJS Repl

  • Install dotenv and discord.js packages and create a client and login.

  • Add this code to index.js

require('dotenv').config()
const express = require('express')
const cors = require('cors')

const app = express();
const PORT = process.env.PORT || 3000;

app.use(cors());

const Discord = require('discord.js');
const client = new Discord.Client();
// Bot treats the strings with $ prefixed otherwise bot
// will process all the messages matching the string
const PREFIX = '$';

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  const message = msg.content;
  // Don't reply if bot sending the messages
  if(msg.author.bot) return;

  // Check for special prefix $ to reply
  if(message.startsWith(PREFIX)) {
    const [CMD_NAME, ...args] = message.trim().substring(PREFIX.length).split(/\s+/)

    console.log(CMD_NAME, args)

    if(CMD_NAME === 'welcome') {
      msg.channel.send('Welcome to the channel :tada:')
    }
  }
});

app.get('/', (req, res) => {
  res.send(`Hello there! I'm alive`);
})

client.login(process.env.DISCORD_BOT_TOKEN);

app.listen(PORT, (err) => {
	if(err) console.error(err);
	console.log(`Listening on ${PORT}`);
});
  • Go to discord channel where you added this bot and send the text ping, bot replies with the message pong

  • Create an express server and keep the repl server running (by default, it goes offlive after 60 mins of inactivity), so that the bot doesn't go to offline mode once the tab was closed. Once the express server started, repl provides the URL similar to https://Discord.kpunith8.repl.co and use this URL to ping from https://uptimerobot.com/, the service https://uptimerobot.com/ will ping the server every 5 minutes for free accounts to keep the Repl alive.

  • Code is deployed in heroku https://github.com/kpunith8/Discord-Quotes-Bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment