Skip to content

Commit b9c211f

Browse files
authored
Implement automod extension (#40)
* WIP * Revise errors * More adjustments and fixes * Update dependencies * Update changelog * Remove debugging code for #39 * Implement message_delete event * Fix AutomodEventBase.format_content * Rename message_pinged -> message_mentioned * More renaming and abstractions * Consolidate data encoding and make string templates safe * Implement the ability to enable/disable rules * Implement configurable logging with error reporting * Implement new triggers, conditions, and actions * Remove useless MemberBanned event/trigger * Implement generic reaction trigger * Fix incorrect use of member field access * Seriously * Extend and improve existing conditions * Add missing docs to SendMessage action * Implement more actions * Run rules in parallel * Adjust exception sub-classing * Implement `any_of` and `all_of` conditions * Reorganize utils * Implement more conditions * Extend and improve ExtendedJsonEncoder * Fix TargetRolesBase deserialization * Fix TargetIsNotBotBase check * Adjust docs for AutomodRule * Extend and improve error logging
1 parent 3ef8d7e commit b9c211f

File tree

113 files changed

+4734
-48
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+4734
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.pytest_cache/**
22
**/__pycache__
33
venv/**
4+
bot/**
45
dist/**
56
*.egg-info/**
67

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Added `automod` extension
12+
913
### Fixed
1014

1115
- Fixed a race condition with `JsonFileDatabaseAdapter` (#39)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from discord.ext.commands import Bot
2+
3+
from commanderbot_ext.ext.automod.automod_cog import AutomodCog
4+
from commanderbot_ext.lib.utils import add_configured_cog
5+
6+
7+
def setup(bot: Bot):
8+
add_configured_cog(bot, __name__, AutomodCog)

commanderbot_ext/ext/automod/actions/__init__.py

Whitespace-only changes.

commanderbot_ext/ext/automod/actions/abc/__init__.py

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from dataclasses import dataclass
2+
from typing import Optional, Tuple
3+
4+
from discord import Guild, Member
5+
6+
from commanderbot_ext.ext.automod.automod_action import AutomodActionBase
7+
from commanderbot_ext.ext.automod.automod_event import AutomodEvent
8+
from commanderbot_ext.lib import RoleID
9+
10+
11+
@dataclass
12+
class AddRolesToTargetBase(AutomodActionBase):
13+
roles: Tuple[RoleID]
14+
15+
def get_target(self, event: AutomodEvent) -> Optional[Member]:
16+
raise NotImplementedError()
17+
18+
async def apply(self, event: AutomodEvent):
19+
if member := self.get_target(event):
20+
guild: Guild = member.guild
21+
roles = [guild.get_role(role_id) for role_id in self.roles]
22+
await member.add_roles(roles)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from dataclasses import dataclass
2+
from typing import Optional, Tuple
3+
4+
from discord import Guild, Member
5+
6+
from commanderbot_ext.ext.automod.automod_action import AutomodActionBase
7+
from commanderbot_ext.ext.automod.automod_event import AutomodEvent
8+
from commanderbot_ext.lib import RoleID
9+
10+
11+
@dataclass
12+
class RemoveRolesFromTargetBase(AutomodActionBase):
13+
roles: Tuple[RoleID]
14+
15+
def get_target(self, event: AutomodEvent) -> Optional[Member]:
16+
raise NotImplementedError()
17+
18+
async def apply(self, event: AutomodEvent):
19+
if member := self.get_target(event):
20+
guild: Guild = member.guild
21+
roles = [guild.get_role(role_id) for role_id in self.roles]
22+
await member.remove_roles(roles)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from dataclasses import dataclass
2+
from typing import Tuple
3+
4+
from commanderbot_ext.ext.automod.automod_action import AutomodAction, AutomodActionBase
5+
from commanderbot_ext.ext.automod.automod_event import AutomodEvent
6+
from commanderbot_ext.lib import JsonObject
7+
8+
9+
@dataclass
10+
class AddReactions(AutomodActionBase):
11+
"""
12+
Add reactions to the message in context.
13+
14+
Attributes
15+
----------
16+
reactions
17+
The reactions to add.
18+
"""
19+
20+
reactions: Tuple[str]
21+
22+
async def apply(self, event: AutomodEvent):
23+
if message := event.message:
24+
for reaction in self.reactions:
25+
await message.add_reaction(reaction)
26+
27+
28+
def create_action(data: JsonObject) -> AutomodAction:
29+
return AddReactions.from_data(data)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dataclasses import dataclass
2+
from typing import Optional
3+
4+
from discord import Member
5+
6+
from commanderbot_ext.ext.automod.actions.abc.add_roles_to_target_base import (
7+
AddRolesToTargetBase,
8+
)
9+
from commanderbot_ext.ext.automod.automod_action import AutomodAction
10+
from commanderbot_ext.ext.automod.automod_event import AutomodEvent
11+
from commanderbot_ext.lib import JsonObject
12+
13+
14+
@dataclass
15+
class AddRolesToActor(AddRolesToTargetBase):
16+
"""
17+
Add roles to the actor in context.
18+
19+
Attributes
20+
----------
21+
roles
22+
The roles to add.
23+
"""
24+
25+
def get_target(self, event: AutomodEvent) -> Optional[Member]:
26+
return event.actor
27+
28+
29+
def create_action(data: JsonObject) -> AutomodAction:
30+
return AddRolesToActor.from_data(data)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dataclasses import dataclass
2+
from typing import Optional
3+
4+
from discord import Member
5+
6+
from commanderbot_ext.ext.automod.actions.abc.add_roles_to_target_base import (
7+
AddRolesToTargetBase,
8+
)
9+
from commanderbot_ext.ext.automod.automod_action import AutomodAction
10+
from commanderbot_ext.ext.automod.automod_event import AutomodEvent
11+
from commanderbot_ext.lib import JsonObject
12+
13+
14+
@dataclass
15+
class AddRolesToAuthor(AddRolesToTargetBase):
16+
"""
17+
Add roles to the author in context.
18+
19+
Attributes
20+
----------
21+
roles
22+
The roles to add.
23+
"""
24+
25+
def get_target(self, event: AutomodEvent) -> Optional[Member]:
26+
return event.author
27+
28+
29+
def create_action(data: JsonObject) -> AutomodAction:
30+
return AddRolesToAuthor.from_data(data)

0 commit comments

Comments
 (0)