|
|
|
|
@@ -1,6 +1,8 @@
|
|
|
|
|
"""Telegram command handlers for JIGAIDO - Thin wrappers around core services."""
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
|
|
|
|
from functools import wraps
|
|
|
|
|
from typing import Optional
|
|
|
|
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
|
|
|
|
@@ -20,6 +22,36 @@ TRACKING_SERVICE = TrackingService(TRACKING_STORAGE, ROOM_STORAGE)
|
|
|
|
|
TELEGRAM_BOT_USERNAME = "your_bot_username"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_due_date(due_date_ts: int | None, timezone_str: str) -> str:
|
|
|
|
|
"""Format due date as human-readable with timezone.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
No due date: (none shown)
|
|
|
|
|
Date only: 4 April 2026
|
|
|
|
|
Date + time: 4 April 2026 14:30
|
|
|
|
|
With timezone: 4 April 2026 14:30 (Asia/Jakarta)
|
|
|
|
|
"""
|
|
|
|
|
if not due_date_ts:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
tz = ZoneInfo(timezone_str)
|
|
|
|
|
except (KeyError, ZoneInfoNotFoundError):
|
|
|
|
|
tz = ZoneInfo("UTC")
|
|
|
|
|
|
|
|
|
|
dt = datetime.fromtimestamp(due_date_ts, tz=tz)
|
|
|
|
|
|
|
|
|
|
date_str = dt.strftime("%-d %B %Y")
|
|
|
|
|
|
|
|
|
|
if dt.hour != 0 or dt.minute != 0:
|
|
|
|
|
date_str += f" {dt.strftime('%H:%M')}"
|
|
|
|
|
date_str += f" ({timezone_str})"
|
|
|
|
|
|
|
|
|
|
return date_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_args(text: str) -> list[str]:
|
|
|
|
|
if not text:
|
|
|
|
|
return []
|
|
|
|
|
@@ -105,8 +137,6 @@ def format_bounty(b, show_id: bool = True, slice_length: int = 0) -> str:
|
|
|
|
|
parts.append(f"⏰ Today (OVERDUE)")
|
|
|
|
|
else:
|
|
|
|
|
parts.append(f"⏰ {due_str} ({days_left}d)")
|
|
|
|
|
if b.created_by_user_id:
|
|
|
|
|
parts.append(f"by {b.created_by_user_id}")
|
|
|
|
|
return " | ".join(parts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -241,7 +271,8 @@ async def cmd_add(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
|
|
|
|
|
due_str = ""
|
|
|
|
|
if due_date_ts:
|
|
|
|
|
due_str = f" | Due: {time.strftime('%Y-%m-%d', time.localtime(due_date_ts))}"
|
|
|
|
|
timezone_str = BOUNTY_SERVICE.get_timezone(room_id)
|
|
|
|
|
due_str = f" | Due: {format_due_date(due_date_ts, timezone_str)}"
|
|
|
|
|
await update.message.reply_text(
|
|
|
|
|
f"✅ Bounty added (#{bounty.id}){due_str}",
|
|
|
|
|
disable_web_page_preview=True,
|
|
|
|
|
@@ -485,6 +516,65 @@ async def cmd_timezone(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
await update.message.reply_text(f"✅ Timezone set to {timezone_str}.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _find_user_id_by_username(room_id: int, username: str) -> int | None:
|
|
|
|
|
"""Find user_id by username from bounty creators in the room."""
|
|
|
|
|
bounties = BOUNTY_SERVICE.list_bounties(room_id)
|
|
|
|
|
for bounty in bounties:
|
|
|
|
|
if (
|
|
|
|
|
bounty.created_by_username
|
|
|
|
|
and bounty.created_by_username.lower() == username.lower()
|
|
|
|
|
):
|
|
|
|
|
return bounty.created_by_user_id
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def cmd_admin(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
args = extract_args(update.message.text)
|
|
|
|
|
if not args or args[0] not in ("add", "remove"):
|
|
|
|
|
await update.message.reply_text(
|
|
|
|
|
"Usage:\n"
|
|
|
|
|
"/admin add @username — add admin\n"
|
|
|
|
|
"/admin remove @username — remove admin"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
subcommand = args[0]
|
|
|
|
|
|
|
|
|
|
if len(args) < 2:
|
|
|
|
|
await update.message.reply_text(f"Usage: /admin {subcommand} @username")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
raw_username = args[1]
|
|
|
|
|
if not raw_username.startswith("@"):
|
|
|
|
|
await update.message.reply_text(f"Usage: /admin {subcommand} @username")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
username = raw_username[1:]
|
|
|
|
|
|
|
|
|
|
user_id = get_user_id(update)
|
|
|
|
|
room_id = get_room_id(update)
|
|
|
|
|
|
|
|
|
|
if not BOUNTY_SERVICE.is_admin(room_id, user_id):
|
|
|
|
|
await update.message.reply_text("⛔ Only admins can perform this action.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
target_user_id = _find_user_id_by_username(room_id, username)
|
|
|
|
|
|
|
|
|
|
if target_user_id is None:
|
|
|
|
|
await update.message.reply_text(f"⛔ User @{username} not found.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if subcommand == "add":
|
|
|
|
|
BOUNTY_SERVICE.add_admin(room_id, target_user_id, user_id)
|
|
|
|
|
await update.message.reply_text(f"✅ @{username} is now an admin.")
|
|
|
|
|
elif subcommand == "remove":
|
|
|
|
|
BOUNTY_SERVICE.remove_admin(room_id, target_user_id, user_id)
|
|
|
|
|
await update.message.reply_text(f"✅ @{username} is no longer an admin.")
|
|
|
|
|
except PermissionError as e:
|
|
|
|
|
await update.message.reply_text(f"⛔ {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def cmd_help(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
await update.message.reply_text(
|
|
|
|
|
"👻 JIGAIDO Commands:\n\n"
|
|
|
|
|
|