adamm-hf HF Staff commited on
Commit
bbbbb07
·
verified ·
1 Parent(s): 79fa544

command to sweep for mrbeast

Browse files
Files changed (1) hide show
  1. app.py +141 -31
app.py CHANGED
@@ -81,8 +81,13 @@ IMAGE_SCAN_TIMEOUT = 90 # seconds per attempt; thinking models are s
81
  IMAGE_SCAN_MAX_TOKENS = 8000 # reasoning tokens count against this, so keep it generous
82
  IMAGE_EXTS = (".png", ".jpg", ".jpeg", ".webp", ".gif")
83
 
84
- image_scan_recent_bans = {} # user_id -> ban time, so a multi-message burst only bans/logs once
85
- _bg_tasks = set() # keeps background scan tasks referenced until they finish
 
 
 
 
 
86
 
87
  SCAN_PROMPT = """You are a moderation classifier for the Hugging Face Discord server.
88
  The image(s) were all posted together in one message. Judge them as a set.
@@ -168,26 +173,34 @@ async def _classify_images(session, data_urls, message_text="", models=None):
168
  return None
169
 
170
 
171
- async def scan_message_images(message):
 
 
 
 
 
172
  try:
173
- if not HF_TOKEN or not message.guild or message.author.bot:
174
- return
175
- member = message.author if isinstance(message.author, discord.Member) else message.guild.get_member(message.author.id)
176
- if member is None or is_exempt(member):
177
- return
 
 
 
178
  urls = _image_urls_from_message(message)
179
  if not urls:
180
- return
181
 
182
  async with aiohttp.ClientSession() as session:
183
  images = [img for img in [await _fetch_image(session, u) for u in urls] if img]
184
  if not images:
185
- return
186
  verdict = await _classify_images(session, [d for d, _ in images], message.content)
187
 
188
  if verdict is None:
189
  print(f"Image scan: every provider failed for message {message.id}")
190
- return
191
 
192
  is_mrbeast = verdict.get("mrbeast") is True
193
  is_scam = verdict.get("scam") is True
@@ -200,42 +213,67 @@ async def scan_message_images(message):
200
 
201
  hit = is_scam and confidence >= IMAGE_SCAN_MIN_CONFIDENCE and (is_mrbeast or not IMAGE_SCAN_REQUIRE_MRBEAST)
202
  if not hit:
203
- return
204
 
 
 
205
  # Discord deletes attachments along with the message, so keep our own copy as evidence
206
  files = [discord.File(io.BytesIO(jpeg), filename=f"evidence_{i + 1}.jpg") for i, (_, jpeg) in enumerate(images)]
207
- jump = f"https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id}"
208
- details = (f"User: {member.mention} ({member.id}) in {message.channel.mention}\n"
 
209
  f"Verdict: mrbeast={is_mrbeast} scam={is_scam} confidence={confidence:.2f} via `{verdict['model']}`\n"
210
  f"Reason: {reason}\n[Jump to message!]({jump})")
211
- alert = {"True": "<@&1106995261487710411>", "False": "<@&1108342563628404747>"}.get(os.environ.get("TEST_SERVER"), "")
 
212
 
213
  if IMAGE_SCAN_DRY_RUN:
214
- await bot.log_channel.send(f"🧪 **[IMAGE SCAN, DRY RUN] would auto-ban**\n{details}", files=files)
215
- return
 
216
 
217
- # check-and-set with no await in between, so parallel scans of one burst can't double-ban
218
- if time.time() - image_scan_recent_bans.get(member.id, 0) < 600:
219
- return
220
- image_scan_recent_bans[member.id] = time.time()
 
 
 
 
221
 
222
- me = message.guild.me
223
- if not (me.guild_permissions.ban_members and member.top_role < me.top_role):
224
- await bot.log_channel.send(f"❌ Image-scan ban skipped (missing perms / role hierarchy)\n{details}", files=files)
225
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  try:
228
- await member.send(f"You have been automatically banned from **{message.guild.name}** because your account "
229
- f"posted scam images (this usually means the account was compromised). Secure your "
230
- f"account, then contact @Adam to appeal.")
231
  except Exception:
232
  pass
233
- await member.ban(delete_message_seconds=86400, # wipes their last 24h of messages in every channel
234
- reason=f"Auto-ban (image scan): {reason}"[:512])
235
- await bot.log_channel.send(f"🚨 **AUTO-BANNED (image scan)**\n{details}\n{alert}", files=files)
 
 
236
 
237
  except Exception as e:
238
  print(f"scan_message_images Error: {e}")
 
239
 
240
 
241
  # stats stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -572,6 +610,7 @@ async def on_member_ban(guild, banned_user):
572
  @bot.event
573
  async def on_member_unban(guild, unbanned_user):
574
  try:
 
575
  await asyncio.sleep(5)
576
  async for entry in guild.audit_logs(action=discord.AuditLogAction.unban, limit=1):
577
  if unbanned_user == entry.target: # verify that unbanned user is in audit log
@@ -843,6 +882,77 @@ async def scantest(ctx):
843
  await ctx.send("\n".join(lines)[:1900])
844
 
845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
846
 
847
 
848
 
 
81
  IMAGE_SCAN_MAX_TOKENS = 8000 # reasoning tokens count against this, so keep it generous
82
  IMAGE_EXTS = (".png", ".jpg", ".jpeg", ".webp", ".gif")
83
 
84
+ IMAGE_SWEEP_DEFAULT_DAYS = 30 # how far back !scansweep looks by default
85
+ IMAGE_SWEEP_MAX_DAYS = 90
86
+ IMAGE_SWEEP_CONCURRENCY = 6 # parallel model calls during a sweep
87
+
88
+ image_scan_banned_ids = set() # users the scanner banned (or found banned), so a burst or a sweep acts once per user
89
+ _bg_tasks = set() # keeps background scan tasks referenced until they finish
90
+ _sweep_running = False
91
 
92
  SCAN_PROMPT = """You are a moderation classifier for the Hugging Face Discord server.
93
  The image(s) were all posted together in one message. Judge them as a set.
 
173
  return None
174
 
175
 
176
+ async def scan_message_images(message, sweep=False):
177
+ """Scan one message's images and act on a hit. Returns "hit", "clean", "failed" or "skipped".
178
+
179
+ sweep=True is used by !scansweep for older messages: the author may have left the server since,
180
+ and the flagged message is deleted directly because a ban only clears the last 7 days of messages.
181
+ """
182
  try:
183
+ if not HF_TOKEN or not message.guild or message.author.bot or message.webhook_id:
184
+ return "skipped"
185
+ guild = message.guild
186
+ member = message.author if isinstance(message.author, discord.Member) else guild.get_member(message.author.id)
187
+ if member is None and not sweep:
188
+ return "skipped"
189
+ if member is not None and is_exempt(member):
190
+ return "skipped"
191
  urls = _image_urls_from_message(message)
192
  if not urls:
193
+ return "skipped"
194
 
195
  async with aiohttp.ClientSession() as session:
196
  images = [img for img in [await _fetch_image(session, u) for u in urls] if img]
197
  if not images:
198
+ return "failed"
199
  verdict = await _classify_images(session, [d for d, _ in images], message.content)
200
 
201
  if verdict is None:
202
  print(f"Image scan: every provider failed for message {message.id}")
203
+ return "failed"
204
 
205
  is_mrbeast = verdict.get("mrbeast") is True
206
  is_scam = verdict.get("scam") is True
 
213
 
214
  hit = is_scam and confidence >= IMAGE_SCAN_MIN_CONFIDENCE and (is_mrbeast or not IMAGE_SCAN_REQUIRE_MRBEAST)
215
  if not hit:
216
+ return "clean"
217
 
218
+ user = member or message.author
219
+ label = "IMAGE SWEEP" if sweep else "IMAGE SCAN"
220
  # Discord deletes attachments along with the message, so keep our own copy as evidence
221
  files = [discord.File(io.BytesIO(jpeg), filename=f"evidence_{i + 1}.jpg") for i, (_, jpeg) in enumerate(images)]
222
+ jump = f"https://discord.com/channels/{guild.id}/{message.channel.id}/{message.id}"
223
+ details = (f"User: {user.mention} ({user.id}) in {message.channel.mention}, "
224
+ f"posted {convert_to_timezone(message.created_at, zurich_tz)}\n"
225
  f"Verdict: mrbeast={is_mrbeast} scam={is_scam} confidence={confidence:.2f} via `{verdict['model']}`\n"
226
  f"Reason: {reason}\n[Jump to message!]({jump})")
227
+ # a sweep can hit many old posts; on_member_ban already pings @alerts once per ban, so don't add a second ping
228
+ alert = "" if sweep else {"True": "<@&1106995261487710411>", "False": "<@&1108342563628404747>"}.get(os.environ.get("TEST_SERVER"), "")
229
 
230
  if IMAGE_SCAN_DRY_RUN:
231
+ action = "delete this post + auto-ban" if sweep else "auto-ban"
232
+ await bot.log_channel.send(f"🧪 **[{label}, DRY RUN] would {action}**\n{details}", files=files)
233
+ return "hit"
234
 
235
+ if sweep:
236
+ # a ban only clears the last 7 days of messages, so remove older scam posts directly
237
+ try:
238
+ await message.delete()
239
+ except discord.NotFound:
240
+ pass
241
+ except Exception as e:
242
+ print(f"Image sweep: could not delete message {message.id}: {e}")
243
 
244
+ # claim the user before any await, so parallel scans of one burst (or a sweep) only ban once
245
+ if user.id in image_scan_banned_ids:
246
+ return "hit"
247
+ image_scan_banned_ids.add(user.id)
248
+
249
+ try:
250
+ await guild.fetch_ban(user)
251
+ await bot.log_channel.send(f"🧹 **[{label}] removed scam post from an already-banned user**\n{details}", files=files)
252
+ return "hit"
253
+ except discord.NotFound:
254
+ pass # not banned yet
255
+
256
+ me = guild.me
257
+ if not (me.guild_permissions.ban_members and (member is None or member.top_role < me.top_role)):
258
+ image_scan_banned_ids.discard(user.id)
259
+ await bot.log_channel.send(f"❌ {label} ban skipped (missing perms / role hierarchy)\n{details}", files=files)
260
+ return "hit"
261
 
262
  try:
263
+ await user.send(f"You have been automatically banned from **{guild.name}** because your account "
264
+ f"posted scam images (this usually means the account was compromised). Secure your "
265
+ f"account, then contact @Adam to appeal.")
266
  except Exception:
267
  pass
268
+ await guild.ban(user,
269
+ delete_message_seconds=604800 if sweep else 86400, # sweep: max 7 days; live: last 24h, every channel
270
+ reason=f"Auto-ban ({label.lower()}): {reason}"[:512])
271
+ await bot.log_channel.send(f"🚨 **AUTO-BANNED ({label.lower()})**\n{details}\n{alert}", files=files)
272
+ return "hit"
273
 
274
  except Exception as e:
275
  print(f"scan_message_images Error: {e}")
276
+ return "failed"
277
 
278
 
279
  # stats stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------
 
610
  @bot.event
611
  async def on_member_unban(guild, unbanned_user):
612
  try:
613
+ image_scan_banned_ids.discard(unbanned_user.id) # let the image scanner act again if the account is ever re-hijacked
614
  await asyncio.sleep(5)
615
  async for entry in guild.audit_logs(action=discord.AuditLogAction.unban, limit=1):
616
  if unbanned_user == entry.target: # verify that unbanned user is in audit log
 
882
  await ctx.send("\n".join(lines)[:1900])
883
 
884
 
885
+ # !scansweep [days] scans image posts from the last N days (default 30) in every channel and thread,
886
+ # with the same rules as the live scanner. Respects IMAGE_SCAN_DRY_RUN. Staff only, one sweep at a time.
887
+ @bot.command(name="scansweep")
888
+ async def scansweep(ctx, days: int = IMAGE_SWEEP_DEFAULT_DAYS):
889
+ global _sweep_running
890
+ if not ctx.guild or not is_exempt(ctx.author):
891
+ return
892
+ if not HF_TOKEN:
893
+ await ctx.send("`HF_TOKEN` isn't set, so the image scanner is off.")
894
+ return
895
+ if _sweep_running:
896
+ await ctx.send("A sweep is already running.")
897
+ return
898
+ _sweep_running = True
899
+ try:
900
+ days = max(1, min(days, IMAGE_SWEEP_MAX_DAYS))
901
+ guild = ctx.guild
902
+ cutoff = discord.utils.utcnow() - timedelta(days=days)
903
+ mode = "DRY RUN, log only" if IMAGE_SCAN_DRY_RUN else "LIVE, deletes and bans"
904
+ header = f"🔎 Image sweep, last {days} days ({mode})"
905
+ status = await ctx.send(f"{header}: collecting channels…")
906
+
907
+ # everywhere messages can live: text channels, voice-channel chats, active threads, threads archived since the cutoff
908
+ sources = list(guild.text_channels) + list(guild.voice_channels) + list(guild.threads)
909
+ for parent in list(guild.text_channels) + list(getattr(guild, "forums", [])):
910
+ try:
911
+ async for th in parent.archived_threads(limit=None):
912
+ if th.archive_timestamp and th.archive_timestamp < cutoff:
913
+ break # newest-archived first, so everything after this is older
914
+ sources.append(th)
915
+ except Exception as e:
916
+ print(f"Image sweep: can't list archived threads in {parent}: {e}")
917
+ sources = list({s.id: s for s in sources}.values()) # dedupe, keep order
918
+
919
+ counts = {"messages": 0, "hit": 0, "clean": 0, "failed": 0, "skipped": 0}
920
+ sem = asyncio.Semaphore(IMAGE_SWEEP_CONCURRENCY)
921
+
922
+ async def scan_one(m):
923
+ async with sem:
924
+ counts[await scan_message_images(m, sweep=True)] += 1
925
+
926
+ for i, src in enumerate(sources, 1):
927
+ tasks = []
928
+ try:
929
+ async for m in src.history(limit=None, after=cutoff):
930
+ counts["messages"] += 1
931
+ if m.attachments or m.embeds or getattr(m, "message_snapshots", None):
932
+ tasks.append(asyncio.create_task(scan_one(m)))
933
+ except Exception as e:
934
+ print(f"Image sweep: can't read {src}: {e}")
935
+ if tasks:
936
+ await asyncio.gather(*tasks)
937
+ if tasks or i % 10 == 0 or i == len(sources):
938
+ scanned = counts["hit"] + counts["clean"] + counts["failed"]
939
+ try:
940
+ await status.edit(content=f"{header}: {i}/{len(sources)} channels · {counts['messages']:,} messages · "
941
+ f"{scanned:,} image posts scanned · {counts['hit']} scam hits")
942
+ except Exception:
943
+ pass
944
+
945
+ scanned = counts["hit"] + counts["clean"] + counts["failed"]
946
+ await ctx.send(f"✅ Sweep done ({mode}): {counts['messages']:,} messages in {len(sources)} channels/threads, "
947
+ f"{scanned:,} image posts scanned, **{counts['hit']} scam hits**, "
948
+ f"{counts['failed']} couldn't be scanned. Each hit is logged in admin-logs with its images.")
949
+ except Exception as e:
950
+ print(f"scansweep Error: {e}")
951
+ await ctx.send(f"❌ Sweep stopped with an error: `{e}`")
952
+ finally:
953
+ _sweep_running = False
954
+
955
+
956
 
957
 
958