Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Discord Bot in Python beginner Tutorial!

Discord Bot in Python beginner Tutorial!


Today we will look into the Basics of Discord Bots for your own server, as this Series progresses we will get into every aspect you could possibly need, to shape your own Discord bot the way you need it to be!

Important side note: This Setup is for Testing purposes, once you shut down your computer the bot will shutdown aswell until you run its program again.

1. Requirements
2. Creating the Discord Bot and inviting
3. Creating the Workspace for Python
4. Getting the Bot online and first real command
5. Welcoming Members
6. Automatically giving Roles when Joining
7. Tips and Tricks!



1. Requirements:

You need a Discord Account
A Server to test in
Python (atleast 3.8)
And some Motivation to get started!


2. Creating the bot

Go to: https://discord.com/developers/applications and Log in to your main Account

Now Click on "New Application" in the Top right corner! Create the Application and give it a name!

[Image: Bild-2025-08-05-124437078.png]

On the left Bar click on "Bot" and reset the Token, copy it and save it! !DO NOT SHARE THIS TOKEN WITH ANYONE!
Also give it, Presence Intent, server members Intent, Message Content Intent!

[Image: Bild-2025-08-05-125428103.png]

Now Lets invite it! On the left bar, click on OAuth2 and scroll down to URL generator

Give it Scope: Bot
And Permissions as this is mainly for testing reasons and YOUR the one in control, give him Administrator.
Scroll down to Bottom, Copy the URL open it and use the Server you want to test it on.

He should be on the Server now!


3.Python

We will use the Code:
Code
pip install -U discord.py

Now Create a new workspace for the Code in for example Visual Studio Code

Select language Python and lets get going!

I also count this as setup:

Code
import discord
from discord.ext import commands

The second line is a useful tool so we dont need to make everything manually!

4. Getting this Bot online:

Again as this is a Testing Bot we will allow him to do anything!

I will explain every Part exactly and what every Code line does!

Code
intents = discord.Intents.all()


This will give the Bot the ability to do everything, so we dont need to come back and change everytime we add something!

Code
bot = commands.Bot(command_prefix="!", intents=intents)


This Code sets the prefix, for our Bot "!" can be anything you like

Code
@bot.event
async def on_ready():
print(f"Bot is online as {bot.user}")

This is an "Event" everytime your Bot is up and running this code will give you the Message on terminal:

[Image: Bild-2025-08-05-132420688.png]

That was the first Part, your bot is now online!

Lets Write the First real command:

Code
@bot.command()
async def hello(ctx):
await ctx.send(f"Hi {ctx.author.name}, I am online!")

The @bot.command() shows the bot that this is a command Code

Code
async def hello(ctx):

We use async because wthe bot needs to work properly even when he does this command, because multiple people could do !(command) and he gotta do all these things in the same time!

The hello(ctx) is showing the bot that this command runs on !hello and (ctx) is the command for context, to show information as who sent the message, in what channel etc.

With the last bit of Code, the Bot gets explained what it needs to do, so he will send a message, with the information of ctx

The last bit:

Code
bot.run("Your_Token_Here!")

This is so the bot starts its job, when we will add new things (next few steps, this will always be the last Code Line!

Now test it by running the Command and writing !hello in the server :)

Here is the Code so you can see it fully written out, only try if something doesnt work!;

Spoiler (Click to View)


5. Lets Get Our Bot to Write Welcome Messages, when a new member joins!

The code for this is quite simple:

Code
@bot.event

again, this is an event handler, once an event happens this command runs and checks if it should do its job.

Code
async def on_member_join(member):


defines a new function that runs automatically when a new member joins the parameter "Member" contains all information about the Member like User name, Join Date, Profile Pic etc.

Code
channel = discord.utils.get(member.guild.text_channels, name="Welcome")

This code looks through all the Channels of the Server until it finds Welcome
member.guild refers the bot to the server the member joined, .txt_channels loks through all txt channels
discord.utils.get(...) looks for a channel with name="Welcome" and returns it if found. If the channel does not exist, it returns None

Code
if channel:

This checks if the Channel from the previous line was found or not

Code
await channel.send(f"Welcome to the server, {member.mention}! 🎉")

sends a message in the found Channel, await is needed because its an asynchronous action (means it happens over the network) The member.mention, Tags the member @ so he gets notified :)

This is the easiest way of writing a Welcome Message in the Welcome Channel of your server! :D

Here is the Full code without my explaining text:

Spoiler (Click to View)

6. Automatically giving Roles when Joining


I think your starting to get the hang of it aswell :)

Because we already have the Welcome Message we can make this a little easier for us

This needs some Requirements!

You need to make a Join Role mine will be "Member"
The Role of your Bot needs to be Higher than then "Member Role"

[Image: Bild-2025-08-05-141359535.png]

Now lets get to the Code!

Remember, we still are on the Event handler of a joining member, we are just adding this to the previously written code

Code
role = discord.utils.get(member.guild.roles, name="Member")

member.guild.roles looks through the roles of the server the member joined into, if it finds "Member" it adds Member as variable to role if it doesnt exist, it will save role as "none"

Code
if role:

Checks if the role has been found!

Code
await member.add_roles(role)

This assigns the new Member the variable used in "role" this aswell is a network command, so "await" is needed!

Once again this is the Code we now used:

Spoiler (Click to View)


7.Tips and Tricks:


1. Use intents correctly!

If your planning on making a bot public to everyone, selling it even you should only give it the intents it needs
This was just a Test, so it was no problem giving him Every right :)
2.Keep your Token a secret!
Never share your bot token.
If your token gets leaked, regenerate it immediately.
3.Be Async aware!
Most Discord actions (sending messages, editing, etc.) are asynchronous.
Always use await where needed or your bot will crash.
4. Use F-Strings for Dynamic Text
You might have Noticed we use f"Hello {user.name}"
this makes building messages easier and cleaner!
5. Test in a private Server!
Just like we did here! You dont want to give a broken product away :)
6. Lastly, Read the Discord Help Docs!
The discord.py documentation is extremely helpful.
You’ll learn new features and best practices just by browsing it.
https://discordpy.readthedocs.io/en/stable/


Final Words
This was my first real Python guide, from the stuff i have learned, it was very fun writing this Thread and i hope some will find interest and knowledge in here they didnt have before!
I will progress in this Series, soon, talking about Securing your Bot properly, What are Cogs?, How to make an Interface and so on :)

I wish you all a nice day and hopefully have some fun with the information provided!



7's word on the best thread in 24hours (Click to View)
The risk of not taking risk is riskier than comfort.

Never Deal Off-Site!
i really appreciate the way you explained what each code is telling the other. its really helpful and i will be trying this soon.
Good tutorial for people who want to learn to write an Discord Bot in python mate! But I’ll stand by discord.js haha
Thanks for your detailed steps! 👍 Have a nice day~!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Node.JS] by VecturaAug 5, 2025 06:29 PM 3 127 Aug 5, 2025 06:29 PM
Last Post: HerrWinston
[Javascript]  by NOTHING MATTERSOct 3, 2017 10:49 AM 4 526 Oct 3, 2017 10:49 AM
Last Post: /wrists
[PHP] by VespeiApr 19, 2017 10:41 AM 0 305 Apr 19, 2017 10:41 AM
Last Post: Vespei