Starvard University · Discord Bots
Create your first Discord bot, from zero. By the end of this course, a small program you wrote will be sitting in your server, answering commands at any hour of the day.
The whole course in brief
Twelve lessons, distilled. Read this and you already understand how bots work.
All you need is a computer, a Discord account, and a server you own to practice in. Everything in this course is free. New to code? Take Python 101 first, or follow along slowly here; every step is spelled out.
The twelve lessons
Lesson 1
A Discord bot is a computer program that logs into Discord the way you do, with one difference: it has its own special account, made for programs. When you see a bot welcoming new members, running a game, or keeping score in a contest, you are watching software read messages and respond, fast enough to feel like a person.
Two words cause almost all beginner confusion, so let us separate them now. The application is the project you register with Discord. It lives in the Developer Portal, and it holds your settings. The bot user is the account that actually joins servers and appears in the member list. Think of the application as the whole machine and the bot user as its face. One application has exactly one bot user, and this course will build both.
Why build one? Because a bot never sleeps, never forgets, and never gets bored. It can greet every new member at 3 in the morning, answer the same question a thousand times without sighing, and run the little rituals that make a community feel alive. Every large server you admire has a few of these quiet workers in it.
Lesson 2
Everything starts in the Discord Developer Portal, the control room for every bot on the platform. Open discord.com/developers in your browser and log in with your normal Discord account. Click Applications in the corner, then the New Application button.
Notice the Application ID on that page. It is public information, safe to share, and useless on its own. The dangerous secret arrives in lesson 4, and it lives somewhere else entirely. For now, just enjoy the fact that your bot legally exists.

Lesson 3
In the left menu of your application, click Bot. This page manages the bot user itself. If Discord asks you to add the bot, confirm it. Modern portals create the bot user along with the application, so you may land here with everything already in place.
Two settings matter on day one. The username is what members will see in the member list and above every message the bot sends. The avatar is its picture. Upload something simple and readable at tiny sizes: a bold shape or a friendly face beats a detailed illustration that turns to mush at 32 pixels.
One more switch deserves your attention: Public Bot. While it is on, anyone who finds your invite link can add your bot to their server. For a practice bot, turn it off. Your bot stays yours until you are ready to share it with the world.
Lesson 4
On the Bot page sits a button labeled Reset Token. The token is a long string of characters, and it is the password to your bot's account. Your code will present it to Discord to prove it is allowed to speak as the bot. Whoever holds the token controls the bot completely: they can make it say anything, in any server it belongs to.
Press Reset Token, confirm, and copy the new token somewhere private for the next few minutes. You will paste it into a file on your own computer in lesson 7, and nowhere else. Not into Discord chat. Not into a screenshot. Not into a video, a forum post, or a message to a friend who is "just helping you debug." Most stolen bots are not hacked; they are pasted.
If the worst happens, the fix is fast and total: press Reset Token again. The old token dies instantly and every copy of it becomes worthless. Discord also watches public code sites and resets tokens it finds posted there, which has saved many beginners. Do not rely on that safety net. Treat the token like the key to your house, because for your bot, it is.

Lesson 5
Discord does not stream everything that happens in a server to every bot. Instead, your bot subscribes to categories of events, called Gateway Intents. Most intents are harmless and switched on by default. Three of them are called privileged, because they touch people's privacy, and Discord makes you ask for them by hand.
The two you need live on the Bot page, under Privileged Gateway Intents. Server Members Intent lets your bot see the member list and notice when people join or leave; without it, welcoming new members is guesswork. Message Content Intent lets your bot read the actual words inside messages. Without it, your bot receives messages with the text stripped out, so a command like !hello arrives as an empty envelope and your bot sits there, silent, while you question your life choices.
Discord tightened these rules in 2022 to protect members from data-hungry bots, and every beginner since has lost an evening to this lesson. Here is the part that saves yours: the switches must agree in two places. Turn them on in the portal, and declare them in your code (lesson 8 shows the two lines). Small bots like yours simply flip the switches; only bots in more than 100 servers go through a review.
Lesson 6
Your bot exists, but it belongs to no server yet. The door in is a special link you build yourself. In your application, open OAuth2, then URL Generator. Under Scopes, check bot. A Bot Permissions panel appears below it; this is where you choose what your bot is allowed to do.
Start with a modest set: View Channel, Send Messages, Read Message History, and Add Reactions. Copy the generated URL at the bottom, paste it into your browser, choose your practice server, and authorize. You need the Manage Server permission on that server, which is why your own test server is the right playground. Your bot now appears in the member list, shown as offline. It is not broken; it is asleep, waiting for code.
Resist the big tempting checkbox. Administrator hands over every permission at once, and beginners reach for it because it makes errors disappear. It also means a bug, or a leaked token, can do unlimited damage. Add permissions one at a time, as features ask for them, and your bot stays a helpful guest instead of a loaded weapon.
Lesson 7
Time for code. If Python is not on your computer yet, install it from python.org; our Python 101 course walks through it gently. Make a folder for your project, call it something like mybot, and open a terminal inside it. Install the two libraries this course uses:
pip install discord.py python-dotenv
The first, discord.py, is the free library that speaks Discord's language so your code does not have to. The second, python-dotenv, reads secret files. Now create two files in the folder. The first is bot.py, which will hold your code. The second is named exactly .env, and it holds one line:
DISCORD_TOKEN=paste.your.token.here
Replace the placeholder with the token from lesson 4. This separation is the whole security model of the course: code can be shared, shown, and posted anywhere, because the password lives in a different file that never leaves your computer. If you ever use Git, add .env to your .gitignore file on day one, before your first commit, not after your first scare.
Lesson 8
Here is the entire bot. Type it into bot.py, or copy it, but read every line; the explanation after the code is the real lesson.
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@bot.command()
async def hello(ctx):
await ctx.send(f"Hello, {ctx.author.name}! I am alive.")
bot.run(TOKEN)
The first four lines bring in tools: os for reading your computer's settings, discord itself, the commands helper that turns plain functions into chat commands, and load_dotenv, which opens your .env file. The next two lines call it and fetch the token into a variable named TOKEN. Notice what is not in this file: the token itself.
The intents block is lesson 5, written in code. discord.Intents.default() starts with the standard set, and the next two lines switch on the ability to read message text and to see members. If these two lines disagree with the portal switches, nothing works, and now you know why.
The line creating bot makes the bot and sets its command prefix to !, the character a command must start with. Below it, on_ready is an event: code that runs by itself when the bot finishes logging in. It prints a line to your terminal so you can see the login worked. The hello function is a command: when a member types !hello, the bot answers in the same channel, greeting them by name. ctx, short for context, is how the bot knows who asked and where.
The last line, bot.run(TOKEN), is the engine starting. It connects to Discord, presents the token, and then simply stays running, listening. Almost everything you build from here is more events and more commands, stacked on this skeleton.
Lesson 9
In your terminal, in the project folder, run:
python3 bot.py
A second later the terminal prints Logged in as followed by your bot's name. Switch to Discord: your bot now shows a green online dot. Go to a channel it can see and type !hello. It answers, greeting you by name. That reply traveled from your keyboard to Discord, to your little script, and back. You built that.
Now learn the rhythm of bot building. Change the greeting inside the hello function. Back in the terminal, press Ctrl+C to stop the bot (watch its dot go gray), run the command again, and test the new greeting. Edit, stop, run, test: you will repeat this loop a thousand times, and it is the same loop the professionals use, just with fancier chairs.
One truth to absorb before lesson 10: the bot is only alive while that program runs. Close the terminal or the laptop, and the bot sleeps. For a practice bot, that is perfect. Lesson 12 covers keeping it awake around the clock.
Lesson 10
Your bot will break, and the error messages will look alarming. They are actually signposts. Read the last line first; it names the problem, while the lines above it are just the path the code took to reach it. Here are the four errors every beginner meets:
.env.! prefix. Check lesson 5, then check your spelling.The method that fixes all of them: read the last line, change exactly one thing, run again. Changing three things at once teaches you nothing and often adds a second bug to the first. Programmers are not people whose code never breaks. They are people who have made friends with the error messages.
Lesson 11
Your bot grows one feature at a time, and each new command is the same small shape as hello. Here is a second one, the traditional health check of bots everywhere:
@bot.command()
async def ping(ctx):
await ctx.send("Pong!")
Type !ping and the bot answers Pong!. Silly, and genuinely useful: it proves the bot is awake and listening, which is the first thing you check whenever anything feels off.
Messages are not the only way to talk to a bot. Buttons are clickable panels under a message, and discord.py builds them with a small class:
class HelloView(discord.ui.View):
@discord.ui.button(label="Say hello", style=discord.ButtonStyle.green)
async def say_hello(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_message("Hello from a button!")
@bot.command()
async def button(ctx):
await ctx.send("Press it. You know you want to.", view=HelloView())
Run it, type !button, and click. The same idea powers sign-up sheets, polls, and game controls in big community bots. Reactions complete the toolkit: your bot can add an emoji reaction to any message, and it can notice when members add theirs, which is how bots run votes and RSVPs. Finally, know that Discord also offers slash commands, the kind you pick from a menu after typing /. They are the modern standard for public bots and worth learning next; the humble ! commands in this course remain the clearest way to understand what is happening under the hood.
Lesson 12
A bot is just a running program, so keeping it online means keeping a computer running it. The ladder of options, cheapest first: your own computer, awake while you are; an old laptop or a small single-board computer humming in a corner; a rented server for a few dollars a month, which is what most public bots live on; or a hosting platform that runs code for you. The code does not care where it lives. Move the folder, including the .env file, run the same command, and your bot is back.

Last, the rules, because they protect your account as much as they protect Discord. When you created your application you agreed to Discord's Developer Terms of Service and Developer Policy. In plain language, they say this:
None of this is fine print designed to trap you. It is the difference between a welcome guest and a nuisance, written down. Build bots that make servers kinder and more fun, and you will never have a problem.
Graduation
You registered an application, gave it a bot user with a face, guarded its token like a house key, switched on its senses, invited it in with the smallest set of permissions that could work, and taught it to answer when called. You know how to read its errors, grow it a command at a time, keep it awake, and stay inside the rules. That is not a toy demo; that is the complete craft of Discord bots, at starter scale. From here the path is practice: one new command a week, and your bot quietly becomes the hardest working member of your server.
Keep learning
This course is part of Starvard University, striving for greatness.
Starvard is a free university being built right now by kind, hard-working people. Do a first small task with us, like checking a course, adding a book to the library, or testing pages on your phone, and get $10 for it.
Get $10: help us build →