How to create Chuck Norris facts bot on Discord
Tags: javascript, tutorial
Published: 2020-06-06 11:11:44

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).


## Setting up Discord App and bot
* Create a new application
* Fill some basic information and save your application
   ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/69pju96sstpkf3ukdsjs.png)
* 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](https://dev-to-uploads.s3.amazonaws.com/i/fuqj6xse422hf9t1ldvi.png)
* Go to the URL and authorize on your server and bot will be added to the server.
   ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w76lg1u1fwkcm53nl89y.png)


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](https://github.com/discordjs/discord.js#example-usage)


```js
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);
```

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](https://dev-to-uploads.s3.amazonaws.com/i/58h00hnvcw62ph9wwl2g.png)


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


```js
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)
    }
});
```

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](https://dev-to-uploads.s3.amazonaws.com/i/b2stis705d96rj37ytfr.png)



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](https://m.do.co/c/155941ccdb5d) with PM2


Here are some of the ideas that you can do for your bot.
 * Insult API (https://insult.mattbas.org/api/en/insult.json)
 * Meme API (https://meme-api.herokuapp.com/gimme) please read the docs for sending attachments
 * COVID API (https://coronavirus-19-api.herokuapp.com/countries/)
 * Dad Joke API (https://icanhazdadjoke.com/slack)


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.