[Python] Python Bot That Repeats Deleted Messages on Discord (Issue)

Discussão em 'Python' iniciado por Stack, Janeiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote a Discord bot that is supposed to repeat deleted messages from users. It responds to a ping without issue, and composes a message if someone deletes but does not pull the content of the message that is deleted. The only time it is pulling the content of a deleted message is if it is pinged in the message. Ex: "@bot, this is a deleted message" this will be repeated by the bot, but "this is a deleted message" will spit out a no content error. I am a novice at Python and would love any pointers. Code below with comments. Thank you.

    import discord
    import os
    from flask.app import cli
    from keep_alive import keep_alive
    from discord.ext import commands

    intents = discord.Intents.default()
    intents.message_content = True
    intents.messages = True
    client = discord.Client(intents=discord.Intents.default())
    token = os.getenv('token.env')

    # Event to detect and log deleted messages
    @client.event
    async def on_message_delete(message):

    # Response message
    response = (
    f" **Deleted Message Alert** \n"
    f"User: {message.author}\n"
    f"{message.content or 'No text content'}"
    )

    # Ignore messages sent by bots
    if message.author.bot:
    return

    # Send the response to the same channel
    await message.channel.send(response)

    # Event to detect when the bot is pinged
    @client.event
    async def on_message(message):
    # Check if the bot is mentioned
    if client.user in message.mentions:
    await message.channel.send("Hello!")

    # Ensure other commands and events still work
    await client.process_commands(message)

    # Log when the bot is ready
    @client.event
    async def on_ready():
    print(f"Logged in as {client.user.name} - {client.user.id}")

    #server activation
    keep_alive()

    # Runs the bot
    client.run(os.getenv("TOKEN"))


    Tried other suggestions from sources found online, which either killed the code entirely or had the same error.

    Re-wrote and formatted the code differently with no impact/change.

    Took sections out to test if the "event to detect when the bot is pinged" is causing issue, no change other than bot did not respond to a ping.

    Also removed the no text content section which just produced a blank message.

    EDIT: I have asked an LLM its opinion on the code and have gotten nothing helpful.

    Continue reading...

Compartilhe esta Página