How to Create a Discord Bot

Today I want to share with you how to create a discord bot. Discord bots have been becoming very popular over the last few months and to be honest I was rather late to jump on the bandwagon, not making my first discord bot until a few weeks ago. Now that I have made one it is easy to see why everyone is going crazy about them, they are not only fun but you can get them to do some pretty cool stuff for you and you friends.

In this guide, we will cover the basics of discord bots as well as how to create a discord bot. After reading this guide, you’ll be able to create your own Discord bot as well as troubleshoot the common errors that you might face while trying to create your bot.

Table of Contents

What Are Discord Bots?

Discord bots are essentially “automated robots” that join your server and respond to events. You’ll see them show up in channels just like a regular user would, with the only real differentiator being their blue “bot” tag.

Your typical Discord bot has some programming/code in the background that tells it what to do when it encounters a trigger you specified. This means that when something happens in Discord, your bot sees it happen and takes action. You get to program that action.

A great example is the ping pong bot we have below. When it’s online, the bot is in my channel and actively listening to all chat messages.

Every time a chat message is sent, the bot gets a little notification from the Discord API saying, “Hey, we have a callback/event! A chat message was sent!”

The bot then reads this message and looks for the word “ping”. If it finds the word ping in the message, it responds saying, “pong!”

Discord Ping Pong Bot in Server

This is a simplified example, but that’s essentially how bots work on the back-end.

Whenever certain events happen in your Discord channel your bot gets notified through the Discord API (Application Programming Interface).

If your bot has code to handle an event, then it goes through and runs that bit of code you associated with the event.
In the ping-pong example, we created code that handles any new chat messages (our event trigger).

This code looks through any new messages for the word ‘ping”, and if it finds it, sends back a chat message through the Discord API into our channel. Our bot officially speaks!

Discord Bots: Behind the Scenes

We talked about how Discord bots work in a general sense, but let’s get into the details a little bit.

Take a close look at the diagram below:

Discord users, people who are connected to your server, are on the left.

The official Discord server and API, maintained and run by Discord itself, is in the middle.

Your bot server is on the right and can be running on either your personal computer or an official hosted server that runs 24/7.

And finally, your bot code also on the right. This is the actual code behind your bot that lets it do what it needs to do.

How Discord Bots Work (API Diagram)

These are the four primary elements of any Discord bot.

Discord users interact in a channel, send messages, and take certain actions.

Your bot (running on your server) then interacts with Discord through their API, figures out what events or triggers happened, and reacts in a pre-programmed way.

How to Create a Discord Bot

Part 1: Preparing Your Computer

  1. Download Node.js from https://nodejs.org/en/download/. Node.js is a free JavaScript runtime that you’ll need to make your bot. You can pick either the Windows or macOS installer as well as which version. The LTS version is recommended for this process.
  2. Run the installer. A Windows computer just needs to click on the downloaded file to launch the installer, but a macOS computer needs to unpack the file to find the installer application. Make sure you read any and all agreements as you go through the installer.
  3. Create a Discord account (optional). If you don’t already have a Discord account, you sign up for one at https://discord.com/.
  4. Login to your Discord account and channel. Open the Discord app on your computer and navigate to the channel you want your bot in.https://ad1ced99437943e46d2fcc9b25383957.safeframe.googlesyndication.com/safeframe/1-0-38/html/container.htmlAdvertisement

Part 2: Creating the Bot on Discord

  1. Go to https://discord.com/developers/applications/me in a web browser. You should be logged in already through the app, but log in again if you are prompted. In this part, you’ll be creating an app that activates the bot, so you’ll be creating an app as well as a bot.
  2. Click the blue New Application button. You’ll see this to the right side of the browser. A window will pop up for your app’s name.
    • Type in the application’s name and click “Create.” You’ll want to create a name that is descriptive, like “Greeterbot” if your app bot greets people. However, “Greeterbot” will most likely trigger errors later on because it’s a popular name, so add a series of numbers after the name, like “Greeterbot38764165441.”
  3. Click Bot in the left-hand menu. This is also the jigsaw puzzle piece icon.
  4. Click Add Bot. This is under the “Build-A-Bot” header.
    • Click “Yes, do it!” in the pop-up to confirm your action.
    • If you get an error about the name being too popular, go to the application page and change the app name. For example, “Music Bot” was too popular, so adding a few numbers to the end of the app helped.
  5. Click Click to Reveal Token. You’ll see this in the information area of your bot. When you click that text, you see a string of letters and numbers.
    • Click “Copy” to copy all that text. You can paste it on a sticky note somewhere, but make sure you have access to that code and don’t give it out to anyone. Whoever has that code can control the bot. This code will always be here if you need it.Advertisement

Part 3: Sending the Bot to the Discord Server/Channel

  1. Click General Information. This is in the menu on the left.
  2. Click Copy under Client ID. You’ll see this around the middle of the web page.
  3. Paste your copied ClientID into the following URL: “https://discord.com/oauth2/authorize?&client_id=CLIENTID&scope=bot&permissions=8”
  4. Paste your URL into the address bar on your web browser. You’ll be taken to a page where you can assign your bot to a channel.
    • Click the drop-down box to display all your compatible channels.
    • Click “Authorize” to continue. You’ll get a confirmation that the bot was moved and that you can close the active tab.Advertisement

Part 4: Coding the Bot

  1. Create a folder on your desktop for your bot code. You’ll be making files of code that will go in here.
  2. Open a text editor. You can use the default programs like Notepad for Windows or TextEdit for Mac.
  3. Type the following code:
    • Make sure you enter that Bot Token number you got from the previous steps between the quotation marks in the text.
  4. Save the file as “auth.json”. Make sure the file doesn’t save with a .txt file extension.
  5. Start a new document. You can do this by either pressing Ctrl+N (Windows), or ⌘ Cmd+N (Mac), or clicking “New” from the “File” tab.
  6. Type the following code:
    • Make sure you replace “author” name with your name. You can also change the “description” if you don’t like “My first discord bot.”
  7. Save the file as “package.json”. Make sure the file doesn’t save with a .txt file extension.
  8. Start a new document. You can do this by either pressing Ctrl+N (Windows), or ⌘ Cmd+N (Mac), or clicking “New” from the “File” tab.
  9. Type your bot’s code. For example, if you want to create a bot that responds to any messages that start with “!”, type the following code:
  10. Save the file as “bot.js”. Make sure the file doesn’t save with a .txt file extension.
    • You can close your text editor.https://ad1ced99437943e46d2fcc9b25383957.safeframe.googlesyndication.com/safeframe/1-0-38/html/container.htmlAdvertisement

Part 5: Installing Bot Dependencies

  1. Open a command prompt window. On Windows, you can search “Cmd” in the Windows search field in the Start Menu. On Mac, you can search Spotlight for “Command Prompt.”
  2. Navigate to your bot folder on your desktop. For example, you can type cd\Users\Default Desktop\Desktop\DiscordBotfoldername.
  3. Type npm install discord.io winston –save and press ↵ Enter. With Node.js installed, this line will automatically download the dependencies for your bot in your desktop folder.
  4. Type npm install https://github.com/woor/discord.io/tarball/gateway_v6 and press ↵ Enter. That code will make sure there’s nothing else you need to install for your bot to work.
    • You now have code for your bot and will test that your code works in the next part.https://ad1ced99437943e46d2fcc9b25383957.safeframe.googlesyndication.com/safeframe/1-0-38/html/container.htmlAdvertisement

Part 6: Running the Bot

  1. Type node bot.js and press ↵ Enter in the command prompt. If you get an error line, you did something wrong.
  2. Type “!Intro” in Discord. You’ll want to type this in the channel your bot is in. The example code provided triggers the bot to respond “Pong!” to text starting with “!”. So to test if the bot is working, type “!Intro” and wait for a response.[1]
  3. Check your coding if you didn’t hear a response. If your bot didn’t respond to your “!Intro” in Discord, go through this wikiHow again and check to make sure your bot is set up correctly. Make sure:
    • Node.js installed correctly.
    • The Bot Token is entered correctly in your auth.json file.
    • You’re in the same channel as the bot.
    • The bot is on the server.
    • Your coding is correct in your auth.json, bot.js, and package.json files.
    • You downloaded all the dependencies for your bot to work using Command Prompt with Node.js installed.

Conclusion

Discord is a fast growing social media platform that has been widely used by gamers. This implies that there is a strong need for the creation of discord bots. As a result, it has gained wide acceptance as an ideal place to create bots.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x