Whether you’re automating community tasks or integrating Discord with external APIs, this guide walks you through creating a bot from scratch using Python and the latest Discord developer tools.
Step 1: Create a Discord Application
- Go to Discord Developer Portal and log in.
- Click “New Application” and give your bot a name.
- Navigate to the Bot tab and click “Add Bot”.
- Copy the Bot Token and store it securely. Do not hardcode it—use environment variables.
Step 2: Invite Your Bot to a Server
- Go to the OAuth2 > URL Generator section.
- Select the
botscope. - Choose required permissions (e.g., Send Messages, Attach Files, Read Message History).
- Copy the generated URL and open it in your browser to invite the bot to your server.
Step 3: Configure Your Bot Code
Create a .env file to store your token securely:
DISCORD_TOKEN=your_bot_token_here
In your bot.py file:
import discord
import os
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
@client.event
async def on_ready():
print(f'Connected to Discord as {client.user}')
client.run(TOKEN)
Step 4: Install Required Libraries
Run the following command in your terminal:
pip install discord.py python-dotenv
Note: As of 2025, discord.py is maintained under Pycord. Consider switching if you need slash command support.
Step 5: Run Your Bot
python bot.py
Expected output:
Connected to Discord as BotName#1234
Step 6: Upload Images via Command
Here’s a basic command handler using discord.ext.commands:
from discord.ext import commands
bot = commands.Bot(command_prefix='/')
@bot.command()
async def upload_images(ctx, channel_name: str, image_path: str):
channel = discord.utils.get(ctx.guild.channels, name=channel_name)
if channel:
with open(image_path, 'rb') as f:
await channel.send(file=discord.File(f))
else:
await ctx.send("Channel not found.")
bot.run(TOKEN)
FAQ
| Question | Answer |
|---|---|
| Can I use slash commands instead of prefix commands? | Yes. Use discord.app_commands or switch to Pycord for full support. |
| How do I keep my bot online 24/7? | Host it on platforms like Railway, Render, or a VPS with systemd. |
| Can I upload multiple images at once? | Yes, loop through a list of file paths and send them sequentially. |
Glossary
| Term | Definition |
|---|---|
| Bot Token | A secret key used to authenticate your bot with Discord |
| OAuth2 | Protocol used to authorize your bot to join servers |
| discord.py | Python library for interacting with Discord’s API |
| Pycord | A modern fork of discord.py with active development |
| Slash Commands | Modern Discord commands triggered by a forward slash |
Gear We Recommend
Testing configs is easier with a dedicated admin machine set up right. Here’s the kit we use.
Browse our Windows Admin Toolkit picks on AmazonAs an Amazon Associate, TechyGeeksHome earns from qualifying purchases.
Discover more from TechyGeeksHome
Subscribe to get the latest posts sent to your email.