How to Make a bot in Discord
Discord has an excellent API for writing custom bots and a very active bot community. Today we look at how to create your own.You need a bit of programming knowledge to program a bot. So this is not for everyone. Luckily there are some modules for common languages that make programming very easy. We use the most popular, discord.js.
Go to Discord’s bot portal and create a new application.
You want to write down the customer number and the secret (which of course you should keep secret). However, this is not the bot, just the “application”. You need to add the bot on the bot tab.
Make a note of this token and keep it secret. Under no circumstances transfer this key to Github. Your bot is hacked almost immediately.
Install Node.js and get the encoding
To execute Java script code outside of a web page, you need Node. Download it, install it and make sure it works in a terminal (or a command prompt as it all should work on Windows systems). The default command is “node”.
We also recommend installing the nodemon tool. It’s a command-line app that monitors your bot’s code and automatically restarts when it’s changed. You can install it by running the following command:
npm i -g nodemon
You need a text editor. You could just use the editor, but we recommend either Atom or VSC.
Here is our “hello world”:
This code is from the example discord.js. Let us break it down.
- The first two lines configure the client. Line one imports the module into an object called “Discord” and line two initializes the client object.
- The client.on (‘ready’) block is fired when the bot starts. Here it is only configured so that its name is logged in the terminal.
- The client.on (‘message’) The block is fired each time a new message is sent to a channel. Of course you have to check the message content if block does. If the message is just ping, she answers with “Pong!”
- The last line logs in with the token from the bot portal. Obviously, the token in the screenshot is a fake. Never put your token on the Internet.
Copy this code, paste your token below and save it in index.js in a special folder.
How to execute the bot
Go to your terminal and run the following command:
nodemon –inspect index.js
This launches the script and launches the Chrome Debugger, which you can access by typing chrome: // inspect / into Chrome’s Omnibar, then open “dedicated devtools for Node”.
Now it should just be “logged in as”, but here I added a line that logs all message objects received at the console:
What is this message object? A lot of stuff, actually:
Most importantly, you have the author information and channel information that you can access with msg.author and msg.channel. I recommend this method to log objects in the Chrome Node devtools and just look around to see how it works. Maybe you find something interesting. In this example, the bot logs its responses in the console so that the bot’s responses are raised client.on (‘message’). So I made a spambot:
To add the bot to your server
This part is more difficult than it should be. You must take this URL:
Replace CLIENTID with the client ID of your bot found on the General Information tab of the application page. Once that’s done, you can give your friends the link to add the bot to their servers as well.
Beyond the basic attitude, everything else is completely up to you. This would not be a real tutorial if we were at Hello World. So let’s go over some of the documentation so you have a better idea of what’s possible. I recommend that you read as much as possible as this is very well documented.
I would recommend adding console.log (client) Go to the top of your code and take a look at the client object in the console:
From here you can learn a lot. Because you can add a bot to multiple servers at the same time, servers are part of the Guilds map object. Within this object are the individual guilds (the API name for “servers”) and these guild objects have channel lists that contain all the information and lists of messages. The API is very extensive and learning can take a while. But at least it’s easy to set it up and start learning.