Compare commits
6 Commits
fix/issue-
...
9cc9a6bf2f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cc9a6bf2f | ||
| e3b813661d | |||
| bdb0f3cd8b | |||
|
|
649b1ffbd3 | ||
|
|
d38d47fb79 | ||
|
|
780cba6301 |
@@ -15,7 +15,6 @@ from commands import (
|
|||||||
cmd_my,
|
cmd_my,
|
||||||
cmd_show,
|
cmd_show,
|
||||||
cmd_start,
|
cmd_start,
|
||||||
cmd_timezone,
|
|
||||||
cmd_track,
|
cmd_track,
|
||||||
cmd_untrack,
|
cmd_untrack,
|
||||||
cmd_update,
|
cmd_update,
|
||||||
@@ -43,7 +42,6 @@ def build_app() -> Application:
|
|||||||
app.add_handler(CommandHandler("delete", cmd_delete))
|
app.add_handler(CommandHandler("delete", cmd_delete))
|
||||||
app.add_handler(CommandHandler("track", cmd_track))
|
app.add_handler(CommandHandler("track", cmd_track))
|
||||||
app.add_handler(CommandHandler("untrack", cmd_untrack))
|
app.add_handler(CommandHandler("untrack", cmd_untrack))
|
||||||
app.add_handler(CommandHandler("timezone", cmd_timezone))
|
|
||||||
app.add_handler(CommandHandler("show", cmd_show))
|
app.add_handler(CommandHandler("show", cmd_show))
|
||||||
|
|
||||||
app.add_handler(MessageHandler(filters.COMMAND, cmd_help))
|
app.add_handler(MessageHandler(filters.COMMAND, cmd_help))
|
||||||
@@ -60,7 +58,6 @@ async def post_init(app: Application) -> None:
|
|||||||
("edit", "Edit a bounty"),
|
("edit", "Edit a bounty"),
|
||||||
("track", "Track a bounty"),
|
("track", "Track a bounty"),
|
||||||
("untrack", "Stop tracking"),
|
("untrack", "Stop tracking"),
|
||||||
("timezone", "Get/set room timezone"),
|
|
||||||
("show", "Show bounty details"),
|
("show", "Show bounty details"),
|
||||||
("help", "Show help"),
|
("help", "Show help"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -114,12 +114,17 @@ def parse_args(
|
|||||||
return text, link, due_date_ts, clear_link, clear_date
|
return text, link, due_date_ts, clear_link, clear_date
|
||||||
|
|
||||||
|
|
||||||
def format_bounty(b, show_id: bool = True, room_id: int | None = None) -> str:
|
def format_bounty(
|
||||||
|
b, show_id: bool = True, slice_length: int = 0, room_id: int | None = None
|
||||||
|
) -> str:
|
||||||
parts = []
|
parts = []
|
||||||
if show_id:
|
if show_id:
|
||||||
parts.append(f"[#{b.id}]")
|
parts.append(f"[#{b.id}]")
|
||||||
if b.text:
|
if b.text:
|
||||||
parts.append(b.text)
|
text = b.text
|
||||||
|
if slice_length > 0 and len(text) > slice_length:
|
||||||
|
text = text[:slice_length] + "..."
|
||||||
|
parts.append(text)
|
||||||
if b.link:
|
if b.link:
|
||||||
parts.append(f"🔗 {b.link}")
|
parts.append(f"🔗 {b.link}")
|
||||||
if b.due_date_ts:
|
if b.due_date_ts:
|
||||||
@@ -132,7 +137,7 @@ def format_bounty(b, show_id: bool = True, room_id: int | None = None) -> str:
|
|||||||
if days_left < 0:
|
if days_left < 0:
|
||||||
parts.append(f"⏰ {due_str} (OVERDUE)")
|
parts.append(f"⏰ {due_str} (OVERDUE)")
|
||||||
elif days_left == 0:
|
elif days_left == 0:
|
||||||
parts.append(f"⏰ {due_str} (TODAY)")
|
parts.append(f"⏰ Today (OVERDUE)")
|
||||||
else:
|
else:
|
||||||
parts.append(f"⏰ {due_str} ({days_left}d)")
|
parts.append(f"⏰ {due_str} ({days_left}d)")
|
||||||
if b.created_by_user_id:
|
if b.created_by_user_id:
|
||||||
@@ -165,13 +170,60 @@ def get_room_id(update: Update) -> int:
|
|||||||
|
|
||||||
async def cmd_bounty(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
async def cmd_bounty(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
room_id = get_room_id(update)
|
room_id = get_room_id(update)
|
||||||
bounties = BOUNTY_SERVICE.list_bounties(room_id)
|
args = extract_args(update.message.text)
|
||||||
|
|
||||||
if not bounties:
|
show_all = "all" in args
|
||||||
await update.message.reply_text("No bounties yet.")
|
args = [a for a in args if a != "all"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
limit = int(args[0]) if args else 5
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
limit = 5
|
||||||
|
|
||||||
|
now = int(time.time())
|
||||||
|
cutoff_24h = now - 86400
|
||||||
|
|
||||||
|
all_bounties = BOUNTY_SERVICE.list_bounties(room_id)
|
||||||
|
|
||||||
|
def is_expired(b) -> bool:
|
||||||
|
return b.due_date_ts is not None and b.due_date_ts < cutoff_24h
|
||||||
|
|
||||||
|
def sort_key(b):
|
||||||
|
if b.due_date_ts is not None:
|
||||||
|
return (0, b.due_date_ts)
|
||||||
|
return (1, b.created_at)
|
||||||
|
|
||||||
|
filtered_bounties = [b for b in all_bounties if not is_expired(b) or show_all]
|
||||||
|
filtered_bounties.sort(key=sort_key)
|
||||||
|
|
||||||
|
total_count = len(filtered_bounties)
|
||||||
|
displayed_bounties = filtered_bounties[:limit]
|
||||||
|
|
||||||
|
if not displayed_bounties:
|
||||||
|
if show_all:
|
||||||
|
await update.message.reply_text("No bounties yet.")
|
||||||
|
else:
|
||||||
|
await update.message.reply_text(
|
||||||
|
"No active bounties. Use /bounty all to show expired."
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
lines = [format_bounty(b, show_id=True, room_id=room_id) for b in bounties]
|
lines = []
|
||||||
|
if limit < total_count:
|
||||||
|
lines.append(f"Showing {limit} of {total_count} bounties:")
|
||||||
|
slice_length = 40
|
||||||
|
elif show_all and total_count > limit:
|
||||||
|
lines.append(f"Showing {limit} of {total_count} bounties (including expired):")
|
||||||
|
slice_length = 40
|
||||||
|
else:
|
||||||
|
lines.append(f"Showing {total_count} bounties:")
|
||||||
|
slice_length = 0
|
||||||
|
|
||||||
|
for b in displayed_bounties:
|
||||||
|
lines.append(
|
||||||
|
format_bounty(b, show_id=True, slice_length=slice_length, room_id=room_id)
|
||||||
|
)
|
||||||
|
|
||||||
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
||||||
|
|
||||||
|
|
||||||
@@ -228,7 +280,6 @@ async def cmd_add(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|||||||
if due_date_ts:
|
if due_date_ts:
|
||||||
timezone_str = BOUNTY_SERVICE.get_timezone(room_id)
|
timezone_str = BOUNTY_SERVICE.get_timezone(room_id)
|
||||||
due_str = f" | Due: {format_due_date(due_date_ts, timezone_str)}"
|
due_str = f" | Due: {format_due_date(due_date_ts, timezone_str)}"
|
||||||
|
|
||||||
await update.message.reply_text(
|
await update.message.reply_text(
|
||||||
f"✅ Bounty added (#{bounty.id}){due_str}",
|
f"✅ Bounty added (#{bounty.id}){due_str}",
|
||||||
disable_web_page_preview=True,
|
disable_web_page_preview=True,
|
||||||
@@ -427,19 +478,18 @@ async def cmd_show(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|||||||
title = bounty.text or "(no text)"
|
title = bounty.text or "(no text)"
|
||||||
lines.append(f"[#{bounty.id}] {title}")
|
lines.append(f"[#{bounty.id}] {title}")
|
||||||
|
|
||||||
due_parts = []
|
if bounty.link:
|
||||||
|
lines.append(f"🔗 {bounty.link}")
|
||||||
|
|
||||||
if bounty.due_date_ts:
|
if bounty.due_date_ts:
|
||||||
due_str = time.strftime("%d %B %Y %H:%M", time.localtime(bounty.due_date_ts))
|
due_str = format_due_date(bounty.due_date_ts, timezone)
|
||||||
due_parts.append(f"Due: {due_str} ({timezone})")
|
lines.append(f"📅 {due_str}")
|
||||||
|
|
||||||
username = bounty.created_by_username or f"user#{bounty.created_by_user_id}"
|
username = bounty.created_by_username or f"user#{bounty.created_by_user_id}"
|
||||||
if bounty.link:
|
lines.append(f"👤 @{username}")
|
||||||
due_parts.append(f"{bounty.link} by @{username}")
|
|
||||||
else:
|
|
||||||
due_parts.append(f"by @{username}")
|
|
||||||
|
|
||||||
if due_parts:
|
created_str = time.strftime("%Y-%m-%d %H:%M", time.localtime(bounty.created_at))
|
||||||
lines.extend(due_parts)
|
lines.append(f"📌 Created: {created_str}")
|
||||||
|
|
||||||
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
||||||
|
|
||||||
@@ -457,38 +507,8 @@ async def cmd_help(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|||||||
"/delete <id> — delete bounty (admin only)\n"
|
"/delete <id> — delete bounty (admin only)\n"
|
||||||
"/track <id> — track a bounty (groups only)\n"
|
"/track <id> — track a bounty (groups only)\n"
|
||||||
"/untrack <id> — stop tracking (groups only)\n"
|
"/untrack <id> — stop tracking (groups only)\n"
|
||||||
"/timezone [tz] — get/set room timezone (admin only)\n"
|
|
||||||
"/show <id> — show bounty details\n"
|
"/show <id> — show bounty details\n"
|
||||||
"/start — re-initialize\n"
|
"/start — re-initialize\n"
|
||||||
"/help — this message",
|
"/help — this message",
|
||||||
disable_web_page_preview=True,
|
disable_web_page_preview=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def cmd_timezone(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
||||||
args = extract_args(update.message.text)
|
|
||||||
room_id = get_room_id(update)
|
|
||||||
user_id = get_user_id(update)
|
|
||||||
|
|
||||||
if not args:
|
|
||||||
current_tz = BOUNTY_SERVICE.get_timezone(room_id)
|
|
||||||
await update.message.reply_text(f"Current timezone: {current_tz}")
|
|
||||||
return
|
|
||||||
|
|
||||||
timezone_str = args[0]
|
|
||||||
|
|
||||||
try:
|
|
||||||
ZoneInfo(timezone_str)
|
|
||||||
except (KeyError, ZoneInfoNotFoundError):
|
|
||||||
await update.message.reply_text(
|
|
||||||
"⛔ Invalid timezone. Use IANA format (e.g., Asia/Jakarta)"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
BOUNTY_SERVICE.set_timezone(room_id, timezone_str, user_id)
|
|
||||||
except PermissionError as e:
|
|
||||||
await update.message.reply_text(f"⛔ {e}")
|
|
||||||
return
|
|
||||||
|
|
||||||
await update.message.reply_text(f"✅ Timezone set to {timezone_str}.")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user