1"""Leveling schemas, and how much XP each kind of activity is worth."""
2
3from datetime import datetime # pydantic needs it at runtime
4from enum import StrEnum
5
6from pydantic import BaseModel, ConfigDict, Field
7
8VOICE_XP_PER_MINUTE = 5
9"""XP per minute in voice with at least one other member, undeafened, outside the AFK channel."""
10EVENT_XP = 150
11"""XP for attending a guild event: once attendance marks you came, or a day after it started if nobody took it."""
12GROUP_XP = 50
13"""XP for each #lfg group you spent ``COUNTED_GROUP_MINUTES`` in, once the group has ended."""
14EVENT_XP_GRACE_HOURS = 24
15"""An event nobody took attendance for pays its sign-ups this long after it started."""
16XP_LOOKBACK_DAYS = 30
17"""Events and groups older than this never pay XP, so turning leveling on doesn't reward ancient history."""
18MAX_VOICE_XP_MEMBERS = 500
19MAX_BACKFILL_BUCKETS = 5000
20"""Buckets accepted per ``PUT /activity/messages`` call."""
21
22
[docs]
23class XPSource(StrEnum):
24 """What earned XP besides chat."""
25
26 VOICE = "voice"
27 EVENT = "event"
28 GROUP = "group"
29
30
[docs]
31class XPAwardResult(BaseModel):
32 """Outcome of ``POST /members/{user_id}/xp`` (one chat message's worth of XP).
33
34 ``awarded`` is False while the member is on cooldown (or leveling is disabled); every other field then
35 reflects the unchanged totals. ``earned_role_ids`` are level-role rewards for every level crossed by this
36 award; ``remove_role_ids`` are lower level-roles to take away when the guild has ``level_roles_stack=False``.
37 """
38
39 awarded: bool
40 amount: int = Field(description="XP (experience points) awarded by this message.")
41 xp: int = Field(description="The member's new total XP (experience points).")
42 level: int
43 previous_level: int
44 leveled_up: bool
45 earned_role_ids: list[int] = Field(default_factory=list)
46 remove_role_ids: list[int] = Field(default_factory=list)
47
48
[docs]
49class XPGrantResult(XPAwardResult):
50 """XP one member got from voice, an event or an #lfg group, with the same level-up details as a chat award."""
51
52 user_id: int = Field(description="The member's Discord user ID.")
53 source: XPSource
54
55
[docs]
56class VoiceXPRequest(BaseModel):
57 """Body for ``POST /xp/voice``: members who spent ``minutes`` in voice, already filtered by the bot."""
58
59 user_ids: list[int] = Field(
60 default_factory=list, max_length=MAX_VOICE_XP_MEMBERS, description="Discord user IDs to award XP to."
61 )
62 present_user_ids: list[int] = Field(
63 default_factory=list,
64 max_length=MAX_VOICE_XP_MEMBERS,
65 description=(
66 "Discord user IDs present under the stats voice rule (any voice/stage channel but the AFK channel, "
67 "with at least two non-bot members), which may be a superset of user_ids. Their voice minutes are "
68 "recorded even when leveling is disabled or they aren't eligible for voice XP."
69 ),
70 )
71 minutes: int = Field(default=1, ge=1, le=10, description="Minutes each member spent in voice since the last call.")
72
73
[docs]
74class MessageBackfillBucket(BaseModel):
75 """One hour's message count for one member, read once from Discord channel history."""
76
77 user_id: int = Field(description="The member's Discord user ID.")
78 hour_start: datetime = Field(description="The UTC hour this count covers; normalized to the hour on save.")
79 messages: int = Field(ge=0, description="Messages sent in that hour.")
80
81
[docs]
82class MessageBackfill(BaseModel):
83 """Body for ``PUT /activity/messages``: message counts read once from Discord channel history."""
84
85 buckets: list[MessageBackfillBucket] = Field(max_length=MAX_BACKFILL_BUCKETS)
86
87
[docs]
88class MessageBackfillResult(BaseModel):
89 """Reply to ``PUT /activity/messages``."""
90
91 buckets: int = Field(description="How many (member, hour) buckets were touched.")
92
93
[docs]
94class XPActivityReport(BaseModel):
95 """Event and #lfg group XP paid out by one ``POST /xp/activity`` call; empty when nothing new qualified."""
96
97 grants: list[XPGrantResult] = Field(default_factory=list)
98
99
[docs]
100class RankSchema(BaseModel):
101 """A member's standing. ``rank`` is 1-based position by XP, or None if they have no XP yet."""
102
103 user_id: int
104 rank: int | None
105 xp: int = Field(description="The member's total XP (experience points).")
106 level: int
107 message_count: int
108 xp_into_level: int = Field(description="XP (experience points) earned since reaching the current level.")
109 xp_for_next_level: int = Field(description="XP (experience points) needed to reach the next level.")
110
111
[docs]
112class LeaderboardEntry(BaseModel):
113 """One leaderboard row."""
114
115 rank: int
116 user_id: int
117 xp: int = Field(description="The member's total XP (experience points).")
118 level: int
119 message_count: int
120
121
[docs]
122class LeaderboardPage(BaseModel):
123 """A page of the leaderboard, highest XP first."""
124
125 entries: list[LeaderboardEntry]
126 total: int
127 limit: int
128 offset: int
129
130
131MAX_XP = 10_000_000_000
132"""Ceiling for admin XP overrides (~level 1800); keeps the level calculation bounded and fits a BIGINT."""
133
134
[docs]
135class SetXPRequest(BaseModel):
136 """Admin override of a member's XP total (level is recomputed)."""
137
138 xp: int = Field(ge=0, le=MAX_XP, description="The new total XP (experience points) to set.")
139
140
[docs]
141class MemberLevelSchema(BaseModel):
142 """Raw stored level row."""
143
144 model_config = ConfigDict(from_attributes=True)
145
146 guild_id: int
147 user_id: int
148 xp: int = Field(description="The member's total XP (experience points).")
149 level: int
150 message_count: int
151
152
[docs]
153class LevelRoleSchema(BaseModel):
154 """Role rewarded at a level."""
155
156 model_config = ConfigDict(from_attributes=True)
157
158 level: int
159 role_id: int
160
161
[docs]
162class LevelRoleUpsert(BaseModel):
163 """Body for ``PUT /level-roles/{level}``."""
164
165 role_id: int