diff options
9 files changed, 18 insertions, 24 deletions
diff --git a/bot/exts/filtering/_filter_lists/filter_list.py b/bot/exts/filtering/_filter_lists/filter_list.py index a32b0741e..4913315eb 100644 --- a/bot/exts/filtering/_filter_lists/filter_list.py +++ b/bot/exts/filtering/_filter_lists/filter_list.py @@ -6,7 +6,6 @@ from collections.abc import Iterable from dataclasses import dataclass from enum import Enum from functools import reduce -from operator import or_ from typing import Any import arrow @@ -133,7 +132,7 @@ class AtomicList: return None try: return reduce( - or_, (filter_.actions or self.defaults.actions for filter_ in filters) + ActionSettings.union, (filter_.actions or self.defaults.actions for filter_ in filters) ).fallback_to(self.defaults.actions) except TypeError: # The sequence fed to reduce is empty, meaning none of the filters have actions, diff --git a/bot/exts/filtering/_filter_lists/invite.py b/bot/exts/filtering/_filter_lists/invite.py index dd14d2222..c2a75e831 100644 --- a/bot/exts/filtering/_filter_lists/invite.py +++ b/bot/exts/filtering/_filter_lists/invite.py @@ -120,7 +120,7 @@ class InviteList(FilterList[InviteFilter]): # Blocked invites come second so that their actions have preference. if triggered: if actions: - actions |= self[ListType.DENY].merge_actions(triggered) + actions = actions.union(self[ListType.DENY].merge_actions(triggered)) else: actions = self[ListType.DENY].merge_actions(triggered) all_triggers[ListType.DENY] = triggered diff --git a/bot/exts/filtering/_settings.py b/bot/exts/filtering/_settings.py index fff4239ec..75e810df5 100644 --- a/bot/exts/filtering/_settings.py +++ b/bot/exts/filtering/_settings.py @@ -7,6 +7,8 @@ from copy import copy from functools import reduce from typing import Any, NamedTuple, Optional, TypeVar +from typing_extensions import Self + from bot.exts.filtering._filter_context import FilterContext from bot.exts.filtering._settings_types import settings_types from bot.exts.filtering._settings_types.settings_entry import ActionEntry, SettingsEntry, ValidationEntry @@ -175,13 +177,13 @@ class ActionSettings(Settings[ActionEntry]): def __init__(self, settings_data: dict, *, defaults: Settings | None = None, keep_empty: bool = False): super().__init__(settings_data, defaults=defaults, keep_empty=keep_empty) - def __or__(self, other: ActionSettings) -> ActionSettings: + def union(self, other: Self) -> Self: """Combine the entries of two collections of settings into a new ActionsSettings.""" actions = {} # A settings object doesn't necessarily have all types of entries (e.g in the case of filter overrides). for entry in self: if entry in other: - actions[entry] = self[entry] | other[entry] + actions[entry] = self[entry].union(other[entry]) else: actions[entry] = self[entry] for entry in other: diff --git a/bot/exts/filtering/_settings_types/actions/infraction_and_notification.py b/bot/exts/filtering/_settings_types/actions/infraction_and_notification.py index 215afabdd..a3b59d05d 100644 --- a/bot/exts/filtering/_settings_types/actions/infraction_and_notification.py +++ b/bot/exts/filtering/_settings_types/actions/infraction_and_notification.py @@ -9,6 +9,7 @@ from botcore.utils.members import get_or_fetch_member from discord import Colour, Embed, Member, User from discord.errors import Forbidden from pydantic import validator +from typing_extensions import Self import bot as bot_module from bot.constants import Channels @@ -159,7 +160,7 @@ class InfractionAndNotification(ActionEntry): ) ctx.action_descriptions.append(passive_form[self.infraction_type.name]) - def __or__(self, other: ActionEntry): + def union(self, other: Self) -> Self: """ Combines two actions of the same type. Each type of action is executed once per filter. @@ -169,9 +170,6 @@ class InfractionAndNotification(ActionEntry): To avoid bombarding the user with several notifications, the message with the more significant infraction is used. """ - if not isinstance(other, InfractionAndNotification): - return NotImplemented - # Lower number -> higher in the hierarchy if self.infraction_type is None: return other.copy() diff --git a/bot/exts/filtering/_settings_types/actions/ping.py b/bot/exts/filtering/_settings_types/actions/ping.py index b3725917c..ee40c54fe 100644 --- a/bot/exts/filtering/_settings_types/actions/ping.py +++ b/bot/exts/filtering/_settings_types/actions/ping.py @@ -1,6 +1,7 @@ from typing import ClassVar from pydantic import validator +from typing_extensions import Self from bot.exts.filtering._filter_context import FilterContext from bot.exts.filtering._settings_types.settings_entry import ActionEntry @@ -39,9 +40,6 @@ class Ping(ActionEntry): new_content = " ".join([resolve_mention(mention) for mention in mentions]) ctx.alert_content = f"{new_content} {ctx.alert_content}" - def __or__(self, other: ActionEntry): + def union(self, other: Self) -> Self: """Combines two actions of the same type. Each type of action is executed once per filter.""" - if not isinstance(other, Ping): - return NotImplemented - return Ping(guild_pings=self.guild_pings | other.guild_pings, dm_pings=self.dm_pings | other.dm_pings) diff --git a/bot/exts/filtering/_settings_types/actions/remove_context.py b/bot/exts/filtering/_settings_types/actions/remove_context.py index 410740028..e030f06d2 100644 --- a/bot/exts/filtering/_settings_types/actions/remove_context.py +++ b/bot/exts/filtering/_settings_types/actions/remove_context.py @@ -5,6 +5,7 @@ from botcore.utils import scheduling from botcore.utils.logging import get_logger from discord import Message from discord.errors import HTTPException +from typing_extensions import Self import bot from bot.constants import Channels @@ -105,9 +106,6 @@ class RemoveContext(ActionEntry): await command(FakeContext(alerts_channel, command), ctx.author, None, reason=SUPERSTAR_REASON) ctx.action_descriptions.append("superstar") - def __or__(self, other: ActionEntry): + def union(self, other: Self) -> Self: """Combines two actions of the same type. Each type of action is executed once per filter.""" - if not isinstance(other, RemoveContext): - return NotImplemented - return RemoveContext(remove_context=self.remove_context or other.remove_context) diff --git a/bot/exts/filtering/_settings_types/actions/send_alert.py b/bot/exts/filtering/_settings_types/actions/send_alert.py index 04e400764..f554cdd4d 100644 --- a/bot/exts/filtering/_settings_types/actions/send_alert.py +++ b/bot/exts/filtering/_settings_types/actions/send_alert.py @@ -1,5 +1,7 @@ from typing import ClassVar +from typing_extensions import Self + from bot.exts.filtering._filter_context import FilterContext from bot.exts.filtering._settings_types.settings_entry import ActionEntry @@ -16,9 +18,6 @@ class SendAlert(ActionEntry): """Add the stored pings to the alert message content.""" ctx.send_alert = self.send_alert - def __or__(self, other: ActionEntry): + def union(self, other: Self) -> Self: """Combines two actions of the same type. Each type of action is executed once per filter.""" - if not isinstance(other, SendAlert): - return NotImplemented - return SendAlert(send_alert=self.send_alert or other.send_alert) diff --git a/bot/exts/filtering/_settings_types/settings_entry.py b/bot/exts/filtering/_settings_types/settings_entry.py index 31e11108d..e41ef5c7a 100644 --- a/bot/exts/filtering/_settings_types/settings_entry.py +++ b/bot/exts/filtering/_settings_types/settings_entry.py @@ -4,6 +4,7 @@ from abc import abstractmethod from typing import Any, ClassVar, Union from pydantic import BaseModel, PrivateAttr +from typing_extensions import Self from bot.exts.filtering._filter_context import FilterContext from bot.exts.filtering._utils import FieldRequiring @@ -80,7 +81,7 @@ class ActionEntry(SettingsEntry): ... @abstractmethod - def __or__(self, other: ActionEntry): + def union(self, other: Self) -> Self: """ Combine two actions of the same type. Each type of action is executed once per filter. diff --git a/bot/exts/filtering/filtering.py b/bot/exts/filtering/filtering.py index a66adfc73..66f4715f3 100644 --- a/bot/exts/filtering/filtering.py +++ b/bot/exts/filtering/filtering.py @@ -1,6 +1,5 @@ import datetime import json -import operator import re import unicodedata from collections import defaultdict @@ -917,7 +916,7 @@ class Filtering(Cog): result_actions = None if actions: - result_actions = reduce(operator.or_, (action for action in actions)) + result_actions = reduce(ActionSettings.union, actions) return result_actions, messages, triggers |