pythondiscorddiscord.py

Assistance for detecting when discord automod is activated (discord.py)


I have a automod setup on my discord server. I want to expand the automod on the discord, and I setup my bot to detected, yet I cannot find any sort of documentation. Or anything to detect the automod


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

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

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


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

Is my code

I have attempted to use on_automod_action_execution, I have checked the discord.py documentation https://discordpy.readthedocs.io/en/stable/ and do not find a way to detect when its triggered


Solution

  • Welcome to Stack Overflow!

    The issue with your code is that for your bot event, you need to use one of Discord.py's event triggers. Your on_automod_action_execution is not a trigger, nor does it exist anywhere in the documentation.

    But, if you look around a bit you'll find on_automod_action (which is the same thing) here. The trigger takes an argument execution which is a discord.AutoModAction object. Here's your fixed function:

    @bot.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}")
                # You can add more attributes to display under the event's attributes in the documentation such as messages and channel's id and etc
            except Exception as e:
                print(f"Failed to handle AutoMod event: {e}")
    

    Here's an example of it working: I added a custom keyword filter to AutoMod to detect the phrase "bazinga". (Note that AutoMod doesn't work on Moderators, yes I used an alt account)enter image description hereenter image description here