2025, Dec 07 23:00

How to Capture Discord.py AutoMod Events: Correct the Listener to on_automod_action and Log Data

Learn why on_automod_action_execution never fires in Discord.py and fix your AutoMod listener with on_automod_action. Log rule hits, user IDs and matches

Expanding AutoMod on a Discord server often starts with one simple need: detect when an AutoMod rule fires so you can log, alert, or react. If your handler never triggers, the reason may be more mundane than configuration—an incorrect event name in your Discord.py listener.

Problem

A bot is set up with full intents, and a listener tries to capture AutoMod executions. However, nothing fires. The core of the issue is a mismatched event name that isn’t part of Discord.py’s event system.

import discord
from discord.ext import commands
from discord import app_commands

flags = discord.Intents.all()
client = commands.Bot(command_prefix=".", intents=flags)

@client.event
async def on_ready():
    print(f"Signed in as {client.user.name} ({client.user.id})")


@client.event
async def on_automod_action_execution(evt: discord.AutoModAction):
    try:
        print("AutoMod Action Executed")
        print(f"Rule ID: {evt.rule_id}")
        print(f"User ID: {evt.user_id}")
        print(f"Matched Content: {evt.matched_content}")
        print(f"Matched Keyword: {evt.matched_keyword}")
        print(f"Content: {evt.content}")
    except Exception as err:
        print(f"Failed to handle AutoMod event: {err}")

Why this fails

Discord.py relies on predefined event names. The function name on_automod_action_execution is not a recognized trigger and does not appear in the official documentation, so the coroutine never runs. The correct event is on_automod_action, documented here: discord.on_automod_action. The callback receives a single argument that is a discord.AutoModAction object with details such as the rule and the matched content.

Solution

Use the documented event name and accept the execution object. The rest of the logic—reading fields from the payload and printing them—can stay the same.

import discord
from discord.ext import commands
from discord import app_commands

all_intents = discord.Intents.all()
runner = commands.Bot(command_prefix=".", intents=all_intents)

@runner.event
async def on_ready():
    print(f"Logged in as {runner.user.name} ({runner.user.id})")


@runner.event
async def on_automod_action(execution: discord.AutoModAction):
    try:
        print("AutoMod Action Executed")
        print(f"Rule ID: {execution.rule_id}")
        print(f"User ID: {execution.user_id}")
        print(f"Matched Content: {execution.matched_content}")
        print(f"Matched Keyword: {execution.matched_keyword}")
        print(f"Content: {execution.content}")
    except Exception as exc:
        print(f"Failed to handle AutoMod event: {exc}")

What to keep in mind

The event name must match exactly, otherwise the handler will never run. The argument passed into the event is your discord.AutoModAction payload, which exposes the fields being printed above. This pattern works, for example, with a keyword filter set to catch a specific phrase; a custom keyword such as “bazinga” triggers the event and the output includes the matched content.

If you observe each AutoMod action being logged twice, verify that the process isn’t started more than once or that the event handler isn’t defined in duplicate. AutoMod doesn’t work on Moderators, so use a non-moderator account when testing rule matches.

Why this matters

Correctly wiring the AutoMod listener is foundational for building moderation workflows: auditing rule hits, notifying staff, or enriching moderation logs with context. Without the proper event, none of those downstream pieces will activate.

Wrap-up

Use on_automod_action to detect AutoMod triggers in Discord.py and read details from the discord.AutoModAction object. Test with a non-moderator account to confirm behavior, and ensure the bot runs as a single instance to avoid duplicated outputs. For additional attributes available on the payload, refer to the official documentation linked above and print what you need for your moderation pipeline.