Discord Bots : How To Make a Discord Bot
If you are a frequent online media consumer, then you probably are aware of what Discord is. To put it simply, it is a platform wherein you join servers to talk to the people you want regarding a certain topic or issue. Discord was originally created for the video gaming community, so that online video gaming icons and professionals could interact with each other and their fans by creating a designated server. But over time, discord is now widely used in non-gaming communities such as education, lifestyle, therapy, food, etc.so people from all walks of life with different interests can talk and share with each other.
If you are looking to run discord servers yourself, it is probably a good idea to have discord bots. What are they? Discord bots are like automated entities in your servers who can do tasks like greet new people who join, play songs, send emoticons, congratulate your special visitors, censor out negative words and users, etc. Creating a discord bot might seem difficult because they are coded in JavaScript, and that might scare off a lot of people, butit is a lot easier than you might think. You will need a bit of programming language knowledge, but there some popular modules which you can use to make abot in discord pretty easily. For the purpose ofthis tutorial, we will be concentrating on the discord.js module.
Read on to the steps which will show you how to create a discord bot.
How to create a discord account?
If you haven’t already, you obviously need to create a discord account first. You need to visit this portal. Then enter your email address, your desired username, and your desired password. Once you are satisfied then press ‘Continue.’ You will receive an email which you must verify, and you can start using your discord account immediately.
How to download Node.js module?
Node.js is free and an opensource module. You can get it from here.Once you have downloaded it, install it, and you are almost ready to start creating your own discord bots.
Creating a discord bot
Now you are all set to create your own bot on discord. You will need an editor like notepad which should be easily available. Now visit the discord bot portal. Here is where you will have to create an app to make your bot work. Your primary goal here is to get an ‘authorization token’ for your bot so that Discord recognizes the code you write and adds it to the bots on the servers.
When the portal is open, select ‘New App.’ Now give your bot a name and select the button ‘Save Changes.’ Be sure to make a note of the Client ID and Client Secret for future references. You shouldn’t disclose the Client Secret because it is a secret!!
Now you have to add your bot.Click on the ‘Bot’ menu and select ‘Add bot.’Select the application you made and add it. Look for the option ‘Click to Reveal Token’. Click on it, and you will receive a special token. Make a note of this and do not under any circumstances reveal it to anyone else, especially over the internet. If you do, then it is highly likely your discord server will get hacked and misused.
We are finally ready to start coding. It is recommended you install nodemon tool.It’s a command line app that can instantly restart on changes and also does a fantastic job of monitoring your bot’s code. You can use this command to install it:
npm i -g nodemon
Let us now look at a sample code.
const Discord = require(‘discord.js’);
const client = new Discord.Client();
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’);
You can find other sample code from here.
Now let us see how the code above can be broken down.
1. The first two lines are to configure the client. You can import the module into an object c through line one which is also called“Discord,”. You can also initialize the client object through line two.
2. The client.on(‘ready’) block will fire when the bot starts up. Here, it’s configuration is to log its name to the terminal.
3. The client.on(‘message’) block will fire every time a new message is posted to any channel. It’s important that you check the content of the message, and that’s what the if block does. If the message initially means “ping,” then you will get a reply as “Pong!”
4. The last line logs in with the token from the bot portal. Don’t take it seriously because the token is fake. Never ever post your token on the internet.
Create a dedicated folder, paste in your app token and save it as index.js (name).
Running Your Discord Bot To Server
Open your terminal and run the following command.
nodemon --inspectindex.js
This will start the script and also the Chrome Debugger, which you can access by typing chrome://inspect/into Chrome’s Omnibar and then opening “dedicated devtools for Node.”
Now, it should say “Logged in as<bot-name>,” but a line has been added that log all message objects received to the console.
Now you can see a lot going on, and we will not go into the nitty-gritty. But you should notice the author info and the channel info, which you can access with msg.author andmsg.channel. This way you can add objects to the Chrome Node devtools, and just looking around to see what makes it work.
How to add your bot to your server?
Copy the URL below.