Message Handling & Translate Command

This is where the magic happens! The bot watches every message and automatically translates for people who need it. Plus, there’s a backup /translate command.

Let the Bot Read Messages

First, we need permission to read messages:

# Tell Discord we want to read messages
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

The Auto-Translation Code

This runs every time someone sends a message:

@bot.event
async def on_message(message):
    # Don't translate bot messages
    if message.author.bot:
        return
    
    # Skip super short messages (less than 3 letters)
    if len(message.content.strip()) < 3:
        return
    
    # If nobody signed up, don't bother
    if not user_languages:
        return

    # Figure out what language this message is
    detected_lang = detect_language(message.content)
    if detected_lang not in ["en", "es"]:
        return

    # Check each person who signed up
    for uid, preferred_lang in user_languages.items():
        # Don't send people their own messages
        if uid == message.author.id:
            continue
        
        # Skip if they already speak this language
        if detected_lang == preferred_lang:
            continue

        # Translate it!
        translation = translate_text(message.content, detected_lang, preferred_lang)
        if translation:
            # Find the user
            user = bot.get_user(uid)
            if user is None:
                try:
                    user = await bot.fetch_user(uid)
                except:
                    continue
            if not user:
                continue
            
            # Send them a DM
            try:
                original_lang_name = "English" if detected_lang == "en" else "Spanish"
                target_lang_name = "English" if preferred_lang == "en" else "Spanish"
                dm_text = (
                    f"**Translation** ({original_lang_name} -> {target_lang_name})\n"
                    f"**Original:** {message.content}\n"
                    f"**Translation:** {translation}\n"
                    f"*From {message.author.display_name} in #{message.channel.name}*\n\n"
                )
                await user.send(dm_text)
            except:
                pass  # If DM fails, just skip it

What It Does Step by Step

  1. Filter out junk:

    • Skip bot messages (no infinite loops!)
    • Skip tiny messages (like “ok” or “hi”)
    • Skip if nobody signed up
  2. Check the language:

    • Use detect_language() to figure it out
    • Only continue if it’s English or Spanish
  3. Loop through users:

    • Check each person who signed up
    • Skip the person who sent the message
    • Skip people who already speak that language
  4. Send translations:

    • Translate to their preferred language
    • Find them on Discord
    • Send them a nice DM with the translation

The Manual /translate Command

Sometimes you just want to translate something quickly:

@bot.tree.command(name="translate", description="Manually translate English ↔ Spanish")
@app_commands.describe(
    phrase="What do you want to translate?",
    translate_to="english or spanish (default: spanish)"
)
async def translate_command(interaction: discord.Interaction, phrase: str, translate_to: str | None = None):
    # Show "thinking..." while we work
    await interaction.response.defer(thinking=True)
    
    # Figure out which way to translate
    target = (translate_to or "spanish").lower()
    if target in ("spanish", "es"):
        from_lang, to_lang, label = "en", "es", "Spanish"
    elif target in ("english", "en"):
        from_lang, to_lang, label = "es", "en", "English"
    else:
        await interaction.followup.send("Please choose 'english' or 'spanish'")
        return
    
    # Do the translation
    result = translate_text(phrase, from_lang, to_lang)
    if result:
        await interaction.followup.send(f"{label}: {result}")
    else:
        await interaction.followup.send("Translation failed. Is the server running?")

Cool Features

What the DM Looks Like

When someone writes “¡Buenos días amigos!”, English speakers get this DM:

**Translation** (Spanish -> English)
**Original:** ¡Buenos días amigos!
**Translation:** Good morning friends!
*From UserName in #general*

Testing Everything

Test Auto-Translation

  1. Person A: /register_language -> picks Spanish
  2. Person B: /register_language -> picks English
  3. Person A types: “Hello everyone!”
    • Person B gets a Spanish translation in DMs
  4. Person B types: “¡Gracias!”
    • Person A gets an English translation in DMs

Test Manual Translation

/translate phrase:"Hello world"
-> Spanish: ¡Hola Mundo!

/translate phrase:"Buenos dias" translate_to:english
-> English: Good morning

/translate phrase:"How are you?"
-> Spanish: ¿Cómo estás?

Common Problems

What’s WrongWhyHow to Fix
No auto-translationMessage Content intent is offTurn it on in Developer Portal
No DMs arrivingUser has DMs disabledThey need to allow server DMs
Slow translationsServer is busyBe patient or restart LibreTranslate
Bot translates itselfMissing bot checkMake sure the bot check is there