Setting up a Discord bot - dairyking98/leonards-wiki GitHub Wiki

Discord Bots and Moderation Setup

Discord bots are essentially automated programs that connect to Discord’s servers via their API to listen for events (like messages, member joins, etc.) and respond accordingly. They run continuously, monitoring activities and executing programmed tasks. Below is a comprehensive guide on how they work, how to set one up, and options for built-in moderation tools like MEE6 and Dyno.


How Discord Bots Work

API Connection

  • Bots use Discord’s API, primarily via WebSockets for real-time events and REST endpoints for actions (sending messages, banning users, etc.).

Event-Driven Architecture

  • They “listen” to events such as new messages, user joins, or reactions.
  • When a specified event occurs, the bot triggers a corresponding function to handle it.

Programming Libraries

  • Developers typically use libraries like discord.py for Python or discord.js for Node.js to simplify interacting with Discord’s API.

Permissions

  • Bots require proper permissions to operate.
  • When you create a bot, you generate an invite link with specific permissions (like message deletion or user banning) that must be assigned when adding the bot to your server.

Setting Up a Discord Bot

1. Create an Application

  • Visit: Discord Developer Portal
  • Steps:
    • Create a new application.
    • Add a bot user.
    • Copy the bot’s token (keep it secure as it’s like your bot’s password).

2. Invite the Bot

  • Generate an Invite URL:
    • Include the necessary permissions.
  • Invite: Use the URL to add the bot to your Discord server.

3. Develop the Bot

  • Choose a Programming Language:
    e.g., Python with discord.py.
  • Write Code:
    Program the bot to listen for events (like new messages) and incorporate logic to detect spam.

Implementing Anti-Spam Features

To protect your server from spam infiltrators, incorporate anti-spam logic using strategies like:

Rate Limiting

  • Track how many messages a user sends within a set time window.
  • If the threshold is exceeded, take action (e.g., warn, delete messages, or mute).

Pattern Recognition

  • Identify common spam patterns (e.g., repeated messages, identical content, or excessive mentions).

Automated Actions

  • Depending on the severity, configure the bot to delete spam messages, issue warnings, or kick/ban repeat offenders.

Sample Code in Python (Using discord.py)

import discord
from discord.ext import commands
import time
from collections import defaultdict

# Set the command prefix, for example '!'
bot = commands.Bot(command_prefix='!')

# Dictionary to track message timestamps for each user
user_message_times = defaultdict(list)

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

@bot.event
async def on_message(message):
    # Ignore messages from bots
    if message.author.bot:
        return

    current_time = time.time()
    user_id = message.author.id

    # Record the timestamp of the current message
    user_message_times[user_id].append(current_time)

    # Keep only the timestamps within the last 10 seconds
    user_message_times[user_id] = [
        t for t in user_message_times[user_id] if current_time - t < 10
    ]

    # If the user sent more than 5 messages in 10 seconds, consider it spam
    if len(user_message_times[user_id]) > 5:
        await message.channel.send(f"{message.author.mention}, please slow down!")
        await message.delete()
        # Optionally, you can also mute or kick the user if spam continues.

    # Process other commands if any
    await bot.process_commands(message)

# Replace 'YOUR_BOT_TOKEN' with your actual bot token
bot.run('YOUR_BOT_TOKEN')

Explanation

Tracking Messages

  • Uses a dictionary to store message timestamps per user.

Spam Detection

  • Checks if more than 5 messages are sent within 10 seconds, then triggers a spam response by deleting the message and sending a warning.

Extensibility

  • The logic can be expanded to escalate penalties (e.g., muting or kicking) for repeated offenses.

Hosting and Maintenance

Local or Cloud Hosting

  • Run your bot locally on your machine or host it on platforms like Heroku, AWS, or DigitalOcean for continuous operation.

Security

  • Always keep your bot token secure.
  • Use environment variables or secret management tools instead of hardcoding the token.

Monitoring & Logging

  • Implement logging to track the bot's actions and errors, which helps in troubleshooting and improving spam detection logic.

Alternatives and Recommendations

If building your own bot seems daunting, consider using well-maintained moderation bots like MEE6 or Dyno, which offer robust anti-spam and moderation features out of the box.


Setting Up Moderation Bots: MEE6 and Dyno

Setting Up MEE6

  1. Visit the MEE6 Website:
    mee6.xyz

  2. Log In with Discord:
    Click the Login button and sign in using your Discord account. This authorizes MEE6.

  3. Add MEE6 to Your Server:

    • Click Add to Discord.
    • Choose the server where you have "Manage Server" permissions.
    • Grant the necessary permissions (e.g., managing messages and roles).
  4. Configure Moderation Settings:

    • Access the MEE6 dashboard for your server.
    • Navigate to the Moderation plugin section.
    • Enable anti-spam features and customize thresholds (e.g., message limits, warnings, mutes, or bans).
    • Optionally set up automated actions and custom commands.
  5. Test and Monitor:

    • Test the bot’s functionality in a controlled channel.
    • Monitor logs on the dashboard for any unusual activity.

Setting Up Dyno

  1. Visit the Dyno Website:
    dyno.gg

  2. Log In with Discord:
    Click Login and sign in with your Discord credentials.

  3. Invite Dyno to Your Server:

    • Click Add to Server.
    • Select the server you want Dyno to manage (ensure you have the necessary permissions).
    • Approve the bot’s permissions (usually includes managing messages, roles, and channels).
  4. Configure Anti-Spam and Moderation Settings:

    • Access Dyno’s dashboard.
    • Go to the Modules or Moderation section.
    • Enable the anti-spam module and adjust settings such as message frequency limits, automated warnings, mutes, or bans.
    • Configure additional moderation features (e.g., auto-moderation, custom commands, logging).
  5. Finalize Setup:

    • Save your settings.
    • Test Dyno in your server to ensure proper identification and handling of spam.
    • Regularly update settings based on your server’s activity.

Comparing MEE6 and Dyno

Feature Focus

  • MEE6:

    • Engagement & Gamification:
      Known for its leveling system, custom commands, and features that boost user participation.
    • Moderation:
      Offers auto-moderation tools like spam filters and message moderation, with advanced features available via premium subscriptions.
  • Dyno:

    • Moderation & Server Management:
      Focuses on strong moderation and administrative control with robust automod filters, customizable commands, and detailed logging.
    • Utility Features:
      Includes modules for announcements, auto-roles, and various server management tools.

Customization & User Interface

  • MEE6:

    • Intuitive dashboard for easy configuration of plugins, commands, and moderation settings.
    • Extensive personalization options.
  • Dyno:

    • Granular control over moderation settings and logging.
    • Straightforward setup process praised by many server administrators.

Pricing and Premium Features

  • MEE6:

    • The free version covers basic moderation and engagement.
    • Advanced features require a premium subscription.
  • Dyno:

    • Offers a robust free tier with strong moderation tools.
    • Premium features are available, but the free version is often sufficient.

Community & Support

  • MEE6:

    • Attracts communities focused on engagement and gamification.
    • Extensive documentation and support resources.
  • Dyno:

    • Favored by servers emphasizing strict moderation and detailed logging.
    • Active support team and comprehensive guides.

In Summary

  • Use MEE6 if you want:

    • A balanced mix of moderation and community engagement tools.
    • Features like leveling, custom commands, and interactive elements to boost participation.
  • Use Dyno if you want:

    • A bot focused on robust moderation, detailed logging, and server management.
    • A strong free toolset for handling spam and other disruptions without needing premium features.

Ultimately, the best choice depends on your server’s needs—whether you're aiming to foster an engaging community or prioritize tight moderation and control.