Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
115 lines (77 sloc) 3.03 KB
description
A simple discord bot guide

Basic Discord Bot

Prerequisites

In order to run JavaScript code outside the browser we will use a Node.js. Make sure to install it for the correct operating system.

{% embed url="https://nodejs.org/en/download/" %}

{% hint style="info" %} Use command: "node -v" in your terminal to check that you have successfully installed node.js on your system. {% endhint %}

It is also important that you create a bot account and add the bot to your server.

{% embed url="https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token" caption="You can follow this guide to do so" %}

In order to code we will require a code editor. I highly recommend Visual Studio Code but you may use any that you prefer.

{% embed url="https://code.visualstudio.com/" %}

Setup

Go ahead a create a folder for your bot. We will be creating 2 files. index.js + config.json

In order to communicate with the Discord we will be using a library called discord.js. You can install it with the following command: npm i discord.js

Code

{% code title="index.js" %}

//import modules
const Discord = require('discord.js');
const config = require('./config.json');

//initialize client
const client = new Discord.Client();

//ready event
client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}`);
    client.user.setActivity(`${config.prefix}help`, {type: 'PLAYING'})
});

//message event
client.on('message', message => {
    
    //to reduce code
    var cmd = message.content.slice(prefix.length);

    if (cmd === 'ping') {

        message.channel.send(`Pong! \`${Math.floor(client.ping)}ms\``);

    } else if (cmd ==='help') {
        
        //create new embed using this constructor
        helpEmbed = new Discord.RichEmbed()
        helpEmbed
        .setDescription('Help has arrived!');
        message.channel.send(helpEmbed);

    } else if (cmd === 'reboot') {

        if (message.author.id !== config.ownerId) return;
        message.channel.send('Rebooting...');
        setTimeout(() => {
            process.exit();
        }, 1000);

    };
});

//token is hidden in config.json
client.login(config.token);

{% endcode %}

{% hint style="info" %} The credentials in config.json are for example only. Please replace them with your own. {% endhint %}

{% code title="config.json" %}

{
    "prefix": "$",
    "ownerId": "123456789012345678",
    "token": "iafpiq3n8vi90navrnqwvrv21i.asdasdi3asdsgddag"
}

{% endcode %}

{% hint style="danger" %} Beware that revealing your token to anyone allows them to full access to your bot. {% endhint %}

Run

In order to run the bot use command: node index.js

Links

Here are some links that I personally find helpful with coding discord bots:

{% embed url="https://discord.js.org/\#/" caption="Discord.js Documentation" %}

{% embed url="https://discordjs.guide/" %}

{% embed url="https://anidiots.guide/" %}

{% embed url="https://discordapp.com/invite/bRCvFy9" caption="Great place to get help with discord.js" %}

You can’t perform that action at this time.