Skip to content

Commit 1f054f6

Browse files
luca-montaigutpotb
authored andcommitted
fix: remove cache entries on delete channel (#62)
1 parent f43a0fd commit 1f054f6

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

src/modules/quoiFeur/quoiFeur.helpers.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { ChannelType, type ChatInputCommandInteraction, type Message } from 'discord.js';
1+
import {
2+
ChannelType,
3+
type ChatInputCommandInteraction,
4+
DMChannel,
5+
type Message,
6+
type NonThreadGuildBasedChannel,
7+
} from 'discord.js';
28

39
import { cache } from '../../core/cache';
410
import { removeEmoji, removePunctuation } from '../../helpers/regex.helper';
@@ -70,3 +76,16 @@ export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInt
7076
);
7177
await interaction.reply('Quoi-feur disabled in this channel');
7278
};
79+
80+
export const cleanCacheOnChannelDelete = async (
81+
channel: DMChannel | NonThreadGuildBasedChannel,
82+
) => {
83+
const { id } = channel;
84+
const channels = await cache.get('quoiFeurChannels', []);
85+
if (!channels.includes(id)) return;
86+
87+
await cache.set(
88+
'quoiFeurChannels',
89+
channels.filter((channelId) => channelId !== id),
90+
);
91+
};

src/modules/quoiFeur/quoiFeur.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SlashCommandBuilder } from 'discord.js';
33
import type { BotModule } from '../../types/bot';
44
import {
55
addQuoiFeurToChannel,
6+
cleanCacheOnChannelDelete,
67
reactOnEndWithQuoi,
78
removeQuoiFeurFromChannel,
89
} from './quoiFeur.helpers';
@@ -28,6 +29,7 @@ export const quoiFeur: BotModule = {
2829
],
2930
eventHandlers: {
3031
messageCreate: reactOnEndWithQuoi,
32+
channelDelete: cleanCacheOnChannelDelete,
3133
},
3234
intents: ['Guilds', 'GuildMessages', 'MessageContent', 'GuildMessageReactions'],
3335
};

src/modules/recurringMessage/recurringMessage.helpers.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { CronJob } from 'cron';
22
import { randomUUID } from 'crypto';
3-
import type { ChatInputCommandInteraction, Client } from 'discord.js';
3+
import type {
4+
ChatInputCommandInteraction,
5+
Client,
6+
DMChannel,
7+
NonThreadGuildBasedChannel,
8+
} from 'discord.js';
49

510
import { cache } from '../../core/cache';
611
import { isModo } from '../../helpers/roles';
@@ -134,9 +139,37 @@ export const listRecurringMessages = async (interaction: ChatInputCommandInterac
134139
export const relaunchRecurringMessages = async (client: Client<true>) => {
135140
const recurringMessages = await cache.get('recurringMessages', []);
136141

137-
recurringMessages.forEach(({ id, channelId, frequency, message }) => {
142+
const channelsToClear = new Set<string>();
143+
for (const { id, channelId, frequency, message } of recurringMessages) {
144+
if (!client.channels.cache.get(channelId)) {
145+
channelsToClear.add(channelId);
146+
continue;
147+
}
138148
const job = createRecurringMessage(client, channelId, frequency, message);
139149
job.start();
140150
inMemoryJobList.push({ id, job });
141-
});
151+
}
152+
153+
await cache.set(
154+
'recurringMessages',
155+
recurringMessages.filter(({ channelId }) => !channelsToClear.has(channelId)),
156+
);
157+
};
158+
159+
export const removeAllFromChannel = async (channel: DMChannel | NonThreadGuildBasedChannel) => {
160+
const { id } = channel;
161+
162+
const recurringMessages = await cache.get('recurringMessages', []);
163+
const jobsToRemove = recurringMessages.filter(({ channelId }) => id === channelId);
164+
165+
await cache.set(
166+
'recurringMessages',
167+
recurringMessages.filter(({ channelId }) => id !== channelId),
168+
);
169+
170+
for (const { id } of jobsToRemove) {
171+
const job = inMemoryJobList.find(({ id: jobId }) => id === jobId)?.job;
172+
if (!job) continue;
173+
job.stop();
174+
}
142175
};

src/modules/recurringMessage/recurringMessage.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
hasPermission,
77
listRecurringMessages,
88
relaunchRecurringMessages,
9+
removeAllFromChannel,
910
removeRecurringMessage,
1011
} from './recurringMessage.helpers';
1112

@@ -69,8 +70,8 @@ export const recurringMessage: BotModule = {
6970
},
7071
],
7172
eventHandlers: {
72-
// relaunch recurring messages on bot restart
7373
ready: relaunchRecurringMessages,
74+
channelDelete: removeAllFromChannel,
7475
},
7576
intents: ['Guilds'],
7677
};

0 commit comments

Comments
 (0)