Guide on how to make a Discord bot
Discord is a messaging platform with fabulous voice and video streaming capabilities, built to serve gaming communities. It offers comprehensive extensibility with bots, custom moderation tools, and more, which we’ll show you how to work with here.
What is a bot?
Before asking about how to make a discord bot, we need to understand what a Discord and bot are. Discord is a social platform for VoIP and instant messaging. Users can communicate via voice or video calls, text messages, multimedia, and files in private chats or as part of communities. Anyone can create a Discord server for free. It can be used to:
- bring friends together in a chat;
- post gifs, share memes and screenshots, or talk about your day;
- launch it during the game using voice channels;
- broadcast the game to friends who can offer advice or, more likely, laugh at your poor aim.
Game developers and publishers often use Discord as a place to share game goodies with their communities. Automated processes such as blocking unwanted users and responding to user requests are vital to the prosperity and growth of a community.
Bot users are automated programs that act like real users and automatically respond to events and commands. Bots have an unlimited app number.
You manage a new Discord community, and a user joins it for the first time. You can personally contact this user and welcome him to your community. You can also tell them about your channels or ask them to introduce themselves.
The user feels welcome and enjoys the discussions in the community, and they invite friends.
We are confident that we have what it takes to help you get your platform from the idea throughout design and development phases, all the way to successful deployment in a production environment!Contact us
When the community becomes large, it is no longer possible to contact each new member personally, but the owner still wants to send them something to get to know them as new community members.
The Bot can automatically respond to a new member of the community. You can even customize its context-specific behavior and control how it interacts with each new user.
When creating a bot, there are two key steps:
- Create a bot user in Discord and register it with the guild.
- Write code that uses the Discord API and implements your Bot’s behavior.
This is an example of how a bot can work. It provides a lot of creative possibilities if you know how to create them.
Before discussing the technical details, it’s crucial to understand how a user interacts with Discord and how it presents itself to users. The way it is suggested to bots is conceptually similar (but certainly not visually). Official apps are built on the same APIs as bots. There is a technical possibility to run a bot inside a regular user account with minor modifications, but Discord’s terms of service prohibit this. Bots must work in bot accounts.
List of servers
If you have worked with Slack, a server is similar to a Slack workspace and is a users’ group who can interact with each other within channels on the server. The server is managed by its creator and any other personnel that have access. The creator defines the rules and channel structure on the server and manages the users.
The bot gives the opportunity to get help and communicate with other developers. A button “Create a New Server” is available. Anyone can create a server in a few clicks.
The user interface uses the term “Server,” but the developer and API documentation use “Guild.” These two terms are interchangeable.
On the right sidebar of the server window, the user can see the channel list for the servers he currently works with. Channels can be divided into an arbitrary number of categories.
The category is used to organize channels into sections that users can collapse and expand in their Discord client. Categories allow you to control the permissions of multiple channels at the same time using category settings.
The user can add categories, he must have permissions must have permission to manage categories.
For example, categories on the API server can include:
- LIBRARY, etc.
Each channel functions like a chat where users can discuss any topic the channel is about. The channel we are currently viewing information has a lighter background. Channels with new messages since they were last viewed have white text color.
The owner can see what users have been talking about in the channel he currently views in View Channel. The server admins set up this channel so that regular users can’t send messages to it. The administration uses this channel as a bulletin board for important information, where it is easy to see and will not be drowned out by the chat.
On the right is a user list currently online on this server. Users are organized into various categories, and their names have different colors. This is the result of the roles they have. The function describes what type (if any) the user should appear in, what color their name should be and their permissions on the server. A user can have one and more roles, and determine what should happen in a particular case.
Roles are like ranks on a forum or subreddit. They give users different privileges on the server. Each user has the @everyone role. Server personnel can create and assign other roles. Users can type a text and send messages using the text input function.
This is the current user. Below the username is a number. Many other users may have the same name, so setting a username is possible to avoid confusion.
These are the main parts of the user interface, but there are more. Getting started with Discord is easy, even without creating an account. You can log into Discord by visiting the homepage, clicking “open Discord in the browser,” choosing a username, and playing a refreshing round or two of “click on the bus picture.”
Creating Discord bot
You will need an account and a server to test the Bot. If you haven’t created one yet, go to Discord.com and create one. If you have it, log in to your account and open the server you want your Bot to live on.
You will need a text editor program such as Notepad++ on Windows to do the coding.
Create a Discord server
Before creating a bot, you should start by creating a server, as this is the Bot’s destination.
The Discord server or chat is the space where you manage the channels and communication on the platform.
To create a server, go to the Discord website and log in the dashboard or create an account on the server.
Once on the dashboard, follow these steps to create a server:
- Click the (+) icon on the left sidebar.
- Select the “Create Yourself” option.
- Choose the purpose of making the server.
- Customize the server with a profile picture and a name. Then click “Create”.
- You have now created a server and are ready to develop a bot to manage specific activities on it.
Create and customize the Discord bot
Next, create a bot inside the app by going to the Discord developer portal. Or instead, you can enter the following URL in your browser’s address field:
In the developer console:
- Click “New App” in the top left corner;
- Enter a name for your application in this field. Then click “Create”;
- Look at the left sidebar and select Bot;
- Click “Add Bot” on the far right;
- Select “Yes, do it!”;
- Click “Copy” to copy the bot token;
- Create a text file at the root of your project and paste it there and name the file “secret.txt.”;
- Make sure you don’t expose the copied token;
- Scroll down and turn on the switch under “PRESENCE INTENT”. Also, turn on the toggle under “SERVER MEMBERS INTENT”.
- Then click “OAuth2” in the left pane to add authentication priorities for the Bot.
- Select “URL Generator” to generate an invitation URL for the server.
- From the options below “SCOPES”, choose “Bot”.
- Scroll down to the “BOT PERMISSION” and choose “Roles for the bot”. In this case, we will give it the administrator role.
- Scroll down and click “COPY” to copy the generated URL to your clipboard.
Now let’s connect your server with the Bot.
Link Discord bot to the server
The Bot works mainly on the server. After creating a bot, you will have to assign it to the server you want it to run on.
Copy the generated URL, and follow the steps below to link your chat room to the bot:
- Paste the copied URL into your browser’s address bar and sign in.
- At the prompt, click “Select Server” and select the one you created earlier.
- Click “Continue” to proceed.
- You will see a new menu with a list of the earlier permissions. Select “Log in”.
- Solve the CAPTCHA to complete the authorization of your bot.
- You have created a bot. Return to the server control panel, and you will see a robot on the right sidebar. But as you can see, it is currently offline. So it doesn’t work until you program it.
You have to write a script in any programming language that suits you best to make your bot work.
Program Discord bot with Python
We will use Python and host a bot on a local machine. Host yours on cloud services like Heroku once everything is set up. Let’s dive into how to make a Discord bot using Python.
Set up Python and libraries
To program your Discord bot with Python, you will use the discord.py module, which works on Python 3.5.3 or later.
To update Python, go to python.org to download the latest version.
Check the Python version installed on the PC by typing the following command in your terminal:
If Python is up to date, open a terminal at the root of your project. Then activate the Python virtual environment.
You need to download discord.py and a voice support package Discord.py[voice].
Using the virtual environment, run the following command in the terminal to install the packages: pip install – U discord.py discord.py[voice]
Send a Message With Discord Bot
This is where you will program your bot to respond with a custom message when you post a text on a channel.
# Import the commands module:
from Discord.ext import commands
# Initialize the bot instance and use a blank prefix:
bot = commands.Bot(command_prefix=””)
# Read the bot token from the txt file in your project root:
Secret = open(“secret.txt”, ‘r’)
Secret = Secret.read()
#Run the bot in an event loop:
The bot variable calls custom commands from the command class. Symbols such as the dollar sign ($), an exclamation mark (!), ampersand (&), and others are common prefixes that precede bot commands. And command_prefix in brackets allows you to specify the character that precedes it.
Using the @bot.command() decorator, let’s create a custom “Hi” command and provide a response text for the Bot when it sees it.
Keep in mind that custom commands are Python functions:
# Import the commands module:
from discord.ext import commands
# Initialize the bot instance:
bot = commands.Bot(command_prefix=””)
async def Hi(ctx):
await ctx.send(“Hi, welcome to our server”)
Secret = open(“secret.txt”, ‘r’)
Secret = Secret.read()
Run the Python script in your terminal. Then go to Discord and send “Hello” to see the magic unfold.
If you look closely, you’ll see that we’ve used an asynchronous wait method to service the function. This is important as it is the only way the module can communicate with Discord.
The ctx. instance is a context and has various functions. It allows the Discord bot to send messages.
Reply to messages
We have to update the Bot to use the two lists we created. First, import a random module because the Bot will randomly select encouraging messages. Add the next line to the import statements at the top of the code: import random.
We will now update the on_message() function and check messages to see if they contain a word from the sad_words list. If there is a sad comment, the Bot will send an encouraging message.
The updated code:
async def on_message(message):
if message.author == client.user:
msg = message.content
quote = get_quote()
if any(word in msg for word in sad_words):
This is a good time to test the bot. Now you know enough to create your own bot. But next, you will learn how to implement more advanced features and store data using the Repl.it database.
How to set up a bot to work continuously
Suppose you start your Bot in repl.it and then close the tab it’s running in. Your Bot will stop working.
But there are two ways to keep the Bot running continuously even after the web browser is closed.
The first option is to sign up for a paid plan at Repl.it. The most affordable paid plan is the Hacker Plan and comes with five permanent reps.
Once you have subscribed to this plan, open Repl, click the name at the top, and the “Always On” option.
The second way is to keep your code running even at the free tier, but it’s more complicated. Repl.it will keep the web server running even after you close the tab. But even a web server will only run-up to an hour without any use.
Here’s what the repl.it docs say that the server will run in the background after the browser tab is c once deployed. The server will remain awake and active for an hour after the last request, after which it will go to sleep. Sleeping replicas will be awakened when they receive another request; there is no need to re-run repl. But if you make changes to your server, you will need to restart the replica to reflect those changes in the live version.
To keep the Bot running continuously, we will be using another free service called Uptime Robot.
Uptime Robot can be configured to ping the Bot’s web server to repl.it every 5 minutes. The Bot will never go to sleep with a constant ping and continue to work.
So there are two more things to keep the Bot running continuously: create a web server in repl.it and configure Uptime Robot to constantly ping the web server.
The bots will be able to respond to messages and commands and many other events. You can create bots to interact with users in guilds you make or bots other users invite you to interact with their communities.
Read their extensive documentation to learn more about the powerful discord.py library and take your bots to the next level. The bot-creating algorithm is general for Discord and can be applied for building other types of Discord apps.
I am here to help you!
Explore the possibility to hire a dedicated R&D team that helps your company to scale product development.