Compare commits
1 Commits
dfaf633707
...
fix/issue-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6312d94c0b |
@@ -8,7 +8,6 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
||||
|
||||
from commands import (
|
||||
cmd_add,
|
||||
cmd_admin,
|
||||
cmd_bounty,
|
||||
cmd_delete,
|
||||
cmd_edit,
|
||||
@@ -42,7 +41,6 @@ def build_app() -> Application:
|
||||
app.add_handler(CommandHandler("delete", cmd_delete))
|
||||
app.add_handler(CommandHandler("track", cmd_track))
|
||||
app.add_handler(CommandHandler("untrack", cmd_untrack))
|
||||
app.add_handler(CommandHandler("admin", cmd_admin))
|
||||
|
||||
app.add_handler(MessageHandler(filters.COMMAND, cmd_help))
|
||||
|
||||
@@ -58,7 +56,6 @@ async def post_init(app: Application) -> None:
|
||||
("edit", "Edit a bounty"),
|
||||
("track", "Track a bounty"),
|
||||
("untrack", "Stop tracking"),
|
||||
("admin", "Admin management"),
|
||||
("help", "Show help"),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -291,101 +291,6 @@ async def cmd_untrack(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
await update.message.reply_text("Not tracking bounty #{bounty_id}.")
|
||||
|
||||
|
||||
async def cmd_admin(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
args = extract_args(update.message.text)
|
||||
if not args:
|
||||
await update.message.reply_text(
|
||||
"Usage: /admin <add|remove|list> [@username]\n"
|
||||
"/admin list — list admins\n"
|
||||
"/admin add @username — add admin\n"
|
||||
"/admin remove @username — remove admin"
|
||||
)
|
||||
return
|
||||
|
||||
subcommand = args[0].lower()
|
||||
username = args[1] if len(args) > 1 else None
|
||||
|
||||
requesting_user_id = get_user_id(update)
|
||||
room_id = get_room_id(update)
|
||||
|
||||
if subcommand == "list":
|
||||
admins = BOUNTY_SERVICE.list_admins(room_id)
|
||||
if not admins:
|
||||
await update.message.reply_text("No admins in this room.")
|
||||
return
|
||||
admin_mentions = " ".join(f"admin_id:{uid}" for uid in admins)
|
||||
await update.message.reply_text(f"Admins: {admin_mentions}")
|
||||
return
|
||||
|
||||
if subcommand == "remove":
|
||||
if not username:
|
||||
await update.message.reply_text("Usage: /admin remove @username")
|
||||
return
|
||||
|
||||
if not username.startswith("@"):
|
||||
await update.message.reply_text(
|
||||
f"⛔ {username} is not a valid username (must start with @)."
|
||||
)
|
||||
return
|
||||
|
||||
target_username = username[1:]
|
||||
try:
|
||||
chat = await ctx.bot.get_chat(target_username)
|
||||
target_user_id = chat.id
|
||||
except Exception:
|
||||
await update.message.reply_text(
|
||||
f"⛔ Could not find user @{target_username}."
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
BOUNTY_SERVICE.remove_admin(room_id, target_user_id, requesting_user_id)
|
||||
await update.message.reply_text(
|
||||
f"✅ @{target_username} is no longer an admin."
|
||||
)
|
||||
except PermissionError as e:
|
||||
await update.message.reply_text(f"⛔ {e}")
|
||||
except ValueError:
|
||||
await update.message.reply_text(f"⛔ @{target_username} is not an admin.")
|
||||
return
|
||||
|
||||
if subcommand == "add":
|
||||
if not username:
|
||||
await update.message.reply_text("Usage: /admin add @username")
|
||||
return
|
||||
|
||||
if not username.startswith("@"):
|
||||
await update.message.reply_text(
|
||||
f"⛔ {username} is not a valid username (must start with @)."
|
||||
)
|
||||
return
|
||||
|
||||
target_username = username[1:]
|
||||
try:
|
||||
chat = await ctx.bot.get_chat(target_username)
|
||||
target_user_id = chat.id
|
||||
except Exception:
|
||||
await update.message.reply_text(
|
||||
f"⛔ Could not find user @{target_username}."
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
BOUNTY_SERVICE.add_admin(room_id, target_user_id, requesting_user_id)
|
||||
await update.message.reply_text(f"✅ @{target_username} is now an admin.")
|
||||
except PermissionError as e:
|
||||
await update.message.reply_text(f"⛔ {e}")
|
||||
except ValueError:
|
||||
await update.message.reply_text(
|
||||
f"⛔ @{target_username} is already an admin."
|
||||
)
|
||||
return
|
||||
|
||||
await update.message.reply_text(
|
||||
"Unknown subcommand. Use: /admin <add|remove|list> [@username]"
|
||||
)
|
||||
|
||||
|
||||
async def cmd_start(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
if is_group(update):
|
||||
await update.message.reply_text(
|
||||
|
||||
41
cli/main.py
41
cli/main.py
@@ -127,6 +127,40 @@ def cmd_delete(args):
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def cmd_recover(args):
|
||||
"""List or recover soft-deleted bounties."""
|
||||
bounty_service, _ = create_services()
|
||||
room_id = args.group_id or args.user_id
|
||||
user_id = args.user_id or 0
|
||||
|
||||
if not args.bounty_ids:
|
||||
deleted = bounty_service.list_deleted_bounties(room_id)
|
||||
if not deleted:
|
||||
print("No recoverable bounties")
|
||||
return
|
||||
print("Recoverable bounties:")
|
||||
for b in deleted:
|
||||
from datetime import datetime
|
||||
|
||||
deleted_str = datetime.fromtimestamp(b.deleted_at).strftime("%d %b %Y")
|
||||
print(f" [#{b.id}] {b.text or '(no text)'} | Deleted {deleted_str}")
|
||||
return
|
||||
|
||||
if not bounty_service.is_admin(room_id, user_id):
|
||||
print("Error: Only admins can recover bounties.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
for bounty_id in args.bounty_ids:
|
||||
try:
|
||||
success, msg = bounty_service.recover_bounty(
|
||||
room_id=room_id, bounty_id=bounty_id, user_id=user_id
|
||||
)
|
||||
print(msg)
|
||||
except PermissionError as e:
|
||||
print(f"Error: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def cmd_track(args):
|
||||
"""Track a bounty."""
|
||||
_, tracking_service = create_services()
|
||||
@@ -196,6 +230,9 @@ def main():
|
||||
parser_untrack = subparsers.add_parser("untrack", help="Untrack a bounty")
|
||||
parser_untrack.add_argument("bounty_id", type=int, help="Bounty ID to untrack")
|
||||
|
||||
parser_recover = subparsers.add_parser("recover", help="List or recover soft-deleted bounties")
|
||||
parser_recover.add_argument("bounty_ids", nargs="*", type=int, help="Bounty ID(s) to recover (optional)")
|
||||
|
||||
for sp in [
|
||||
parser_add,
|
||||
parser_list,
|
||||
@@ -204,6 +241,7 @@ def main():
|
||||
parser_delete,
|
||||
parser_track,
|
||||
parser_untrack,
|
||||
parser_recover,
|
||||
]:
|
||||
sp.add_argument(
|
||||
"--group-id", type=int, help="Group context (use group room ID)"
|
||||
@@ -222,7 +260,7 @@ def main():
|
||||
print("Error: either --group-id or --user-id is required", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if args.command in ("add", "list", "my", "update", "delete"):
|
||||
if args.command in ("add", "list", "my", "update", "delete", "recover"):
|
||||
if not (args.group_id or args.user_id):
|
||||
print("Error: --group-id or --user-id required", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -238,6 +276,7 @@ def main():
|
||||
"delete": cmd_delete,
|
||||
"track": cmd_track,
|
||||
"untrack": cmd_untrack,
|
||||
"recover": cmd_recover,
|
||||
}
|
||||
|
||||
if args.command in command_map:
|
||||
|
||||
@@ -210,6 +210,33 @@ class BountyService:
|
||||
self._storage.update_bounty(room_id, bounty)
|
||||
return True
|
||||
|
||||
def recover_bounty(
|
||||
self, room_id: int, bounty_id: int, user_id: int
|
||||
) -> tuple[bool, str]:
|
||||
"""Recover a soft-deleted bounty. Only admins can recover.
|
||||
|
||||
Returns (success, message) tuple.
|
||||
"""
|
||||
all_bounties = self._storage.list_all_bounties(room_id, include_deleted=True)
|
||||
bounty = None
|
||||
for b in all_bounties:
|
||||
if b.id == bounty_id:
|
||||
bounty = b
|
||||
break
|
||||
|
||||
if not bounty:
|
||||
return False, f"Bounty #{bounty_id} not found."
|
||||
|
||||
if bounty.deleted_at is None:
|
||||
return False, f"Bounty #{bounty_id} is not deleted."
|
||||
|
||||
if not self.is_admin(room_id, user_id):
|
||||
raise PermissionError("Only admins can recover bounties.")
|
||||
|
||||
bounty.deleted_at = None
|
||||
self._storage.update_bounty(room_id, bounty)
|
||||
return True, f"Recovered bounty #{bounty_id}."
|
||||
|
||||
|
||||
class TrackingService:
|
||||
"""Service for tracking bounty operations."""
|
||||
|
||||
Reference in New Issue
Block a user