fix: use Telegram API to lookup users by username
_find_user_id_by_username now uses ctx.bot.get_chat() to lookup any user by username, not just bounty creators. This allows /admin add to work for any user in the group.
This commit is contained in:
@@ -771,16 +771,15 @@ async def cmd_recover(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|||||||
await update.message.reply_text("\n".join(response_lines))
|
await update.message.reply_text("\n".join(response_lines))
|
||||||
|
|
||||||
|
|
||||||
def _find_user_id_by_username(room_id: int, username: str) -> int | None:
|
async def _find_user_id_by_username(
|
||||||
"""Find user_id by username from bounty creators in the room."""
|
ctx: ContextTypes.DEFAULT_TYPE, username: str
|
||||||
bounties = BOUNTY_SERVICE.list_bounties(room_id)
|
) -> int | None:
|
||||||
for bounty in bounties:
|
"""Find user_id by username using Telegram API."""
|
||||||
if (
|
try:
|
||||||
bounty.created_by_username
|
chat = await ctx.bot.get_chat(f"@{username}")
|
||||||
and bounty.created_by_username.lower() == username.lower()
|
return chat.id
|
||||||
):
|
except Exception:
|
||||||
return bounty.created_by_user_id
|
return None
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _find_username_by_user_id(room_id: int, user_id: int) -> str | None:
|
def _find_username_by_user_id(room_id: int, user_id: int) -> str | None:
|
||||||
@@ -845,7 +844,7 @@ async def cmd_admin(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|||||||
await update.message.reply_text("⛔ Only admins can perform this action.")
|
await update.message.reply_text("⛔ Only admins can perform this action.")
|
||||||
return
|
return
|
||||||
|
|
||||||
target_user_id = _find_user_id_by_username(room_id, username)
|
target_user_id = await _find_user_id_by_username(ctx, username)
|
||||||
|
|
||||||
if target_user_id is None:
|
if target_user_id is None:
|
||||||
await update.message.reply_text(f"⛔ User @{username} not found.")
|
await update.message.reply_text(f"⛔ User @{username} not found.")
|
||||||
|
|||||||
Reference in New Issue
Block a user