Skip to content
← All tutorials

A moderation bot with discord.py

Filter banned words, warn members and give moderators slash commands.

Start from the template

  1. Create a bot from the Moderation template and choose Python (discord.py).
  2. In Test, send a message containing badword as alex — it is deleted and a warning appears.
  3. Switch Acting as to moddy and try again: moderators are exempt.
  4. Try /kick as alex: the sandbox shows that members without Kick Members can't use it.

The core idea

Reading message text needs the privileged Message Content Intent. A minimal filter looks like this:

import os
import discord

BANNED = {"badword", "scamlink"}
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_message(message: discord.Message):
    if message.author.bot or message.guild is None:
        return
    if any(word in message.content.lower() for word in BANNED):
        await message.delete()
        await message.channel.send(f"{message.author.mention}, that word is not allowed here.")

client.run(os.environ["DISCORD_TOKEN"])

Make it yours

Open AI edit and ask, for example: “Add the words spam and scam to the banned list and time people out for 5 minutes instead of warning them.” Review the change before applying it.

Good practice

  • Keep moderation commands restricted with required permissions.
  • Log actions to a private #mod-log channel.
  • Remember that the bot can't moderate the server owner or members with a higher role.

Ready to build your first bot?

Create a bot — free