feat: allow /block and /unblock by numeric Telegram ID (#701)#774
feat: allow /block and /unblock by numeric Telegram ID (#701)#774ToRyVand wants to merge 2 commits intolnp2pBot:mainfrom
Conversation
WalkthroughThe PR extends block/unblock commands to accept either Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@bot/modules/block/index.ts`:
- Around line 13-16: The block command handler silently calls next() when the
target fails validation; update the bot.command('block', userMiddleware, async
(ctx, next) => { ... }) flow so that when args.length !== 2 or
isValidBlockTarget(args[1]) is false you send the user the localized error
message (use the existing invalid_block_target locale key) instead of silently
calling next(); keep the existing successful path that calls commands.block(ctx,
args[1]) and only call next() for unrelated messages.
In `@locales/fa.yaml`:
- Line 660: The localization key invalid_block_target is still in English;
replace its value with a Persian translation so the fa locale is
consistent—update the locales/fa.yaml entry for invalid_block_target to a
Persian sentence conveying "Please provide a valid `@username` or numeric Telegram
user ID" (e.g., a concise, natural Persian equivalent) while preserving YAML
quoting and formatting.
In `@locales/ko.yaml`:
- Line 658: The locale key invalid_block_target in locales/ko.yaml is still in
English; replace its value with a Korean translation such as "유효한 `@사용자명` 또는 숫자형
텔레그램 사용자 ID를 입력하세요" so the validation message is consistent for Korean users and
keep the key name invalid_block_target unchanged.
In `@locales/ru.yaml`:
- Line 661: Translate the new localization key invalid_block_target in
locales/ru.yaml into Russian and replace the English string "Please provide a
valid `@username` or numeric Telegram user ID" with an appropriate Russian
translation (keeping placeholders/formatting if any); locate the key
invalid_block_target in the file and update its value to the Russian text so the
Russian locale remains consistent.
In `@locales/uk.yaml`:
- Line 657: The localization key invalid_block_target contains an English
message; replace its value with the Ukrainian translation so the key reads with
the provided Ukrainian text (e.g., the suggested "Будь ласка, вкажіть дійсний
`@username` або числовий Telegram ID") in locales/uk.yaml to ensure the string is
localized for Ukrainian users.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 290c7765-f79f-495a-ad8b-8ab62f82a46f
📒 Files selected for processing (14)
bot/modules/block/commands.tsbot/modules/block/index.tsbot/modules/block/messages.tslocales/de.yamllocales/en.yamllocales/es.yamllocales/fa.yamllocales/fr.yamllocales/it.yamllocales/ko.yamllocales/pt.yamllocales/ru.yamllocales/uk.yamltests/bot/modules/block/commands.spec.ts
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@locales/ko.yaml`:
- Line 691: The locale key invalid_block_target is defined but never used;
update the /block and /unblock command handlers so when isValidBlockTarget(...)
returns false they send the localized error message instead of silently doing
return next(): call the codepath that sends a user-facing reply (using the
existing localization function or ctx.reply/t(...) pattern used elsewhere) with
the invalid_block_target string, then return next(); alternatively, if you
prefer removing dead strings, delete invalid_block_target from all locale files
instead—refer to isValidBlockTarget, the /block and /unblock handlers, and the
existing next() return sites to implement this change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 7ab7d287-9d11-4ba9-bda1-1eff88920045
📒 Files selected for processing (11)
bot/modules/block/commands.tslocales/de.yamllocales/en.yamllocales/es.yamllocales/fa.yamllocales/fr.yamllocales/it.yamllocales/ko.yamllocales/pt.yamllocales/ru.yamllocales/uk.yaml
✅ Files skipped from review due to trivial changes (9)
- locales/en.yaml
- locales/de.yaml
- locales/es.yaml
- locales/fr.yaml
- locales/pt.yaml
- locales/fa.yaml
- locales/uk.yaml
- locales/ru.yaml
- locales/it.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
- bot/modules/block/commands.ts
locales/ko.yaml
Outdated
| user_blocked: User successfully blocked | ||
| user_unblocked: User successfully unblocked | ||
| blocklist_empty: You do not have any blocked user | ||
| invalid_block_target: "Please provide a valid @username or numeric Telegram user ID" |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Search for any usage of 'invalid_block_target' in the codebase
echo "=== Searching for 'invalid_block_target' usage in code files ==="
rg -n --type=ts --type=js "invalid_block_target" -C3
echo ""
echo "=== Checking if i18n.t() or ctx.i18n.t() calls reference this key ==="
rg -nP "i18n\.t\(['\"]invalid_block_target"
echo ""
echo "=== Checking block module for any message sending on validation failure ==="
ast-grep --pattern $'if ($_ || !isValidBlockTarget($_)) {
$$$
}'Repository: lnp2pBot/bot
Length of output: 268
🏁 Script executed:
cat -n locales/ko.yaml | sed -n '685,695p'Repository: lnp2pBot/bot
Length of output: 913
🏁 Script executed:
rg -A20 "isValidBlockTarget" bot/modules/block/Repository: lnp2pBot/bot
Length of output: 1703
🏁 Script executed:
git ls-files | grep -E "(block|locale)" | head -20Repository: lnp2pBot/bot
Length of output: 391
🏁 Script executed:
rg "invalid_block_target" locales/Repository: lnp2pBot/bot
Length of output: 1132
Remove unused locale key invalid_block_target.
This locale key is defined in all 10 locale files but never referenced in the code. The /block and /unblock command handlers silently skip with return next() when validation fails, instead of sending this error message to the user.
Either implement the error message feedback when isValidBlockTarget() returns false, or remove the dead locale strings.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@locales/ko.yaml` at line 691, The locale key invalid_block_target is defined
but never used; update the /block and /unblock command handlers so when
isValidBlockTarget(...) returns false they send the localized error message
instead of silently doing return next(): call the codepath that sends a
user-facing reply (using the existing localization function or ctx.reply/t(...)
pattern used elsewhere) with the invalid_block_target string, then return
next(); alternatively, if you prefer removing dead strings, delete
invalid_block_target from all locale files instead—refer to isValidBlockTarget,
the /block and /unblock handlers, and the existing next() return sites to
implement this change.
- Add resolveUser() helper that accepts @username or numeric tg_id - Update block/unblock/blocklist commands to use resolveUser - Show 'ID: <tg_id>' in /blocklist for users without a username - Add isValidBlockTarget() validation in command router - Add invalid_block_target locale key in all 10 supported languages - Add unit tests covering all new and existing block command flows
- Use invalid_block_target locale key instead of silently calling next() when the argument is neither @username nor numeric ID - Add Persian, Korean, Russian and Ukrainian translations for invalid_block_target key
fa17cd0 to
e5a8c57
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
bot/modules/block/commands.ts (1)
101-107: Optional: deduplicate unresolved IDs before passing to the message layer.If duplicate block records ever exist,
unknownIdscan repeat in output. Deduping here keeps/blocklistcleaner.Proposed refinement
- const foundIds = new Set(usersBlocked.map(u => u.tg_id)); - const unknownIds = tgIdBlocks.filter((id: string) => !foundIds.has(id)); + const uniqueBlockedIds = [...new Set(tgIdBlocks)]; + const foundIds = new Set(usersBlocked.map(u => u.tg_id)); + const unknownIds = uniqueBlockedIds.filter((id: string) => !foundIds.has(id));🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@bot/modules/block/commands.ts` around lines 101 - 107, unknownIds may contain duplicates if tgIdBlocks has repeated ids; deduplicate before calling messages.blocklistMessage to avoid repeated unresolved entries. Replace the current unknownIds computation by filtering tgIdBlocks into a deduped list (e.g., track seen ids with a Set) so the final unknownIds passed to messages.blocklistMessage is unique while still using the existing symbols tgIdBlocks, foundIds, and usersBlocked.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@bot/modules/block/commands.ts`:
- Around line 101-107: unknownIds may contain duplicates if tgIdBlocks has
repeated ids; deduplicate before calling messages.blocklistMessage to avoid
repeated unresolved entries. Replace the current unknownIds computation by
filtering tgIdBlocks into a deduped list (e.g., track seen ids with a Set) so
the final unknownIds passed to messages.blocklistMessage is unique while still
using the existing symbols tgIdBlocks, foundIds, and usersBlocked.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 332b0d99-1a6f-4c5a-b46c-9f709841d688
📒 Files selected for processing (14)
bot/modules/block/commands.tsbot/modules/block/index.tsbot/modules/block/messages.tslocales/de.yamllocales/en.yamllocales/es.yamllocales/fa.yamllocales/fr.yamllocales/it.yamllocales/ko.yamllocales/pt.yamllocales/ru.yamllocales/uk.yamltests/bot/modules/block/commands.spec.ts
✅ Files skipped from review due to trivial changes (11)
- locales/en.yaml
- locales/fr.yaml
- locales/pt.yaml
- locales/de.yaml
- locales/it.yaml
- locales/ru.yaml
- bot/modules/block/index.ts
- locales/fa.yaml
- locales/es.yaml
- locales/uk.yaml
- tests/bot/modules/block/commands.spec.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- locales/ko.yaml
- bot/modules/block/messages.ts
Closes #701
Problem
The
/blockcommand only accepted@username. An impersonator can removetheir Telegram username at any time, making them immune to blocking. The
Telegram user ID is a permanent numeric identifier that cannot be changed.
Solution
/block <ID>and/unblock <ID>now accept a numeric Telegram user IDin addition to
@username/blocklistdisplaysID: <tg_id>for blocked users with no usernameisValidBlockTarget()input validation in the command routerinvalid_block_targetlocale key in all 10 supported languagesTests
12 new unit tests added in
tests/bot/modules/block/commands.spec.ts.All 98 tests pass.
Usage
/block @spammer ← unchanged
/block 123456789 ← NEW: block by Telegram ID
/unblock 123456789 ← NEW: unblock by Telegram ID
Summary by CodeRabbit