-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path_cog.py
More file actions
342 lines (276 loc) · 12.6 KB
/
_cog.py
File metadata and controls
342 lines (276 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import random
import re
import tomllib
from dataclasses import dataclass
from pathlib import Path
from typing import Literal
import discord
from async_rediscache import RedisCache
from discord.ext import commands, tasks
from pydis_core.utils.logging import get_logger
from bot import constants
from bot.bot import SirRobin
from bot.utils import members
from bot.utils.decorators import in_whitelist
logger = get_logger(__name__)
ELEVATED_ROLES = (constants.Roles.admins, constants.Roles.moderation_team, constants.Roles.events_lead)
ALLOWED_COMMAND_CHANNELS = (constants.Channels.bot_commands, constants.Channels.sir_lancebot_playground,)
# Channels where the game runs.
ALLOWED_CHANNELS = (
constants.Channels.off_topic_0,
constants.Channels.off_topic_1,
constants.Channels.off_topic_2,
)
LEVEL_ROLES = (
constants.Roles.levels_crystal,
constants.Roles.levels_level3,
constants.Roles.levels_s_tier,
constants.Roles.levels_diamond_rank,
constants.Roles.levels_GOAT,
constants.Roles.levels_mtfn,
constants.Roles.levels_champion,
constants.Roles.levels_mythical_python_charmer,
constants.Roles.levels_supernova_wonder,
constants.Roles.levels_ascenion_20
)
class Levels(commands.Cog):
"""Cog that handles all Level functionality."""
#RedisCache[user_id: int, points: int]
user_points_cache = RedisCache()
#RedisCache[role_id: int, point_threshold: int]
levels_cache = RedisCache()
#RedisCache["value", bool]
running = RedisCache()
def __init__(self, bot: SirRobin):
self.bot = bot
self.rules_all = []
self.rules_pool = []
self.rules_active = []
self.rules_folder_path = Path("./bot/exts/levels/rules/")
self.active_rules_num = 3
self.active_reaction_rule_triggers = []
self.active_message_rule_triggers = []
async def cog_load(self) -> None:
"""Run startup tasks needed when cog is first loaded."""
await self._load_rules()
# Fill in cache with data for later functions to use
if await self.levels_cache.length() == 0:
shuffled_roles = random.sample(LEVEL_ROLES, len(LEVEL_ROLES))
init_threshold_dict = dict.fromkeys(shuffled_roles, 0)
await self.levels_cache.update(init_threshold_dict)
if await self.running.get("value", False):
logger.debug("Starting Rules and Point Renormalization tasks")
await self._cycle_rules_task.start()
await self._calculate_point_thresholds_task.start()
async def _load_rules(self) -> None:
"""
Load and parse levels rules for usage.
If a rule file does not comply with the format
and throws and error, it is skipped over.
"""
total_files_loaded = 0
for toml_file in self.rules_folder_path.glob("*.toml"):
with open(toml_file, "rb") as f:
rule_dict = tomllib.load(f)
rule_name = toml_file.stem
try:
rule_triggers = [RuleTrigger(**rule_trigger) for rule_trigger in rule_dict["rule"]]
rule = LevelRules(rule_name, rule_triggers)
except (TypeError, KeyError):
logger.info(f"{toml_file} not properly formatted, skipping.")
continue
self.rules_all.append(rule)
total_files_loaded += 1
logger.info(f"Total rules loaded: {total_files_loaded}")
@tasks.loop(minutes=42.0)
async def _cycle_rules_task(self) -> None:
"""
Change which rules are currently active.
Rules will statistically be used before a repeat is seen.
This is not a guarnatee though.
"""
if len(self.rules_pool) < self.active_rules_num:
# If pool is empty, reshuffle completely to avoid activating same rule twice
self.rules_pool = random.sample(self.rules_all, len(self.rules_all))
self.rules_active = [self.rules_pool.pop() for _ in range(self.active_rules_num)]
logger.debug(f"Cycled active rules to: {[rule.name for rule in self.rules_active]}")
self.active_message_rule_triggers = [
rule for rule in self.rules_active
for rule_trigger in rule.rule_triggers if rule_trigger.interaction_type=="message"
]
self.active_reaction_rule_triggers = [
rule for rule in self.rules_active
for rule_trigger in rule.rule_triggers if rule_trigger.interaction_type=="reaction"
]
# [rule for rule in self.rules_active if rule.interaction_type=="reaction"]
# self.active_message_rule_triggers = [rule for rule in self.rules_active if rule.interaction_type=="message"]
@tasks.loop(minutes=90.0)
async def _calculate_point_thresholds_task(self) -> None:
"""
Calculate point thresholds based on number of roles, aiming for even deciles based on scores.
If current max score is less than 100, it will fix deciles to increments of 10.
"""
user_points = await self.user_points_cache.to_dict()
all_scores = sorted(user_points.values())
if all_scores and all_scores[-1] >= 100:
num_scores = len(all_scores)
num_levels = len(LEVEL_ROLES)
thresholds = [
all_scores[round(num_scores * level/num_levels)]
for level in range(1, num_levels+1)
]
else:
# At the start of the event, just use multiples of 10 up to 100
thresholds = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
levels = await self.levels_cache.to_dict()
new_levels = dict(zip(levels.keys(), thresholds, strict=False))
await self.levels_cache.update(new_levels)
logger.debug(f"Renormalizing score thresholds. Total scores: {len(all_scores)}")
logger.debug(f"New thresholds: {thresholds}")
async def _update_points(self, user_id: int, points: int) -> None:
"""Updates user's score and ensures correct role is assigned."""
logger.debug(f"User {user_id} getting {points} points.")
if not await self.user_points_cache.contains(user_id):
await self.user_points_cache.set(user_id, points)
else:
if points == 0:
return
current_points = await self.user_points_cache.get(user_id)
new_point_total = current_points + points
await self.user_points_cache.set(user_id, new_point_total)
await self._update_role_assignment(user_id)
async def _update_role_assignment(self, user_id: int) -> None:
"""Updates user's role based on current points and role-point thresholds."""
user_points = await self.user_points_cache.get(user_id)
levels = await self.levels_cache.to_dict()
level_to_assign = None
for role, point_threshold in sorted(levels.items(), key=lambda item: item[1]):
level_to_assign = role
if point_threshold >= user_points:
break
guild = self.bot.get_guild(constants.Bot.guild)
role = guild.get_role(level_to_assign)
user = await members.get_or_fetch_member(guild, user_id)
if role in user.roles:
return
logger.debug(f"Assigning {role.name} to {user.name}")
await members.handle_role_change(user, user.add_roles, role)
@commands.Cog.listener()
async def on_message(self, msg: discord.Message) -> None:
"""Listens to messages and checks against active message rules."""
if not await self.running.get("value", False):
return
if msg.channel.id not in ALLOWED_CHANNELS or msg.author.bot:
return
if len(self.active_message_rules) == 0:
return
total_points = 0
rule_matches = 0
for rule_trigger in self.active_message_rule_triggers:
re_pattern = rule_trigger.message_content
match = re.search(re_pattern, msg.content)
if match:
total_points += rule_trigger.points
rule_matches += 1
# Only update points if they've matched any rules
# If they match multiple rules and earn 0 points,
# that should still get them a role
if rule_matches != 0:
user_id = msg.author.id
await self._update_points(user_id, total_points)
@commands.Cog.listener()
async def on_reaction_add(self, reaction: discord.Reaction, user: discord.Member) -> None:
"""
Listens for reactions and checks for against active reaction rules.
It will only listen for reactions added to messages within the bot's message cache.
"""
if not await self.running.get("value", False):
return
if reaction.message.channel.id not in ALLOWED_CHANNELS or user.bot:
return
if len(self.active_reaction_rules) == 0:
return
if isinstance(reaction.emoji, str):
emoji_name = reaction.emoji
else:
emoji_name = reaction.emoji.name
total_points = 0
rule_matches = 0
for rule_trigger in self.active_reaction_rule_triggers:
if emoji_name in rule_trigger.reaction_content:
total_points += rule_trigger.points
rule_matches += 1
# Only update points if they've matched any rules
# If they match multiple rules and earn 0 points,
# that should still get them a role
if rule_matches != 0:
await self._update_points(user.id, total_points)
@commands.group(name="levels")
async def levels_command_group(self, ctx: commands.Context) -> None:
"""Levels group command."""
if not ctx.invoked_subcommand:
await self.bot.invoke_help_command(ctx)
@levels_command_group.command()
@in_whitelist(channels=ALLOWED_COMMAND_CHANNELS)
async def points(self, ctx: commands.Context) -> None:
"""Check how many points you've accrued for the Role Level system."""
user_id = ctx.author.id
if await self.user_points_cache.contains(user_id):
points = await self.user_points_cache.get(user_id)
await ctx.reply(f"You have {points} points.")
else:
await ctx.reply("You have not earned any points so far! :D")
@levels_command_group.command()
@commands.has_any_role(*ELEVATED_ROLES)
async def shuffle_role_order(self, ctx: commands.Context) -> None:
"""Shuffle which roles are assigned to which point thresholds."""
levels = await self.levels_cache.to_dict()
thresholds = levels.values()
role_order = random.sample(LEVEL_ROLES, len(LEVEL_ROLES))
updated_ordering = dict(zip(role_order, thresholds, strict=False))
await self.levels_cache.update(updated_ordering)
logger.info(f"Roles have been re-shuffled per request of {ctx.author.name}")
@levels_command_group.command()
@commands.has_any_role(*ELEVATED_ROLES)
async def start(self, ctx: commands.Context) -> None:
"""Allows Levels to run, check messages, and assign roles."""
current_state = await self.running.get("value", False)
if current_state:
await ctx.reply("Levels is already running.")
return
self._cycle_rules_task.start()
self._calculate_point_thresholds_task.start()
await self.running.set("value", True)
await ctx.reply("Levels is now turned on.")
@levels_command_group.command()
@commands.has_any_role(*ELEVATED_ROLES)
async def stop(self, ctx: commands.Context) -> None:
"""Disallows Levels to run, check messages, and assign roles."""
current_state = await self.running.get("value", False)
if not current_state:
await ctx.reply("Levels is already off.")
return
self._cycle_rules_task.cancel()
self._calculate_point_thresholds_task.cancel()
await self.running.set("value", False)
await ctx.reply("Levels is now turned off.")
@levels_command_group.command()
@commands.has_any_role(*ELEVATED_ROLES)
async def status(self, ctx: commands.Context) -> None:
"""Replies with current status of Levels."""
current_state = await self.running.get("value", False)
if current_state:
await ctx.reply(":white_check_mark: Levels is currently running.")
else:
await ctx.reply(":x: Levels is current **not** running.")
# Please see ./rules/README.md for how to format rules
@dataclass
class RuleTrigger:
interaction_type: Literal["message", "reaction"]
reaction_content: list[str] | None = None
message_content: str | None = None
points: int = 0
@dataclass
class LevelRules:
name: str
rule_triggers: list[RuleTrigger]