Skip to main content

Command Palette

Search for a command to run...

Server moderation automation

Published
3 min readView as Markdown
L
I build and operate a fleet of 7 self-improving Python bots that trade crypto derivatives, scan freelance markets, and generate digital products automatically. Writing about trading bot architecture, web scraping, API integrations, and autonomous agents. All source code available at payhip.com/botfarm.

Managing a growing online community can be quite a challenge, especially when you're dealing with spammers, trolls, and just plain chaos. That's where automation can save you a lot of headaches. I've been exploring some ways to automate server moderation using bots, and I thought I'd share some of the insights I've gathered, along with a bit of code to get you started.

Setting Up the Bot

First things first, you'll need a bot running on your server. In this example, I'm using Python's discord.py library for Discord, but the concepts are quite similar for Telegram as well. Below is a simple setup to connect your bot to a server and listen for messages:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.messages = True

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')

TOKEN = 'YOUR_BOT_TOKEN'
bot.run(TOKEN)

Once your bot is up and running, it can listen to messages and react according to the rules you define.

Implementing Auto-Moderation

The cornerstone of moderation automation is creating rules that the bot can enforce. For example, you might want to delete messages that contain certain banned words. Here's a simple example of how you could achieve that:

banned_words = ['spamword1', 'spamword2']

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    # Check for banned words
    if any(word in message.content.lower() for word in banned_words):
        await message.delete()
        await message.channel.send(f"{message.author.mention}, that kind of language isn't allowed here.")

    await bot.process_commands(message)

This snippet will automatically delete any message containing words from the banned_words list and notify the user.

Advanced Features

As your server grows, you might find that simple word filtering isn't enough. This is where more advanced features like user tracking and spam pattern recognition come into play. For example, creating a record of user activity can help you identify problematic users over time.

from collections import defaultdict

user_message_counts = defaultdict(int)

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    # Track user message count
    user_message_counts[message.author.id] += 1

    # Implement additional logic here

    await bot.process_commands(message)

With this setup, you can start building more complex rules, like temporarily muting users who exceed a certain number of messages in a short period.

Bringing It All Together

While the examples above give you a taste of what's possible, building a robust moderation system can become complex. I actually packaged my approach into a tool called Discord & Telegram Bot, which simplifies a lot of this process. It provides a unified framework for both Discord and Telegram, along with modular command handlers and webhook integrations.

By using such a framework, you not only save time but also gain a lot of flexibility in how you manage your community. Whether you're looking to automate simple tasks or build a complex admin system, having a strong foundation can make all the difference.

Moderating a server doesn't have to be a burden. With the right tools and some clever coding, you can automate much of the workload and focus on what really matters: building a thriving, positive community.

Also available on Payhip with instant PayPal checkout.


If you need a server to run your bots 24/7, I use DigitalOcean — $200 free credit for new accounts.

More from this blog

P

Python Bots & Automation

27 posts