0%
Part 3 โ†’
Module 11 of 30

Moderation Commands

Build essential moderation tools: kick, ban, unban, and message clearing.

Why Moderation Matters

Every server needs moderation. Bots handle repetitive tasks like removing spam and managing troublemakers. Discord.py makes this easy with built-in methods.

PYTHON
@bot.command() @commands.has_permissions(kick_members=True) async def kick(ctx, member: discord.Member, *, reason="No reason"): await member.kick(reason=reason) await ctx.send(f"Kicked {member.mention}") @bot.command() @commands.has_permissions(ban_members=True) async def ban(ctx, member: discord.Member, *, reason="No reason"): await member.ban(reason=reason) await ctx.send(f"Banned {member.mention}") @bot.command() @commands.has_permissions(manage_messages=True) async def clear(ctx, amount: int = 5): await ctx.channel.purge(limit=amount + 1) await ctx.send(f"Cleared {amount} messages!", delete_after=3)

Q1: What does ctx.channel.purge() do?

Q2: Permission for member.ban()?

Module 12 of 30

Advanced Moderation

Mute system, warning system, and mod-logs.

PYTHON
@bot.command() @commands.has_permissions(moderate_members=True) async def mute(ctx, member: discord.Member, time: int, *, reason="No reason"): await member.timeout(discord.utils.utcnow() + datetime.timedelta(minutes=time), reason=reason) await ctx.send(f"Muted {member.mention} for {time} min")
Module 13 of 30

Roles Management

Add, remove, and manage roles.

PYTHON
@bot.command() @commands.has_permissions(manage_roles=True) async def addrole(ctx, member: discord.Member, role: discord.Role): await member.add_roles(role) await ctx.send(f"Added {role.name} to {member.name}")
Module 14 of 30

Database โ€” SQLite

Store persistent data with SQLite3.

PYTHON
import sqlite3 db = sqlite3.connect("bot.db") cursor = db.cursor() cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, discord_id TEXT, xp INTEGER)") db.commit()
Module 15 of 30

Database โ€” JSON

Lightweight storage with JSON files.

PYTHON
import json def load_data(): with open("data.json") as f: return json.load(f) def save_data(data): with open("data.json","w") as f: json.dump(data,f,indent=2)
Module 16 of 30

Leveling System

XP on message, level-ups, and leaderboard.

PYTHON
@bot.event async def on_message(message): if message.author.bot: return xp = random.randint(10, 25) cursor.execute("UPDATE users SET xp = xp + ? WHERE discord_id = ?", (xp, str(message.author.id))) db.commit() await bot.process_commands(message)
Module 17 of 30

Economy System

Wallet, daily rewards, and shop.

PYTHON
@bot.command() async def daily(ctx): reward = random.randint(50, 200) cursor.execute("UPDATE economy SET balance = balance + ? WHERE user_id = ?", (reward, str(ctx.author.id))) await ctx.send(f"You received {reward} coins!")
Module 18 of 30

Background Tasks

Automate with @tasks.loop().

PYTHON
from discord.ext import tasks @tasks.loop(minutes=10) async def change_status(): await bot.change_presence(activity=discord.Game(name="with KODO")) change_status.start()
Module 19 of 30

Voice Channels

Connect, play audio, and manage voice.

PYTHON
@bot.command() async def join(ctx): if ctx.author.voice: await ctx.author.voice.channel.connect() await ctx.send("Joined!")
Module 20 of 30

Practice Project 2

Build a moderation + leveling bot.

Requirements

Build a bot with: kick, ban, mute, clear, leveling system, SQLite database, and mod-logs channel.