How to create a Discord Bot : A Trivia Bot for fun!

in programming •  7 years ago  (edited)

Creating a Discord Trivia Bot is as easy as 1, 2, 3, 4 steps plus unlimited fun!

https://steemitimages.com/DQmcfbnQ6sE9ePPEKXUcCnNEnu22GGUe4bsy9XErBEb31hM/discord_bot.jpeg

1. Requirements

2. Getting Started

  • First, you need to create a Discord app. Go here (Make sure you are logged in to your Discord account)
    1.png
  • Configure your App, Set the Name and Image then click on "Create App"
    2.png
  • Congratulations your app has been created. Take note of the Client ID and Client Secret (You will need this in the latter part). Now click on "Create a Bot User" to obviously create a bot for your app.
    3.png
    4.png
  • Congratulations once again! you now have your Discord app! Yay! Take note of the bot token
    5.png
  • Now let's add our bot to a server. From your browser, go to this URL https://discordapp.com/oauth2/authorize?&client_id=YOUR_CLIENT_ID_HERE&scope=bot&permissions=0 replace YOUR_CLIENT_ID_HERE with your own Client ID above. You need to have Manage Server permission to add the bot on the server.
    6.png
    7.png

3. The Code

The fun part starts here. You are the father of this bot so let's go ahead and make him useful.

  • First let's create a folder to store our bot's files. In that folder create a file called package.json. This file will contain package information of our bot node application. Below is a sample package information:
{
  "name": "precise-trivia-bot",
  "version": "1.0.0",
  "description": "PreciseTrivia",
  "main": "precise_trivia_bot.js",
  "author": "@precise",
  "dependencies": {
    "discord.io": "^2.5.3",
    "winston": "^2.3.1"
  }
}
  • Next let's create a file called token.json to store our bot token we got earlier.
{
   "token": "MzYxxxxxxxxxTI5.DK0jyyyyyyyyyyyvCNrQsE8XxI"
}

  • Last but not least, create a file for the main code of our bot. In this example, I have precise_trivia_bot.js - make sure that the same filename is reflected on the main attribute of your package.json file. Place the sample code below:
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./token.json');
// logger settings
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});
bot.on('ready', function (evt) {
    logger.info('Logged in as: ' + bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
    // The bot listens for command messages which we set to start with `!`
    if (message.substring(0, 1) == '!') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            // !start
            case 'start':
                bot.sendMessage({
                    to: channelID,
                    message: 'Hello there <@' + userID +'>'
                });
            break;
            // add any other commands here.
         }
     }
});

4. Run and Deploy

Go to the main folder of your bot and run the main file.
$ node precise_trivia_bot.js

Tap yourself at the back! If you see $ info: Logged in as:.... you have successfully run your bot. Well done champ! Now let's go to our Discord Server and check if our bot is responding. The bot should respond to the command !start as what we have set in the main code above.
8.png


BONUS: This ain't a trivia bot without trivia questions! So here it is in action...
ezgif.com-resize.gif


I code with purpose. I am @precise.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Cool, sounds easy. 😛

Wala syang sounds @zazarina 😂 Walang Sounds!😂😂

Cool, this makes it easy to start with.

and it's fun to make them :)

Nice, make one for our discord pls, Pinoy trivia bot :) yay!

Good idea! let's do it :) parang #silliman lang kung nag IRC ka dati hahaha

As easy as 1, 2, 3...4?
There's only
ONE thing
TWO do ...

THREE words
FOUR you ...

What comes after? Lots of space in the blockchain o. Hahaha

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by dreamiely from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

This post has received a 0.78 % upvote from @drotto thanks to: @banjo.

Congratulations @precise! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Cool.. I'll try it later or soon as possible 😁
By the way @precise, Congrats