Create your own Discord Bot

Warning
This article was last updated on 02-05-2022, the content may be out of date.

I’ve used Discord since I found Nishiro Fansub, because they usually work and chat in Discord. Since then, it has become the most messaging application of mine.

Some of the things that I find personally useful with Discord:

  • It’s Free (fuck nitro)
  • Ease of communication (Instant messaging)
  • Moderated chats (permissions)
  • Text & voice capabilities
  • Chat bots
  • Send file attachments
  • Add friends/colleagues
  • Streaming platform integration

Well, enough for intro

First, you have to install Nodejs(lastest) because Discord.js library always update.

In Linux:

1
2
sudo pacman -S nodejs # (Arch-Distros based)
sudo apt install nodejs # (Debian based)

In Windows:

Go to this link and dowload the lastest Nodejs version.

Now you’ll need to create an application on Discord to make your bot work. The goal here is to get an authorization token for the bot so that Discord recognizes your code and adds it to the bot on its servers.

First, go to this link. Your account should be logged in, so you’ll go straight to your account’s list of applications. Hit New Application to get started.

/images/discord_bot1.png

Give the bot a name, then hit the button Create to Create New Application.

/images/discord_bot2.png

Now look in left-navigation, you will see a tab called “Bot”, click there and hit the button Add Bot

/images/discord_bot4.png

After create bot, you will see Token section, click Reset Token and you’ll reveal a string of text. That’s your bot’s authorization token, which allows you to send it code. Don’t share it with anyone — that token allows whoever has it to create code for the bot, which means whoever has it can control your bot. Make sure you saved it to use later.

Now, in the left-navigation again, in OAuth2 tab, you will see URL Generator, hit there and tick the bot box, it’ll create a link to invite your bot into your own server, feel free to set Bot Permissions.

The final URL should look like this, but with your client ID number in it instead of this fake one: https://discordapp.com/oauth2/authorize?&client_id=000000000000000001&scope=bot&permissions=8

Copy the URL with your client ID number in it into your browser. That’ll take you to a website where you can tell Discord where to send your bot. You’ll know it worked if you open Discord in an app or your browser and navigate to your server. The channel will say a bot has joined the room, and you’ll see it on the right side menu under the list of members.

While you’re doing that, you can also take a moment to create a folder in an easy-to-reach place on your computer where you can store all your bot’s files. Call it something simple, like “DiscordBot” or “MyBot."

Now, just dowload Discord.js library.

1
npm install discord.js # Make sure you open terminal in your Bot Folder

First, create a new file called “config.json” to save the bot token

1
2
3
{
    "token": "your-token-goes-here"
}

Then create a new file to code your Bot. Maybe index.js, but you may name it whatever you wish.

Base code example (Click the arrow to see the code):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const { blob } = require('stream/consumers');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });


// When the client is ready, run this code (only once)
client.once('ready', () => {
	console.log('Ready!');
});

// Login to Discord with your client's token
client.login(token);

Now, open your terminal and run node index.js to start the process. If you see “Ready!” after a few seconds, you’re good to go!

  • Call some one code
1
2
3
4
5
client.on('message', msg => {
    if (msg.content.includes('name')) {
      msg.channel.send("Hey <@user_you_want_to_call_id>! <@" + msg.author.id + "> call you ><");
    }
  });
  • Ask bot time
1
2
3
4
5
6
client.on('message', msg => {
  if (msg.content.includes('mấy giờ')) {
    final CurrentDate = new Date();
    msg.channel.send("Dạ bây giờ là " + String(CurrentDate.toLocaleTimeString()) + " ạ");
  }
});
  • Set activity
1
2
3
client.on('ready', () => {
  client.user.setActivity("with Nải");  // Playing with Nải
});

Just contact me if you get any trouble.