Starvard University · Discord Bots

Discord Bots 101

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.

Starvard University
Starvard University
Discord Bots 101 · Starvard University

The whole course in brief

Twelve lessons, distilled. Read this and you already understand how bots work.

  1. A bot is a program with a Discord account. The application is the project in the Developer Portal. The bot user is the account that shows up in your server.
  2. You create the application first. Two minutes in the Developer Portal, no code yet. Name it well; you can change almost everything later.
  3. The Bot tab turns the application into a bot. That page is where its username, picture, and public settings live.
  4. The token is the bot's password. Whoever holds it controls the bot. It lives in a private file on your computer, never in chat, never in a screenshot.
  5. Intents are what your bot is allowed to notice. Server Members and Message Content are the two big switches. Turn them on in the portal and in your code.
  6. The invite link lets the bot in. You build it with the OAuth2 URL Generator: the bot scope and a short list of permissions, nothing more.
  7. Least privilege is the golden rule. A bot that can only read and send messages cannot be turned into a wrecking ball.
  8. discord.py does the heavy lifting. One free library connects your Python code to Discord. Your job is the fun part: the commands.
  9. Twenty lines is a working bot. Imports, intents, one event, one command, run. Every big bot started this size.
  10. Errors are signposts. The last line of the error tells you what broke: a bad token, a missing intent, a missing permission.
  11. A bot only lives while its code runs. Close the laptop and it sleeps. Hosting is just a computer that never closes.
  12. Never automate a normal user account. Self-bots break Discord's rules and get accounts deleted. Bot accounts are the one legal road, and they are free.

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

What a Discord Bot Actually Is

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.

The application is the project. The bot is the account. Everything in this course follows from that one split.

Lesson 2

Create Your Application

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.

  1. Give your application a name. This becomes your bot's first username, so pick something friendly that people can type. You can rename it later.
  2. Agree to Discord's Developer Terms of Service and Developer Policy. Lesson 12 explains them in plain language.
  3. Click Create. You now own an application.
  4. On the General Information page, add a short description and, if you like, an app icon. This page is the public paperwork for your project.

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.

A robot at a control panel full of switches and dials

Two minutes, zero code. Your bot exists on paper now. Next we give it a face.

Lesson 3

The Bot Tab: Giving It a Face

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.

Name it like a member of the team. It will sit in the member list next to real people, so treat it like one.

Lesson 4

The Token: A Password You Never Share

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.

A golden key beside a locked book with a warning sign

Leaked token? Reset first, panic second. A reset takes ten seconds and locks the thief out completely.

Lesson 5

Intents: What Your Bot May See

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.

No intent, no message. If your bot ignores every command, this lesson is the fix nine times out of ten.

Lesson 6

Invite Your Bot to Your Server

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.

Administrator is never the starting point. Give the bot the keys to the rooms it actually uses.

Lesson 7

Set Up Python and discord.py

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.

Code in one file, secrets in another. Share the code freely; guard the little file with your life.

Lesson 8

Your First Bot, Line by Line

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.

Twenty lines is a real bot. Everything else you ever add is decoration on this frame.

Lesson 9

Run It and Talk to It

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.

It answers because your code is running. The bot does not live in Discord's cloud. It lives in that terminal window.

Lesson 10

When It Breaks: Reading Errors

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:

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.

The last line is the diagnosis. Read it first, fix one thing, run again.

Lesson 11

Commands, Buttons, and Reactions

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.

One feature at a time. A bot with three commands that work beats a bot with thirty that almost work.

Lesson 12

Keep It Online, and Play by the Rules

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.

A robot working at a desk at night under the moon

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.

Build bots that make servers better. That is the whole rulebook in one sentence, and it will keep you out of trouble.

Graduation

Your Bot Is Alive

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 going: Python 101 to get fluent in the language your bot speaks, AI Agents: Your First $10,000 for where bots grow up next, and all free Starvard courses.

Back to Starvard University

Keep learning

This course is part of Starvard University, striving for greatness.

Explore Starvard · All free courses

Help build Starvard, and get $10

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 →