Comment on page
Create a Discord Bot in Minutes with Python
If there’s any platform that’s become the darling of online communities, it's Discord. Its intuitive UX, vibrant communities, and diverse feature set have allowed it to grow rapidly over the 6 years.
Among developers at least, a lot of the love for Discord comes from its high programmability. In this tutorial, we are going to be making a simple Discord Bot with Python that you can add to your servers.
To get started, first head to
And Create a New Application
From there, head to the “Bot” tab and create a new bot.
Finally, to add our bot to a server, go to the oAuth2 tab, scroll down to scopes, check bot and visit the generated URL.

You can then select the server you want to add the bot to and you should see it on your server under offline users

That’s all we need to do from the Discord side!
Now let’s code our bot.
First, install discord.py with:
pip install discord
And create a new file called
main.py
We can then authenticate our discord like so:
main.py
1
from discord.ext import commands
2
TOKEN = "FIND YOUR TOKEN IN THE BOT TAB IN DISCORD DEVELOPER PORTAL"
3
4
# Initialize Bot and Denote The Command Prefix
5
bot = commands.Bot(command_prefix="!")
6
7
# Runs when Bot Succesfully Connects
8
@bot.event
9
async def on_ready():
10
print(f'{bot.user} succesfully logged in!')
11
12
bot.run(TOKEN)
You can find your authentication token in the Bot Tab in your developer portal
Now let’s read and respond to messages that people send in channels
main.py
1
from discord.ext import commands
2
TOKEN = "INSERT-TOKEN"
3
4
bot = commands.Bot(command_prefix="!")
5
6
@bot.event
7
async def on_ready():
8
print(f'{bot.user} succesfully logged in!')
9
10
@bot.event
11
async def on_message(message):
12
# Make sure the Bot doesn't respond to it's own messages
13
if message.author == bot.user:
14
return
15
16
if message.content == 'hello':
17
await message.channel.send(f'Hi {message.author}')
18
if message.content == 'bye':
19
await message.channel.send(f'Goodbye {message.author}')
20
21
await bot.process_commands(message)
22
23
bot.run(TOKEN)
We need to include the bot.process_commands at the end in order to make sure the bot also checks if the message is a valid command.
And here you can see it working:

Now let’s add commands. Commands are just responses that are specifically invoked. We designated at the beginning that each command has to start with ‘!’, which will act as the “Alexa” to start listening for a command.
We are going to add two commands, one that returns the square of a number, and one that tells us the scrabble points for a specific word.
main.py
1
from discord.ext import commands
2
TOKEN = "INSERT TOKEN"
3
4
bot = commands.Bot(command_prefix="!")
5
6
@bot.event
7
async def on_ready():
8
print(f'{bot.user} succesfully logged in!')
9
10
@bot.event
11
async def on_message(message):
12
if message.author == bot.user:
13
return
14
15
if message.content == 'hello':
16
await message.channel.send(f'Hi {message.author}')
17
if message.content == 'bye':
18
await message.channel.send(f'Goodbye {message.author}')
19
20
await bot.process_commands(message)
21
22
23
# Start each command with the @bot.command decorater
24
@bot.command()
25
async def square(ctx, arg): # The name of the function is the name of the command
26
print(arg) # this is the text that follows the command
27
await ctx.send(int(arg) ** 2) # ctx.send sends text in chat
28
29
@bot.command()
30
async def scrabblepoints(ctx, arg):
31
# Key for point values of each letter
32
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
33
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
34
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
35
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
36
"x": 8, "z": 10}
37
points = 0
38
# Sum the points for each letter
39
for c in arg:
40
points += score[c]
41
await ctx.send(points)
42
43
44
bot.run(TOKEN)
And here we can see it working!

Now, of course, you’re not going to want to be running your script from your local machine 24/7. That’s why it’s crucial to deploy your script in the cloud.