feat(/add): time parsing, link uniqueness, admin-only

- Add time parsing (HH:MM format) after date
  Example: /add Fix bug https://github.com/foo/bar april 15 14:30
- Update check_link_unique to return conflicting bounty ID
- Add_bounty now includes bounty ID in duplicate link error
- cmd_add now catches PermissionError and displays admin-only message
- Update usage text and help message
- Fixes #45
This commit is contained in:
shokollm
2026-04-04 05:44:27 +00:00
parent 42ed551554
commit 6fb4b38c66
2 changed files with 68 additions and 23 deletions

View File

@@ -96,21 +96,24 @@ class BountyService:
def check_link_unique(
self, room_id: int, link: str | None, exclude_bounty_id: int | None = None
) -> bool:
"""Check if a link is unique within a room (not used by another bounty)."""
) -> int | None:
"""Check if a link is unique within a room (not used by another bounty).
Returns the conflicting bounty ID if found, or None if unique/allowed.
"""
if not link:
return True
return None
room_data = self._storage.load(room_id)
if room_data is None:
return True
return None
for bounty in room_data.bounties:
if bounty.deleted_at is not None:
continue
if bounty.link == link and bounty.id != exclude_bounty_id:
return False
return True
return bounty.id
return None
def add_bounty(
self,
@@ -124,8 +127,11 @@ class BountyService:
if not self.is_admin(room_id, user_id):
raise PermissionError("Only admins can add bounties.")
if not self.check_link_unique(room_id, link):
raise ValueError("A bounty with this link already exists in this room.")
conflicting_id = self.check_link_unique(room_id, link)
if conflicting_id is not None:
raise ValueError(
f"A bounty with this link already exists: #{conflicting_id}"
)
room_data = self._storage.load(room_id)
if room_data is None:
@@ -178,8 +184,10 @@ class BountyService:
if not self.is_admin(room_id, user_id):
raise PermissionError("Only admins can edit bounties.")
if link and not self.check_link_unique(
room_id, link, exclude_bounty_id=bounty_id
if (
link
and self.check_link_unique(room_id, link, exclude_bounty_id=bounty_id)
is not None
):
raise ValueError("A bounty with this link already exists in this room.")