DEV Community

Shriji
Shriji

Posted on

How to create Chuck Norris facts bot on Discord

Discord is awesome one might compare the software as Slack for gamers. Discord also has as many concurrent users as slack. I am going to teach you how to make a Chuck Norris bot. This could be a basic boilerplate to fetch an API to do something on your discord channel.

Prerequisites:

  1/ Roast/Insult API (https://api.chucknorris.io/jokes/random).
  2/ NodeJS. (discord.js, good old node-fetch and dotenv).
  3/ Discord Account (https://discord.com/developers/applications).
Enter fullscreen mode Exit fullscreen mode

Setting up Discord App and bot

  • Create a new application
  • Fill some basic information and save your application Alt Text
  • You will find option called Bot there you can add a bot user
  • Have your token saved somewhere for now and later add it to your .env
  • Create your server for testing the bot
  • Back to your dev portal go to OAuth2 section and select bot and you will get the scope of bot permissions, in our case it's just text Alt Text
  • Go to the URL and authorize on your server and bot will be added to the server. Alt Text

Please follow the images if you are stuck somewhere.

Okay now you have completed 50%, congrats! Now let's begin coding install discord.js and dotenv. Create your .env file and place your token and NEVER COMMIT YOUR .ENV FILE!

The following steps are easy. This is a small modification of the example code at discordjs

require('dotenv').config();
const Discord = require('discord.js');
const client = new Discord.Client();
const fetch = require("node-fetch");

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

client.on('message', msg => {
    if (msg.content === 'ping') {
        msg.reply('pong');
    }
});

client.login(TOKEN);
Enter fullscreen mode Exit fullscreen mode

After you run this code then you should be able to get a reply and also tag you along with message pong when you send ping.
Alt Text

Awesome work so far, now let’s use node-fetch to obtain some Chuck Norris facts from the API.

client.on('message', async msg => {
    if (msg.content === '?fact') {
        const response = await fetch("https://api.chucknorris.io/jokes/random");
        const fact = await response.json();
        let r = fact.value;
        msg.reply(r)
    }
});
Enter fullscreen mode Exit fullscreen mode

Also its good practice to select a prefix in front of your commands I am using ?(question mark) before the keyword fact and it triggers the condition to get a random fact.

Alt Text

Now you have successfully made your own bot, I have also done a roast/insult bot that fetches a random bad word from an array of Tamil and Hindi abuses and it's more fun when the bot insults your friend when they do something silly in the game. Our bot now runs on digitalocean with PM2

Here are some of the ideas that you can do for your bot.

This could be your weekend project to cheer up your gang on the discord server.
Here is the repo of this post.
https://github.com/peopledrivemecrazy/Chuck-Norris-Bot/

Enjoy.

Top comments (0)