#How do I properly create an automod rule?

1 messages · Page 1 of 1 (latest)

rose silo
#

I am trying to create a new automod rule but the documentation so far is really a mess/not really helpful.

Right now I am using the following code:

        rule_name = "test"
        event_type = disnake.AutoModEventType.message_send
        trigger_type = disnake.AutoModTriggerType.keyword
        keywords = ['keyword, test']
        keyword_filter = keywords.split(',') # assuming keywords are separated by commas

        alert_channel = interaction.guild.get_channel(XXXX)

        action_to_do = [
            disnake.AutoModAction(type=disnake.AutoModAction(type=disnake.AutoModActionType.send_alert_message)),
            disnake.AutoModAction(type=disnake.AutoModAction(type=disnake.AutoModActionType.block_message))
        ]
        
        trigger_metadata = disnake.AutoModTriggerMetadata(keyword_filter=keyword_filter)
        
        enabled = True

        automod_rule = await interaction.guild.create_automod_rule(
            name=rule_name,
            event_type=event_type, 
            enabled=enabled,
            trigger_type=trigger_type,
            trigger_metadata=trigger_metadata,
            actions= action_to_do + [disnake.AutoModTimeoutAction(stored[interaction.message.id]['timeout'])] +
            [disnake.AutoModAction(type=disnake.AutoModSendAlertAction(alert_channel))],
            exempt_roles=stored[interaction.message.id]['exempt_roles'],
            exempt_channels=stored[interaction.message.id]['exempt_channels']
        )
        await interaction.response.send_message(f"Successfully created the automod rule `{rule_name}`", ephemeral=True)
#

However, I always end up in the following error:

Traceback (most recent call last):
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/ui/view.py", line 385, in _scheduled_task
    await item.callback(interaction)
  File "/home/bot_name/cogs/automod.py", line 409, in done_button
    automod_rule = await interaction.guild.create_automod_rule(
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/guild.py", line 4573, in create_automod_rule
    actions=[a.to_dict() for a in actions],
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/guild.py", line 4573, in <listcomp>
    actions=[a.to_dict() for a in actions],
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/automod.py", line 108, in to_dict
    "type": self.type.value,
AttributeError: 'AutoModAction' object has no attribute 'value'

Not really helpful and I am kind of stuck at this point. Does anyone see where I went wrong? Can I not use "AutoModAction" at all?

fervent scroll
#

This class is not meant to be instantiated by the user. The user-constructible subclasses are:

AutoModBlockMessageAction

AutoModSendAlertAction

AutoModTimeoutAction
#

never used it, but give me a second to try something

#

Yeah. Looks like you need to set your actions_to_do properly.

action_to_do = [
  disnake.AutoModSendAlertAction(channel=alert_channel),
  disnake.AutoModBlockMessageAction()
]
#

Also this should be causing an error:

keywords = ['keyword, test']
keyword_filter = keywords.split(',')

keywords is already a list, so you can't use split() against a list

rose silo
#

Thanks for giving it a try. Just trying to understand the whole thing more and more.

Changed the code to

action_to_do = [
            disnake.AutoModSendAlertAction(channel=alert_channel),
            disnake.AutoModBlockMessageAction()
        ]

and removed the keyword error. Still new issues pop-up:

Traceback (most recent call last):
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/ui/view.py", line 385, in _scheduled_task
    await item.callback(interaction)
  File "/home/bot_name/cogs/automod.py", line 406, in done_button
    automod_rule = await interaction.guild.create_automod_rule(
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/guild.py", line 4573, in create_automod_rule
    actions=[a.to_dict() for a in actions],
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/guild.py", line 4573, in <listcomp>
    actions=[a.to_dict() for a in actions],
  File "/home/bot_name/venv/lib/python3.9/site-packages/disnake/automod.py", line 108, in to_dict
    "type": self.type.value,
AttributeError: 'AutoModSendAlertAction' object has no attribute 'value'

Since the logger is once again not really helpful here, I still assume it has to do with the "actions" parameter but now rather with:

actions= action_to_do + [disnake.AutoModTimeoutAction(stored[interaction.message.id]['timeout'])] +
            [disnake.AutoModAction(type=disnake.AutoModSendAlertAction(alert_channel))]
#

Looks like it's the same error as above which just gives me a headache

#

Looks like I might just need to remove the second mention of "AutoModSendAlertAction" (within action)

fervent scroll
#

not sure why you're doing actions like that anyway. You already have actions_to_do, why not just add all the actions there and pass one variable to the actions kwarg instead of what you're currently doing?

rose silo
#

To be fair: I got really confused by what to use and what not to use. I looked into the docs for quite a while until I found at least some examples and then kind of mixed them up

#

But you are right, yes

fervent scroll
#

the red box is what is throwing the error, though.'