Botland.lol

Made by MisterCoolGuy123

Introduction

Welcome to Botland.lol, your complete Discord API manual. Page 1 introduces the basics of bots, REST API, events, commands, embeds, webhooks, files, and voice features.

Getting Started

Create a bot in the Developer Portal. Install the library:

pip install discord.py

Basic Bot Code

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    print("Bot online")

@bot.command()
async def hello(ctx):
    await ctx.send("Hello!")

bot.run("TOKEN")

Events

@bot.event
async def on_member_join(member):
    channel = discord.utils.get(member.guild.text_channels, name="general")
    await channel.send(f"Welcome {member.mention}")

Commands

@bot.command()
async def add(ctx, a: int, b: int):
    await ctx.send(str(a+b))

Embeds

@bot.command()
async def info(ctx):
    embed = discord.Embed(title="Info", description="Example", color=0x00ff00)
    embed.add_field(name="Made By", value="MisterCoolGuy123")
    await ctx.send(embed=embed)

Files & Attachments

@bot.command()
async def sendfile(ctx):
    await ctx.send(file=discord.File("pic.png"))

Webhooks

import requests
url = "WEBHOOK_URL"
data = {"content": "Hello from Webhook!"}
requests.post(url, json=data)

Voice

@bot.command()
async def join(ctx):
    if ctx.author.voice:
        await ctx.author.voice.channel.connect()
@bot.command()
async def leave(ctx):
    if ctx.voice_client:
        await ctx.voice_client.disconnect()

Rate Limits

Discord restricts how often you can send requests. The REST API returns headers like X-RateLimit-Remaining. Exceeding limits gives HTTP 429.

OAuth2 Basics

OAuth2 lets bots join servers. Example invite link format:

https://discord.com/oauth2/authorize?client_id=APP_ID&scope=bot&permissions=8

OAuth2 Advanced

Authorization Code Grant Flow allows apps to log in users. After authorization, exchange code for a token:

import requests
url = "https://discord.com/api/oauth2/token"
data = {
  "client_id": "ID",
  "client_secret": "SECRET",
  "grant_type": "authorization_code",
  "code": "CODE",
  "redirect_uri": "https://yoursite/callback"
}
r = requests.post(url, data=data)
print(r.json())

Sharding

Large bots use shards to split guilds across connections.

bot = commands.AutoShardedBot(command_prefix="!")

Gateway

Discord uses WebSocket gateway with heartbeat ACK system. Example heartbeat:

{
  "op":1,
  "d":42
}

Slash Subcommands

@client.tree.command(name="math")
@app_commands.describe(a="First number", b="Second number")
async def math(interaction, a:int, b:int):
    await interaction.response.send_message(str(a+b))

Autocomplete

@math.autocomplete("a")
async def autocomplete_a(interaction, current):
    return [app_commands.Choice(name=str(i), value=i) for i in range(1,6)]

Voice Deep Dive

Voice uses RTP packets, Opus codec, and often FFmpeg for streaming audio.

vc = await channel.connect()
vc.play(discord.FFmpegPCMAudio("song.mp3"))

Libraries

discord.py-self (selfbots, against ToS but used for user automation):

pip install discord.py-self
import discord
client = discord.Client()
@client.event
async def on_ready():
    print("Logged in as", client.user)
client.run("USER_TOKEN")

py-cord (active fork with slash support):

pip install py-cord

nextcord, hikari, interactions.py also provide alternatives with similar APIs.

Projects

Example: Music Bot

@bot.command()
async def play(ctx, url):
    vc = await ctx.author.voice.channel.connect()
    vc.play(discord.FFmpegPCMAudio(url))

Experimental Features

Threads, stage channels, scheduled events, automod:

@client.tree.context_menu(name="UserID")
async def userid(interaction, user:discord.User):
    await interaction.response.send_message(str(user.id))