Step-by-Step Installation
Follow these steps to install Python and get started with creating your own Discord bot using discord.py:
Step 1: Install Python
1. Update the package list:
sudo apt update
2. Install Python:
sudo apt install python3 -y
3. Verify Python installation:
python3 --version
Step 2: Install pip (Python Package Installer)
1. Install pip:
sudo apt install python3-pip -y
2. Verify pip installation:
pip3 --version
Step 3: Install discord.py
1. Install discord.py:
pip3 install discord.py
Discord Bot Tutorial: Ping Command with Slash Commands
Follow the steps below to create a simple Discord bot with a ping command using slash commands.
Directory Structure:
.
└── discord_bot
├── bot.py
└── cogs
└── ping.py
Step 1: Create the main bot file (bot.py):
import discord
from discord.ext import commands
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}!')
bot.load_extension("cogs.ping")
bot.run('YOUR_BOT_TOKEN')
Step 2: Create the ping command in the 'cogs' folder (ping.py):
from discord.ext import commands
from discord import app_commands
class Ping(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print("Ping Cog Loaded")
@app_commands.command(name="ping", description="Check bot latency.")
async def ping(self, interaction: discord.Interaction):
latency = round(self.bot.latency * 1000)
await interaction.response.send_message(f"Pong! Latency: {latency}ms")
async def setup(bot):
await bot.add_cog(Ping(bot))
Step 3: Run the bot using:
python3 bot.py