Loading...
Loading...
Build Discord bots using discord.py. Use when the user asks about Discord bot development, discord.py library, bot commands, slash commands, Discord intents, or Discord API integration. Covers Client, Bot, events, commands extension, app_commands, views, buttons, modals, and cogs.
npx skill4agent add frizzle-chan/mudd discord-pyimport discord
intents = discord.Intents.default()
intents.message_content = True # Required for reading message content
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'Logged in as {client.user}')
@client.event
async def on_message(message):
if message.author == client.user:
return # Ignore self
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('YOUR_BOT_TOKEN')discord.pyintents = discord.Intents.default() # Common intents, excludes privilegedintents = discord.Intents.default()
intents.message_content = True # Read message text (privileged)
intents.members = True # Member join/leave events (privileged)
intents.presences = True # Status updates (privileged)| Use Case | Class | Import |
|---|---|---|
| Basic events, no commands | | |
| Prefix commands (!help) | | |
| Slash commands | Either + | |
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command()
async def ping(ctx):
await ctx.send('Pong!')
bot.run('TOKEN')@client.event@bot.event| Event | When it fires |
|---|---|
| Bot connected and cache ready |
| Message received |
| User joined guild (needs members intent) |
| User left guild |
| Reaction added |
| Bot joined a server |
| Uncaught exception in event |
from discord.ext import commands
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command()
async def greet(ctx, name: str):
"""Greet someone by name."""
await ctx.send(f'Hello, {name}!')
@bot.command(name='add')
async def add_numbers(ctx, a: int, b: int):
"""Add two numbers."""
await ctx.send(f'{a} + {b} = {a + b}')@bot.group()
async def settings(ctx):
if ctx.invoked_subcommand is None:
await ctx.send('Use !settings <subcommand>')
@settings.command()
async def show(ctx):
await ctx.send('Current settings: ...')import discord
from discord import app_commands
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@tree.command(name='ping', description='Check bot latency')
async def ping(interaction: discord.Interaction):
await interaction.response.send_message(f'Pong! {round(client.latency * 1000)}ms')
@client.event
async def on_ready():
await tree.sync() # Sync commands with Discord
print(f'Synced commands for {client.user}')
client.run('TOKEN')@tree.command(name='greet', description='Greet a user')
@app_commands.describe(user='The user to greet')
async def greet(interaction: discord.Interaction, user: discord.Member):
await interaction.response.send_message(f'Hello, {user.mention}!')# In event handler
await message.channel.send('Hello!')
await message.channel.send('With embed', embed=embed)
await message.channel.send('With file', file=discord.File('image.png'))
# Reply to message
await message.reply('Replying to you!')
# In slash command
await interaction.response.send_message('Response')
await interaction.response.send_message('Only you see this', ephemeral=True)
# Edit/followup for slash commands
await interaction.response.defer()
await interaction.followup.send('Delayed response')@bot.command()
@commands.is_owner()
async def shutdown(ctx):
await ctx.send('Shutting down...')
await bot.close()@bot.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int):
await ctx.channel.purge(limit=amount + 1)@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send('You lack permissions for this command.')
elif isinstance(error, commands.CommandNotFound):
pass # Ignore unknown commands
else:
raise error# Core API reference
WebFetch: https://discordpy.readthedocs.io/en/latest/api.html
# Commands extension
WebFetch: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html
# Slash commands (app_commands)
WebFetch: https://discordpy.readthedocs.io/en/latest/interactions/api.html
# Intents guide
WebFetch: https://discordpy.readthedocs.io/en/latest/intents.html
# Quickstart guide
WebFetch: https://discordpy.readthedocs.io/en/latest/quickstart.html
# Frequently asked questions
WebFetch: https://discordpy.readthedocs.io/en/latest/faq.html