1"""Guild members who have signed in through Discord OAuth."""
2
3from datetime import datetime # SQLAlchemy resolves Mapped[] annotations at runtime
4
5from advanced_alchemy.base import BigIntAuditBase
6from advanced_alchemy.types import DateTimeUTC, JsonB
7from sqlalchemy import BigInteger, Boolean, String, Text
8from sqlalchemy.orm import Mapped, mapped_column
9
10
[docs]
11class Member(BigIntAuditBase):
12 """One Discord user who has logged into the member hub, or whom a Quest Master confirmed from Discord.
13
14 ``id`` is a small internal integer (the contract's ``Me.id``), distinct from ``discord_id`` (Discord's
15 64-bit snowflake, carried as a string in JSON). ``username``/``global_name``/``roles``/``guild_nick``/
16 ``avatar_hash``/``is_guild_member`` are refreshed from Discord both at sign-in and continuously by the bot
17 (``PUT /api/guilds/{guild_id}/member-profiles``: a full sweep plus debounced pushes on Discord profile/role/
18 membership changes), so they stay current even for a member who never signs in again after renaming.
19 ``avatar_hash`` is Discord's raw avatar hash (``null`` for the default avatar); build the CDN URL from it
20 rather than storing a URL that can go stale.
21
22 ``battlenet_id``/``battletag`` are set when the member links Battle.net (one Battle.net account per member, so
23 one Blizzard account can't vouch for several Discord accounts); officers compare the BattleTag to the roster.
24 ``battlenet_character_ids`` is the account's WoW character ids, as Battle.net's account profile API (``wow.profile``
25 scope) lists them at link/relink time; ``null`` until the member links, or if they linked before this field
26 existed (they must relink once for the site to have it). Used to auto-link roster characters (``WowCharacter``)
27 the member provably owns, without guessing from the name.
28
29 ``officer_notice_posted_at`` is when the bot posted the new-signup notice embed for this member in the
30 configured signup-notice channel (``null`` until posted; existing members are backfilled non-null at migration
31 so prod isn't flooded). ``officer_notice_channel_id``/``officer_notice_message_id`` locate that message so it
32 can be edited later. ``officer_notice_resolved_at`` is set once the posted message accurately reflects the
33 member's current roster status (approved via the embed's button, or edited after a site confirmation).
34
35 ``last_login_at`` is ``null`` for a member who has never signed in on the site: a Quest Master confirmed them
36 from Discord (the bot's **Confirm in guild** command), which creates the row from their Discord profile.
37 ``roster_confirmed_via`` says who confirmed ``roster_confirmed_at``: ``"officer"`` (a Quest Master, on the site,
38 the signup notice, Discord or MCP) or ``"battlenet"`` (the roster sync found one of the member's Battle.net
39 characters in the in-game guild). ``null`` while unconfirmed. ``roster_removed_at`` is when a Quest Master last
40 removed the member's confirmation; while set, the roster sync never confirms them again on its own (Battle.net's
41 roster can lag an in-game kick), and an officer confirming them clears it.
42 """
43
44 __tablename__ = "member"
45 __table_args__ = {"comment": "Guild members who have signed in with Discord."}
46
47 discord_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
48 username: Mapped[str] = mapped_column(Text)
49 global_name: Mapped[str | None] = mapped_column(Text, default=None)
50 guild_nick: Mapped[str | None] = mapped_column(Text, default=None)
51 avatar_hash: Mapped[str | None] = mapped_column(Text, default=None)
52 roles: Mapped[list[int]] = mapped_column(JsonB, default=list)
53 is_guild_member: Mapped[bool] = mapped_column(Boolean, default=False)
54 last_login_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
55 battlenet_id: Mapped[int | None] = mapped_column(BigInteger, unique=True, default=None)
56 battletag: Mapped[str | None] = mapped_column(Text, default=None)
57 battlenet_character_ids: Mapped[list[int] | None] = mapped_column(JsonB, default=None)
58 battlenet_linked_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
59 roster_confirmed_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
60 roster_confirmed_via: Mapped[str | None] = mapped_column(String(20), default=None)
61 roster_removed_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
62 officer_notice_posted_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
63 officer_notice_resolved_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
64 officer_notice_channel_id: Mapped[int | None] = mapped_column(BigInteger, default=None)
65 officer_notice_message_id: Mapped[int | None] = mapped_column(BigInteger, default=None)
66 character_nick_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
67 """When the bot made its one Discord nickname call for this member: it set their nickname to the guild character
68 they joined on, or found they already had one and left it. ``null`` until then; the bot never renames them again,
69 so they're free to change it. Members from before this existed are backfilled non-null, so none is renamed."""
70 calendar_token: Mapped[str | None] = mapped_column(Text, unique=True, default=None)
71 """Secret token for the member's private calendar feed (``GET /api/calendar/{token}.ics``); generated lazily."""
72 raid_status: Mapped[str | None] = mapped_column(Text, default=None)
73 """EPGP roster status (``"trial"``/``"raider"``, or ``null`` for not-yet-rostered); set by an officer."""
74 trial_started_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
75 """When ``raid_status`` was last set to ``"trial"``; the EPGP bid order treats a trial as a trial for 14 days."""
76
77 @property
78 def display_name(self) -> str:
79 """Guild nick, else Discord global name, else username."""
80 return self.guild_nick or self.global_name or self.username