How To Make A Discord Bot In Python

In this tutorial, we are gonna teach you how to make a discord bot in python using discord.py, So lets get started.


1. Go to the developer portal.

Making a bot requires to make an application using the developer portal. So, to get started, you must click a button that says [New Application]. Once you've named your application check the box that says [By clicking Create, you agree to the Discord Developer Terms of Service and Developer Policy.]

2. Invite the bot.

To invite the bot, you must go to [OAuth2] section, (press the three lines). Now press those three lines and you should see (URL Generator), and select the (bot) scope and scroll down and copy the link below.

3. Go to your server and paste the link and send it.

Make sure youre doing it in a private channel.

4. Click the sent link.

After you've done step 3, Click on the link and you should be greeted with this window. So click on [Authorize], but then it asks you to confirm that you are human, so make sure you do that too.

5. Go to the developer portal again.

Once you authorized and confirmed that you are human, We are gonna go back to the developer portal and do the coding. So click the three lines and click on Bot.

Now first, we are gonna scroll down and go to where this should be shown on. Enable the [MESSAGE CONTENT INTENT] and click on [Save Changes].

6. Get the token.

If you are on the Bot section, scroll up and you should see [Reset Token]. Now click that and you should see a popup that says that do you want to reset the token.

Note that you shouldn't share tokens with anyone.

7. Go to replit.

Once you copied the token, you should go to this site, https://replit.com, as we need to make some functionality to the bot.

If you haven't made an account, make one for now.

Once you made an account, click on [+ Create Repl]

And search for "Python".

8. Installing the "discord" module.

Once you have made a repl, your screen should look like this.

Now, if you see a [Shell] tab, click on it. Now type this.

pip install discord

Once its finished installing the "discord" module, follow the steps.


This should be at the beginning of the code.

import discord
from discord.ext import commands

Now this should be next. (Note: you can also change the prefix to your liking.)

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

And one question, how can we tell that the bot logged in? Well, we simply put these three lines of code.

@bot.event
async def on_ready():
  print("Logged in!")

After we put the lines of code to tell that the bot is online, it's time for the commands. First we are gonna create a simple one, that greets the user that executed the command.

@bot.command(name="hello")
async def hello(ctx):
  await ctx.send(f"Hello, {ctx.author.mention}!")

Now, to run the bot, we have to put this last line of code and also which includes pasting the bots token.

bot.run('PLEASE_REPLACE_THIS_WITH_YOUR_BOTS_TOKEN')

After that coding, press the [Run] button!


9. Checking the bot is online.

After you pressed the run button, you should go back and you should see that the bot is online.

Now type the command with the prefix and see the magic happen.

Press enter.

As you can see, the bot replied! Congrats for making your first command!

Now if we want to make a command so that the bot rolls a dice, we have to go to replit and stop the bot by pressing, well obviously the [Stop] button.


Now make a line above the [bot.run()] and type this in.

@bot.command(name="roll")
async def roll(ctx):
  from random import randint
  await ctx.send(f"Rolled number: {randint(1, 6)}")

Now if you typed that in, press the [Run] button.


Now go to discord and type in the command.

Press enter.

As you can see, it rolled a random number! But to make sure, lets execute the command two times.


End of the tutorial

And there it is, the end of the tutorial. Hope you enjoyed this tutorial and see you next time! :)


Discord Profile