Skip to content

Commit 7420f02

Browse files
authored
fix(plugins): stop duplicate_spam from starving bio_bait_spam at group=4 (#21)
duplicate_spam and bio_bait_spam register at the same PTB group (4) with the identical filter shape (GROUPS & ~COMMAND). PTB checks handlers in registration order within a group and stops at the first match by default (block=True), and duplicate_spam registers first per MANIFEST_ORDER — so bio_bait_spam's callback has never actually run for a real update. Set block=False on both registrations so PTB keeps checking the next handler in group=4 after the first one runs. ApplicationHandlerStop (raised on an actual detection in either handler) still short-circuits the rest of the pipeline as before, so this only affects the common case where neither handler finds anything to act on.
1 parent 382c6da commit 7420f02

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/bot/plugins/builtin/spam.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ def register_inline_keyboard_spam(application: Application) -> list[BaseHandler]
4444
def register_bio_bait_spam(application: Application) -> list[BaseHandler]: # type: ignore[type-arg]
4545
"""Register bio bait spam handler (group=4).
4646
47-
Callback wrapped with ``guard_plugin("bio_bait_spam")``.
47+
Callback wrapped with ``guard_plugin("bio_bait_spam")``. Shares group=4
48+
and filter shape with ``duplicate_spam``; ``block=False`` so both get a
49+
chance to run instead of the first match swallowing the update.
4850
"""
4951
handler: BaseHandler = MessageHandler(
5052
BIO_BAIT_FILTER,
5153
guard_plugin("bio_bait_spam")(handle_bio_bait_spam),
54+
block=False,
5255
)
5356
application.add_handler(handler, group=4)
5457
logger.info("Registered handler: bio_bait_spam_handler (group=4)")
@@ -83,11 +86,15 @@ def register_new_user_spam(application: Application) -> list[BaseHandler]: # ty
8386
def register_duplicate_spam(application: Application) -> list[BaseHandler]: # type: ignore[type-arg]
8487
"""Register duplicate message spam handler (group=4).
8588
86-
Callback wrapped with ``guard_plugin("duplicate_spam")``.
89+
Callback wrapped with ``guard_plugin("duplicate_spam")``. Registered
90+
before ``bio_bait_spam`` in the same group with the same filter shape;
91+
``block=False`` so PTB still checks the next handler in group=4 instead
92+
of stopping after this one matches.
8793
"""
8894
handler: BaseHandler = MessageHandler(
8995
filters.ChatType.GROUPS & ~filters.COMMAND,
9096
guard_plugin("duplicate_spam")(handle_duplicate_spam),
97+
block=False,
9198
)
9299
application.add_handler(handler, group=4)
93100
logger.info("Registered handler: duplicate_spam_handler (group=4)")

tests/test_main_plugins_bootstrap.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,27 @@ def test_spam_has_duplicate_spam_registrar(self):
321321
assert hasattr(spam, "register_duplicate_spam")
322322
assert callable(spam.register_duplicate_spam)
323323

324+
def test_duplicate_spam_and_bio_bait_spam_share_group_and_filter_shape(self):
325+
"""duplicate_spam and bio_bait_spam sit in group=4 with the same filter shape.
326+
327+
Regression guard for the collision this test class is named after: both
328+
handlers match on ChatType.GROUPS & ~COMMAND. If either loses its
329+
``block=False``, PTB's default first-match-wins behavior within a group
330+
makes whichever registers first (duplicate_spam, per MANIFEST_ORDER)
331+
permanently swallow every update in group=4, and the other handler's
332+
callback is never invoked.
333+
"""
334+
from bot.plugins.builtin import spam
335+
336+
app = MagicMock()
337+
app.add_handler = MagicMock()
338+
339+
dup_handlers = spam.register_duplicate_spam(app)
340+
bait_handlers = spam.register_bio_bait_spam(app)
341+
342+
assert dup_handlers[0].block is False
343+
assert bait_handlers[0].block is False
344+
324345
def test_captcha_has_registrar(self):
325346
"""bot.plugins.builtin.captcha has register_captcha function."""
326347
from bot.plugins.builtin import captcha as captcha_mod

0 commit comments

Comments
 (0)