Skip to content

Commit 67b1a80

Browse files
author
0xMett
committed
fix(moderation): resolve @username via Telegram API fallback in _resolve_user_id
Add bot.get_chat() as a third fallback when resolving @username targets, after admin list and known_users lookups. Fixes /unban (and /ban, /mute, /unmute) failing for users not yet tracked in known_users. Also update UNBAN_USAGE string to mention @username as a valid option.
1 parent cc07e65 commit 67b1a80

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/python_italy_bot/handlers/moderation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,13 @@ async def _resolve_user_id(
623623
known = await moderation_service.get_known_user_by_username(username_lower)
624624
if known is not None:
625625
return known.user_id
626+
# Try Telegram API as last resort
627+
try:
628+
resolved_chat = await context.bot.get_chat(f"@{username_lower}")
629+
if resolved_chat.id:
630+
return resolved_chat.id
631+
except Exception:
632+
pass
626633
return None
627634
if re.match(r"^-?\d+$", target):
628635
return int(target)

src/python_italy_bot/strings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def ban_notification(
101101

102102

103103
# Unban
104-
UNBAN_USAGE = "Uso: /unban user_id, o rispondi al messaggio con /unban"
104+
UNBAN_USAGE = "Uso: /unban user_id|@username, o rispondi al messaggio con /unban"
105105

106106

107107
def unban_success(success_count: int, fail_count: int) -> str:

0 commit comments

Comments
 (0)