Add scamimages cog - detect scam images and auto-ban posters#391
Open
b1ume wants to merge 3 commits into
Open
Conversation
Switch image fetching to a persistent aiohttp ClientSession with a 15s timeout and close it in cog_unload to avoid creating sessions per request. Normalize guild log channel storage to integer IDs (including defaults and reads/writes), and narrow confirmation timeout handling to asyncio.TimeoutError instead of catching all exceptions.
There was a problem hiding this comment.
Pull request overview
Adds a new moderation cog (scamimages) to detect known scam images via perceptual hashing and automatically take enforcement action, along with documentation and dependency metadata so it can be installed as part of this cog collection.
Changes:
- Introduces
scamimagescog with admin-only commands to manage a per-guild image-hash database and to manually scan images. - Adds an
on_messagelistener that hashes image attachments and bans users when a match is detected. - Documents the new cog in the repository README and declares required third-party libraries in
info.json.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
scamimages/scamimages.py |
Implements perceptual-hash scanning, admin commands, and automatic ban/logging behavior. |
scamimages/info.json |
Declares cog metadata and Python package requirements (imagehash, Pillow). |
scamimages/__init__.py |
Registers the cog with Red via setup(). |
README.md |
Adds the ScamImages cog summary and command list to project documentation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if not message.attachments: | ||
| return | ||
|
|
||
| image_attachments = [a for a in message.attachments if a.content_type and a.content_type.startswith("image/")] |
Comment on lines
+125
to
+135
| async def _compute_phash_from_url(self, url: str) -> Optional[imagehash.ImageHash]: | ||
| async with self.session.get(url, timeout=REQUEST_TIMEOUT) as response: | ||
| if response.status != 200: | ||
| return None | ||
| image_data = await response.read() | ||
|
|
||
| try: | ||
| image = Image.open(BytesIO(image_data)) | ||
| return imagehash.phash(image) | ||
| except Exception: | ||
| return None |
| ban_reason = f"Automatic ban: posted a known scam image. Hash: {str(phash)}" | ||
|
|
||
| try: | ||
| await message.guild.ban(message.author, reason=ban_reason, delete_message_days=1) |
|
|
||
| async def _compute_phash_from_message(self, message: discord.Message) -> list[Optional[imagehash.ImageHash]]: | ||
| results = [] | ||
| image_attachments = [a for a in message.attachments if a.content_type and a.content_type.startswith("image/")] |
Comment on lines
+363
to
+377
| try: | ||
| reaction, _ = await self.bot.wait_for("reaction_add", timeout=30.0, check=check) | ||
| except asyncio.TimeoutError: | ||
| await confirm_msg.clear_reactions() | ||
| await ctx.send("Timed out. Database was not cleared.") | ||
| return | ||
|
|
||
| if str(reaction.emoji) == "✅": | ||
| await self.config.guild(ctx.guild).hashes.set([]) | ||
| await confirm_msg.clear_reactions() | ||
| await ctx.send("Scam image database has been cleared.") | ||
| log.info("Scam image database cleared for guild %s (%s)", ctx.guild.name, ctx.guild.id) | ||
| else: | ||
| await confirm_msg.clear_reactions() | ||
| await ctx.send("Cancelled. Database was not cleared.") |
Comment on lines
+72
to
+74
| if phash - stored_hash <= SIMILARITY_THRESHOLD: | ||
| await self._handle_match(message, attachment, phash, stored_hash_str) | ||
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Initial Checklist
Details
Does this resolve an issue?
No, this is a new feature.
Changes
Features
scamimagescog that detects known scam images (e.g. MrBeast giveaway scams) using perceptual hashing and automatically bans users who post them.10.add,remove,list,scan,logchannel,clear.Additional
Requires
imagehashandPillow(listed in coginfo.jsonrequirements field).