Thought leadership from the most innovative tech companies, all in one place.

How to Make your own Discord Bot in Python

An image of a hand holding a phone

In this article, we will learn how we can make a Discord Bot step-by-step using the Python programming language which can be easily applied in python homework assignments. Discord is a famous group-chatting platform, especially for gamers. Discord is built to allow members to message each other. Each community is called a server. Due to their Popularity Discord is using for business these days. Developers of Discord had announced a programming feature in Discord that will help the users and other programmers to develop a discord bot that will automate things.

Discord bot is an AI that can perform a number of useful automated tasks and commands on your servers.

  1. Welcome to the new member
  2. Replying to the specific message in the server
  3. Ban the rule breaker
  4. Add music, memes, games, and other stuff to your server
  5. and etc

Discord bot gives you a new experience in Discord platform, You can increase your popularity or business. Discord bot helps you to make new ways to interact with your followers. Using Python Language which is the most famous and easy-to-learn the language we can develop a Discord bot using Discord Official API.


If you had already the latest version of Python installed in your operating system you can use the following command to install the Discord Library.

pip install

Now another important thing we need is the API key of your Discord Account. To get the API key follow the following steps.

  1. Login to your Account on Discord Site
  2. Navigate to the Applicate Page
  3. Click on the New Application Button


4. Give your Application a name and click on create button.


Keep the Settings as same you don't need to change anything after creating the bot application.

5. Click on Bot and on that page click on Add bot


6. After this Your bot will be added and just click on Copy to copy the oath token.


7. Choose the Scope of your Bot application which is "Bot" i selected.


8. Next select the Bot Permission you want to bot had, Choose it carefully this permission will affect your bot. My bot is only used for Messaging so i select the following Permissions.


9. Click on the Copy button and paste it on your browser.


10. After you open the Url in your browser You need to choose your server in which you want your bot should be added.


That its, your Discord Bot is ready and attached to your Discord Server, Amazing! right now it's time to work on the coding part of your bot.

Discord Events for your Bot:

Let make an event for your discord bot in which our bot will respond to the specific messages in your server.

import discordclient = discord.Client()
async def on_ready():
 print('We have logged in as {0.user}'.format(client))@client.event
async def on_message(message):
 if == client.user:
 returnif message.content.startswith('$Hi'):
 await'Hi!')'your token here')

Let me explain how this code is working, the First line we simply importing the discord module, and next, we set discord.client() it to a client variable. Then we made an asynchronous function name on_ready() . The asynchronous function will await the execution of a promise, and an asynchronous function will always return a promise. Also on top of the function, you had noticed we had to use a client.event() that registered an event of the function.

That function is printing the message we had logged in with that username. The next asynchronous function is on_message() which triggers an event for every message received the function had if the condition is checking the is same as the client user. In that, we had to ignore the response of the bot on our own messages. And in the end lines, we are checking the message content, and if the content matches we will send a response message to the server.

Now we had developed our bot and added an event, it's a basic discord bot event. Let run the bot and see the results.

Extending the Discord Bot Events:

We can improve or add new events to our bot. Let add more conditions to our bot to send a response to specific messages. Check below how we can extend the events by adding more conditions to our bot.

Welcoming New Users:

When a new user joins our Discord Server we should welcome him as a new follower. We will modify our Bot to welcome the new user to our server.

import discordclient = discord.Client()
async def on_ready():
 print('We have logged in as {0.user}'.format(client))@client.event
async def on_member_join(member):
 for channel in member.guild.channels:
 if str(channel) == "general":
 await channel.send_message(f"""Welcome to the server {member.mention}""")'your token here')

Let Breakdown the code to understand it's working, The new event function on_member_join() has for loop in it which is iterating the channels in our server, and in the body of the loop, we made a condition that if the channel name is general then send a welcome message to the new user. We also know that we don't need to run these function separately die client.event it automatically triggers the function when a user does any activity on the server.

Deleting "Bad" Messages

Sometimes we don't want some bad words to appear in our server chat by the members, you can delete or filter those words from your discord server.

import discord
client = discord.Client()
async def on_ready():
 print('We have logged in as {0.user}'.format(client))@client.event
async def on_message(message):
 words = ["bad", "stop", "unfriendly"]
 for word in bad_words:
 if message.content.count(word) > 0:
 print("Bad word deleted")
 await'your token here')

So we had to use on_message event function again and we know that this function is tracking every message sent by the user in our discord server whenever message, The bot will search that message in the words list we had created if the word match it will return 1 to the if condition and it will delete that message from the server at the spot.

Getting numbers of Members:

Now our Discord bot also can get the number of members we had on our Discord server. To do so check the below code.

import discordclient = discord.Client()@client.event\
async def on_ready():\
 print('We have logged in as {0.user}'.format(client))

async def on_message(message):
 elif message.content == "!users":
 await"""# of Members: {id.member_count}""")'your token here')

To enable this feature to our to users, they need to message !users in our discord server and when the message is being read by our bot with the on_message event function, it will send a message of total members in our discord server using member_count object calling.

Embedding Messages in Discord:

We can make our response message look more clear and prominent using the embedding method. I will add an event that when the user enters !help the bot will respond with an embedded message.

import discord\
client = discord.Client()\
async def on_ready():\
 print('We have logged in as {0.user}'.format(client))

async def on_message(message):
 if message.content == "!help":
 embed = discord.Embed(title="Help on Bot", description="Some useful commands")
 embed.add_field(name="!hello", value="Greeting the user")
 embed.add_field(name="!users", value="Display number of users")
 await, embed=embed)'your token here')

Alright! we had a modified on_message the function event and added a condition if the user wrote !help then send an embedded message using discord. Embed in which we are passing title and description. In the next line, we adding the fields in our Embedded message we give a view to using that if the enter !hello then the bot will greet him back and if they enter !user then it will return a number of users on the discord server.


So far we learn many things in this article, we learn how we create a discord bot, modify it adding more functionalities and events to it. But you can do more with the Discord Bot. Check out the official documentation of discord Bot by the Discord Developers. This is a beginner's guide to give you a boost of understanding how the discord bot works and how to develop and use its functions and what are the events and etc. I hope this article finds you helpful in the future. 😊 💻

Continue Learning