Source code for sidequest_common.models.groups

 1"""Group voice channels: short-lived Discord voice channels members spawn from the ``#lfg`` panel.
 2
 3The bot creates the channel and a ``GroupSession`` row, reports who is in it while it lives, and closes the session
 4(with the channel's text chat) when the channel empties. Officers read the stats; the transcript is purged after
 5``GROUP_TRANSCRIPT_RETENTION_DAYS``.
 6"""
 7
 8from datetime import datetime  # SQLAlchemy resolves Mapped[] annotations at runtime
 9from uuid import UUID  # SQLAlchemy resolves Mapped[] annotations at runtime
10
11from advanced_alchemy.base import UUIDAuditBase
12from advanced_alchemy.types import DateTimeUTC, JsonB
13from sqlalchemy import BigInteger, ForeignKey, Integer, String, UniqueConstraint
14from sqlalchemy.orm import Mapped, mapped_column
15
16
[docs] 17class GroupSession(UUIDAuditBase): 18 """One group voice channel's life, from the panel click to teardown. 19 20 ``kind`` is an ``EventKind`` value from ``GROUP_KINDS``. ``event_id`` is set when the channel was opened from a 21 scheduled guild event. ``peak_size`` and ``grouped_minutes`` (minutes with two or more members present) only 22 ever rise; a session counts toward stats when ``peak_size >= 2`` and ``grouped_minutes >= 23 COUNTED_GROUP_MINUTES``. 24 25 ``pug_role_id`` is set for a PUG group: the temporary Discord role its invite link grants to outside players, 26 allowed into this channel only. The bot deletes the role when the group ends. ``pug_minutes`` (only ever rises) 27 counts minutes with at least one guild member and one of those outside players present together; a PUG group 28 counts toward its creator's PUG-host badges once it reaches ``COUNTED_GROUP_MINUTES``. 29 """ 30 31 __tablename__ = "group_session" 32 __table_args__ = {"comment": "Group voice channels spawned from the LFG panel, bot-reported."} 33 34 discord_channel_id: Mapped[int] = mapped_column(BigInteger, unique=True) 35 channel_name: Mapped[str] = mapped_column(String(100)) 36 kind: Mapped[str] = mapped_column(String(20), index=True) 37 name: Mapped[str | None] = mapped_column(String(80), default=None) 38 note: Mapped[str | None] = mapped_column(String(200), default=None) 39 needs: Mapped[list[str]] = mapped_column(JsonB, default=list) 40 cap: Mapped[int | None] = mapped_column(Integer, default=None) 41 creator_discord_id: Mapped[int] = mapped_column(BigInteger, index=True) 42 owner_discord_id: Mapped[int] = mapped_column(BigInteger) 43 event_id: Mapped[UUID | None] = mapped_column( 44 ForeignKey("guild_event.id", ondelete="SET NULL"), index=True, default=None 45 ) 46 card_message_id: Mapped[int | None] = mapped_column(BigInteger, default=None) 47 pug_role_id: Mapped[int | None] = mapped_column(BigInteger, default=None) 48 started_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True) 49 ended_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), index=True, default=None) 50 end_reason: Mapped[str | None] = mapped_column(String(20), default=None) 51 peak_size: Mapped[int] = mapped_column(Integer, default=0) 52 grouped_minutes: Mapped[int] = mapped_column(Integer, default=0) 53 pug_minutes: Mapped[int] = mapped_column(Integer, default=0)
54 55
[docs] 56class GroupSessionMember(UUIDAuditBase): 57 """Minutes one Discord member spent in one group channel. Bot-reported; stored as ``max(existing, reported)``.""" 58 59 __tablename__ = "group_session_member" 60 __table_args__ = ( 61 UniqueConstraint("session_id", "discord_id", name="uq_group_session_member_session_discord"), 62 {"comment": "Minutes a Discord member spent in a group voice channel, bot-reported."}, 63 ) 64 65 session_id: Mapped[UUID] = mapped_column(ForeignKey("group_session.id", ondelete="CASCADE"), index=True) 66 discord_id: Mapped[int] = mapped_column(BigInteger, index=True) 67 display_name: Mapped[str] = mapped_column(String(100)) 68 minutes: Mapped[int] = mapped_column(Integer, default=0)
69 70
[docs] 71class GroupSessionPair(UUIDAuditBase): 72 """Minutes two members spent in the same group channel together. ``discord_id_a < discord_id_b`` always.""" 73 74 __tablename__ = "group_session_pair" 75 __table_args__ = ( 76 UniqueConstraint("session_id", "discord_id_a", "discord_id_b", name="uq_group_session_pair_session_ab"), 77 {"comment": "Minutes a pair of members spent together in a group voice channel, bot-reported."}, 78 ) 79 80 session_id: Mapped[UUID] = mapped_column(ForeignKey("group_session.id", ondelete="CASCADE"), index=True) 81 discord_id_a: Mapped[int] = mapped_column(BigInteger, index=True) 82 discord_id_b: Mapped[int] = mapped_column(BigInteger, index=True) 83 minutes: Mapped[int] = mapped_column(Integer, default=0)
84 85
[docs] 86class GroupSessionTranscript(UUIDAuditBase): 87 """A closed group channel's text chat, kept for moderation and purged after the retention window. 88 89 ``messages`` is a list of ``GroupTranscriptMessage`` dicts, oldest first. 90 """ 91 92 __tablename__ = "group_session_transcript" 93 __table_args__ = {"comment": "Text chat of a closed group voice channel; purged after 30 days."} 94 95 session_id: Mapped[UUID] = mapped_column( 96 ForeignKey("group_session.id", ondelete="CASCADE"), unique=True, index=True 97 ) 98 messages: Mapped[list[dict[str, object]]] = mapped_column(JsonB, default=list)