|
| 1 | +import typing |
| 2 | + |
| 3 | +import aiohttp |
| 4 | +from pydis_core.utils.logging import get_logger |
| 5 | + |
| 6 | +from bot.exts.filtering._filter_context import Event, FilterContext |
| 7 | +from bot.exts.filtering._filter_lists.filter_list import FilterList, ListType |
| 8 | +from bot.exts.filtering._filters.filter import Filter |
| 9 | +from bot.exts.filtering._filters.image_hash import ImageHashFilter |
| 10 | +from bot.exts.filtering._image_hash import RhodiumAPIError, get_image_hash |
| 11 | +from bot.exts.filtering._settings import ActionSettings |
| 12 | + |
| 13 | +if typing.TYPE_CHECKING: |
| 14 | + from bot.exts.filtering.filtering import Filtering |
| 15 | + |
| 16 | +log = get_logger(__name__) |
| 17 | + |
| 18 | +_MAX_IMAGE_SIZE = 5_000_000 |
| 19 | + |
| 20 | + |
| 21 | +class ImageHashesList(FilterList[ImageHashFilter]): |
| 22 | + """A list of perceptual image hashes that should trigger filtering when matched.""" |
| 23 | + |
| 24 | + name = "image_hash" |
| 25 | + |
| 26 | + def __init__(self, filtering_cog: Filtering): |
| 27 | + super().__init__() |
| 28 | + filtering_cog.subscribe(self, Event.MESSAGE) |
| 29 | + |
| 30 | + def get_filter_type(self, content: str) -> type[Filter]: |
| 31 | + """Get a subclass of filter matching the filter list and the filter's content.""" |
| 32 | + return ImageHashFilter |
| 33 | + |
| 34 | + @property |
| 35 | + def filter_types(self) -> set[type[Filter]]: |
| 36 | + """Return the types of filters used by this list.""" |
| 37 | + return {ImageHashFilter} |
| 38 | + |
| 39 | + async def actions_for( |
| 40 | + self, ctx: FilterContext |
| 41 | + ) -> tuple[ActionSettings | None, list[str], dict[ListType, list[Filter]]]: |
| 42 | + """Dispatch the given event to the list's filters, and return actions to take and messages to relay to mods.""" |
| 43 | + if not ctx.attachments: |
| 44 | + return None, [], {} |
| 45 | + |
| 46 | + image_hashes = [] |
| 47 | + for attachment in ctx.attachments: |
| 48 | + if ( |
| 49 | + attachment.content_type is None |
| 50 | + or not attachment.content_type.startswith("image") |
| 51 | + or attachment.size > _MAX_IMAGE_SIZE |
| 52 | + ): |
| 53 | + continue |
| 54 | + |
| 55 | + try: |
| 56 | + image_hash = await get_image_hash(attachment.url) |
| 57 | + except aiohttp.ClientError: |
| 58 | + log.exception("Unhandled aiohttp exception while getting image hash") |
| 59 | + continue |
| 60 | + except RhodiumAPIError as e: |
| 61 | + log.exception("Rhodium API error: %s", e) |
| 62 | + continue |
| 63 | + except TimeoutError: |
| 64 | + log.exception("Timed out getting image hash") |
| 65 | + continue |
| 66 | + |
| 67 | + image_hashes.append(image_hash) |
| 68 | + |
| 69 | + if not image_hashes: |
| 70 | + return None, [], {} |
| 71 | + |
| 72 | + trigger_ctx = ctx.replace(content=image_hashes) |
| 73 | + triggers = await self[ListType.DENY].filter_list_result(trigger_ctx) |
| 74 | + if not triggers: |
| 75 | + return None, [], {ListType.DENY: triggers} |
| 76 | + |
| 77 | + actions = self[ListType.DENY].merge_actions(triggers) |
| 78 | + messages = [] |
| 79 | + for filter_ in triggers: |
| 80 | + distance = ctx.filter_info.get(filter_, "?") |
| 81 | + messages.append( |
| 82 | + f"{filter_.id} (`{filter_.content}` distance `{distance}`)" |
| 83 | + f" - {filter_.description or '*No description*'}" |
| 84 | + ) |
| 85 | + return actions, messages, {ListType.DENY: triggers} |
0 commit comments