Skip to content

Commit 824aa0b

Browse files
authored
fix(on_message): ensure slow mode is unset after server restart (#60)
* fix(slow_mode): handle server restarts - Check busy level if channel's current rate limit does not match server state * fix(slow_mode): adjust server state tracking - Track slow mode state per unique channel - Refactor related variables to snake_case * chore: nothing to see here
1 parent 2215244 commit 824aa0b

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/events/on_message/_slow_mode.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function debug(args: any | []) {
1313
}
1414

1515
type QueueItem = { userId: string; timestamp: number };
16-
const messageQueueChannelMap = new Map<string, QueueItem[]>([]);
16+
const message_queue_channel_map = new Map<string, QueueItem[]>([]);
1717

1818
const ONE_MINUTE = 60_000;
1919
const FIVE_MINUTES = 300_000;
@@ -90,7 +90,15 @@ type SlowModeActivation = {
9090
expiry: number;
9191
};
9292

93-
let currentSlowMode: SlowModeActivation | undefined;
93+
/** Track slow mode state per channel */
94+
const slow_mode_state_channel_map = new Map<string, SlowModeActivation>();
95+
96+
/** Look up rate limit per user based on level. */
97+
function level_to_rate_limit(level: BusyLevels | -1): number {
98+
return (
99+
levelConfigs.find((el) => el.level === level)?.rateLimitPerUser ?? -1
100+
);
101+
}
94102

95103
export default async function slow_mode(message: Message): Promise<void> {
96104
if (
@@ -105,44 +113,56 @@ export default async function slow_mode(message: Message): Promise<void> {
105113
const now = Date.now();
106114
const { channelId } = message;
107115

108-
if (!Array.isArray(messageQueueChannelMap.get(channelId))) {
116+
if (!Array.isArray(message_queue_channel_map.get(channelId))) {
109117
// instantiate array, mapped against channel id
110-
messageQueueChannelMap.set(channelId, []);
118+
message_queue_channel_map.set(channelId, []);
111119
}
112120

113-
const channelMessageQueue = messageQueueChannelMap.get(channelId)!;
121+
const channel_message_queue = message_queue_channel_map.get(channelId)!;
114122
debug([
115123
'queue on message',
116124
{
117-
channelMessageQueue,
118-
messageQueueChannelMap,
125+
channelMessageQueue: channel_message_queue,
126+
messageQueueChannelMap: message_queue_channel_map,
119127
},
120128
]);
121129

122-
channelMessageQueue.push({ userId: message.author.id, timestamp: now });
130+
channel_message_queue.push({ userId: message.author.id, timestamp: now });
123131

124132
// Remove old messages from queue
125-
while (channelMessageQueue[0].timestamp < now - QUEUE_TIME_RANGE) {
126-
channelMessageQueue.shift();
133+
while (channel_message_queue[0].timestamp < now - QUEUE_TIME_RANGE) {
134+
channel_message_queue.shift();
127135
}
128136

129-
debug(['queue on purge', channelMessageQueue]);
137+
debug(['queue on purge', channel_message_queue]);
130138
const { level: newLevel, messagesUntilNextLevel } = checkBusyLevel(
131139
now,
132-
channelMessageQueue,
140+
channel_message_queue,
133141
);
134142

143+
// Secret joke
144+
// TODO: refactor into its own hook
145+
if (message.content.includes('the bot is here')) {
146+
await message.react('👀');
147+
}
148+
135149
// Return early if:
136150
//
137151
// - new level is equal to current level
138152
// OR
139153
// - new level is lower or equal to the current level AND current rate limit
140154
// hasn't expired
155+
// BUT ALSO
156+
// - channel has slow mode already activated
157+
const currentSlowMode = slow_mode_state_channel_map.get(channelId);
141158
const currentModeExpired = now > (currentSlowMode?.expiry ?? 0);
142159
const currentLevel = currentSlowMode?.level ?? -1;
160+
const hasStateMismatch =
161+
level_to_rate_limit(currentLevel) !== message.channel.rateLimitPerUser;
143162
if (
144-
newLevel === currentLevel ||
145-
(newLevel <= currentLevel && !currentModeExpired)
163+
!hasStateMismatch &&
164+
(newLevel === currentLevel ||
165+
(newLevel <= currentLevel && !currentModeExpired))
146166
) {
147167
debug([
148168
'early return due to new level not exceeding current, or current rate limit has not expired',
@@ -190,11 +210,11 @@ export default async function slow_mode(message: Message): Promise<void> {
190210
await message.channel.send(targetConfig.channelMessage);
191211

192212
const setTime = Date.now();
193-
currentSlowMode = {
213+
slow_mode_state_channel_map.set(channelId, {
194214
level: targetConfig.level,
195215
timestamp: setTime,
196216
expiry: setTime + targetConfig.timeout,
197-
};
217+
});
198218
}
199219

200220
/** Get number of unique users in message queue */

0 commit comments

Comments
 (0)