A Discord bot is commonplace in nearly every Discord server (or guild) you are a part of. In this guide you will learn what a Discord bot is, and also how to create your own Discord bot.
The Basics
Discord bots can be created for multiple different reasons. Let’s dive into some of the different reasons.
Moderation. Discord can lack in certain moderation processes which is what a Discord bot in most cases is used for. Here’s some examples;
- Keyword blocking/deletion.
- Auto filtering of members who are flagged as spam.
- Prevention of channel bombing (Where multiple channels are created to flood the Discord server.)
Entertainment. Sometimes you need a bit of fun in your Discord server while waiting on friends to come online or even just to spend some time while doing something IRL. Discord bots can absolutely provide some quick and easy entertainment in different forms such as:
Music Bots to listen to music in a voice call with your friends.
Simple games such as
- Blackjack
- Higher or Lower
- Heads or Tails,
- Etc…
AI Generated content such as Images or Text.
Support. Discord bots have become more prevalent in services for general support.
- Handling customer service tickets.
- Providing detailed information on certain aspects of other sites/games.
- Auto reply of basic information.
Discord bots are in most cases more than just a basic feature in a Discord server and can be very complex. The best Discord bots are the ones that do the most but still stay within the realm of understanding as a consumer.
Setting up your IDE or Development Software
The first thing before even starting a Discord bot is making sure you have the correct program (IDE) to build your Discord bot. There are plenty of options out there ranging from the absolute basic with no advanced features to something as advanced as having AI built into it. With the growing prevalence of AI, it will be hard to avoid it however, if used correctly, AI can boost productivity in your Discord bot development.
What Code Editor Should You Choose?
This completely depends on your own personal preference. Below I will rank the most common IDE programs below. In the rest of the guide we will go over the specific code and steps in creating the Discord bot itself.
Notepad++ – 4/10
This is obviously extremely similar to regular Notepad that is default on your device however, this Notepad++ is more powerful, with color coded text to see errors more noticeably.
Visual Studio Code (VSC) – 7/10
As the most popular powerhouse of the IDE world, this is a great option. This is the first IDE most developers in our day-n-age start with and personally is mine. It’s extremely clean and has a massive amount of backing to ensure it stays updated and fueled by an active community who create plugins that can be installed to further enhance your coding adventures.
Cursor AI – 9/10
The newest installment to code IDE programs, this is an absolute unit. Not only is it a near copy paste look and feel from VSC, it also boasts an amazing feature to use AI which can read and write to your code without much input outside of overseeing the process to ensure it runs smoothly. This is a much more advanced IDE to use which would be recommended only if you are more advanced or if you plan to stick to vibe coding primarily.
What do I recommend?
Now why would I personally recommend it? If you are reading this guide to learn, I would recommend first using VSC. It’s very beginner friendly and does not muddy the waters with AI that does things you may never have seen before or create errors that you would have never had before. VSC is amazing!
Now what?
Once you have your chosen IDE installed, open this and open a project by creating a folder wherever you desire and leave it blank for now. We will dive into the file creation shortly!
Creating Your Discord Bot Application
This is the easy part! Lets dive right into with the steps.
Step 1:
- Visit – https://Discord.com/developers/applications and log in.
Step 2:
- Click on New Application at the top right of your screen.
Step 3:
- Create the name for your Discord bot (any name is fine).
- Make sure you agree to the Terms and Service after you have read them and click “Create”
Step 4:
- Now your bot is created! Great job! The next step just involves assigning the bot permissions.
- Click on the “Bot” tab on the left hand side. Scroll down till you see Privileged Gateway Intents and select all 3 intents and enable them.
- Click Save Changes
What is this?
This allows your bot to have more abilities in Discord. You do not actually need to enable these options unless your bot does NOT rely on messages sent in Discord to be read or sent, listen to Discord server members joining/leaving or those existing in the Discord server, or if your bot does not need to see the status of the users in the channels like those joining voice call channels. It would be highly recommended to just enable all these intents.
Step 5:
- Invite you bot by clicking on the OAuth2 tab now.
- Scroll down to the box labeled OAuth2 URL Generator and click on “bot” and then scroll down to “Administrator” and check that box as well.
- Scroll down a bit further to the “GENERATED URL” and click the copy button.
- Paste this URL into any browser URL box and hit enter. You will be taken back to Discord to invite the bot to your Discord server.
Step 6:
- Click on the “Bot” tab one last time.
- Click on the Reset Token button and “reset” your token and copy it. (You will need this later when placing your “BOT_TOKEN” in the code).
- Do NOT try to use the code seen in the screenshot. It is already expired.
- Do NOT give your bot token to anyone for any reason. This gives full permission access to your bot on a developer side and can even allow the user to log into your bot with a client (which is against Discord’s TOS).
Writing Your First Bot Script
You are now ready to start coding! Head over to your preferred IDE and let’s get started. I will be using Cursor however, remember that Cursor and VSC will look nearly identical!
What programming language should I use?
This is another question that is completely up to you. Personally I love working with JavaScript. While it can be a bit more confusing at times, it’s great for overall speed and flexibility. For this tutorial I will be displaying the code as JavaScript only. If you are looking for Python or another language like Typescript, you will have to do some of your own research. This guide shows how you can create the bot from scratch!
Lets get your bot ONLINE!
The very first and most important step is to simply write the first lines of code to get your bot online.
Name this file “index.js” as most hosting sites will use this name as your starting file by default. You can always change it later but for the sake of simplicity and compatibility on start, use “index.js”
const { Client, GatewayIntentBits } = require('Discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', () => {
console.log(`I am online!`);
});
client.login('YOUR_BOT_TOKEN_HERE');
Great! Now that is all set. Let’s actually turn it on so we can see the online status! There’s a couple things we should do first to ensure our JavaScript files are working as they should.
- Ensure NodeJS is installed – https://nodejs.org/en
- Type node index.js
- Look in your terminal and see the console log which should say “I am online!” if you copy and pasted my above code.
- Check your Discord server where you invited the bot and see the online status of the bot.
- If you do not see the bot online then make sure you have all the above steps completed.
- Double check your bot token is correct
- Double check that your bot is in the correct Discord server.
- Double check that node is installed by typing “npm version”
In the terminal. You should be seeing results!
Adding a Feature to Your Bot
Now that the bot is online and ready to run, let’s dive into the main thing being commands!
The most basic command most guides will have you create is the “Ping” command. In this guide, we will do it slightly differently by using text based commands! This way you can learn how the bot listens to messages and responds based on the message it receives.
Step 1:
- Navigate to your index.js file.
- Increase the intents for your bot by changing
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
To
const client = new Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
] });
This allows your bot to see Discord messages and the content of the messages!
- Create the pinging command;
client.on('messageCreate', (message) => {
if (message.author.bot) return;
if (message.content === 'ping') {
message.reply('pong!');
}
});
That’s it! So what exactly is happening here? Great question, basically on client load or bot start, the bot will start listening to all the messages sent in the Discord server it is in. If the message is received by a user and not another bot that has the content of “ping” it will reply with “pong!”. Try it out and see the response!
Hosting Your Bot
Now hosting your bot can be confusing sometimes. Not knowing what to pick and not understanding how hosting a bot works can be daunting. In most cases, you do not get to see the exact step by step process since it is behind a paywall for any decent hosting services. Luckily for you, I have the scoop! Let’s dive into something that is cheap and still offers a great hosting service!
Cybrancee – $1.49 USD/Month – Easy to use
Once you have the server purchased for JavaScript or whatever language you use for your bot lets look at how to add your files!
- Go to your active services and Click Manage
- Click go to panel
Now you are ready to paste your files into the server!
- On the navigation on the left side click on files
- Click on the further right button that says New File.
- Paste all the contents of your index.js and click save and then name it index.js
- Click on the “startup” tab and turn on the “NPM Install” and ensure the Bot JF File is indeed set to index.js.
- Go back to the navigation bar on the left side and click on “Console”
- Click the big green button that says “Start”
You are all done! Look for the online status of your Discord bot and continue to develop the bot in your local IDE and copy and paste the files once they are changed. As you continue to develop your Discord bot, it will only get better!
Tips and Best Practices
- Take Your Time
The absolute best piece of advice I would give you when starting out with Discord bot creation is to not overcomplicate things and don’t get ahead of yourself! Stick to what you know and keep it clean. (Easier said than done.) Take your time! It is easy to get lost in the amount of files created.
- Hard coding
Try your best to avoid hard coding sensitive information such as bot tokens directly into the code. In this example it would be hard coded however, it’s best to use something like a .env file to store that information and then have your main file call on that file. It’s much more secure!
- Ethical uses
Discord bots are a great feature that Discord allows us to use. Make sure to follow Discord’s TOS and do not use it maliciously. We do not want to lose the use of the Discord bots!
- Ideas for your bot
Some amazing things I have personally explored and created use slash commands where you type /commandhere. This makes the experience more seamless and can offer some great UI experiences. Look at the Discord resources and explore Github for ideas and knowledge further! Remember, the world is your oyster when it comes to Discord bot creation!