Skip to content

Commit

Permalink
[FIX] cancel job if deleted & correct reply
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Dec 23, 2023
1 parent 68b8066 commit c9d6207
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
20 changes: 14 additions & 6 deletions src/BaseClient/ClientHelperModules/cache/bot/stickyTimeouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,40 @@ export interface StickyTimeouts {
/**
* Sets a sticky timeout for a given channel ID and job.
* @param channelId - The ID of the channel to set the sticky timeout for.
* @param messageId - The ID of the message to set the sticky timeout for.
* @param job - The job to set the sticky timeout for.
*/
set: (channelId: string, job: Jobs.Job) => void;
set: (channelId: string, messageId: string, job: Jobs.Job) => void;
/**
* Deletes the sticky timeout for a given channel ID.
* @param channelId - The ID of the channel to delete the sticky timeout for.
*/
delete: (channelId: string) => void;
/**
* Finds the sticky timeout for a given message ID.
* @param messageId - The ID of the message to find the sticky timeout for.
*/
find: (messageId: string) => { job: Jobs.Job; messageId: string } | undefined;
/**
* Map of channel IDs to their corresponding jobs with sticky timeouts.
*/
cache: Map<string, Jobs.Job>;
cache: Map<string, { job: Jobs.Job; messageId: string }>;
}

const self: StickyTimeouts = {
set: (channelId, job) => {
set: (channelId, messageId, job) => {
const cached = self.cache.get(channelId);
cached?.cancel();
cached?.job.cancel();

self.cache.set(channelId, job);
self.cache.set(channelId, { messageId, job });
},
delete: (channelId) => {
const cached = self.cache.get(channelId);
cached?.cancel();

cached?.job.cancel();
self.cache.delete(channelId);
},
find: (messageId) => [...self.cache.values()].find((v) => v.messageId === messageId),
cache: new Map(),
};

Expand Down
7 changes: 2 additions & 5 deletions src/Commands/ContextCommands/Message/stick-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import * as ch from '../../../BaseClient/ClientHelper.js';
export default async (cmd: Discord.MessageContextMenuCommandInteraction) => {
if (!cmd.inCachedGuild()) return;

const res = await ch.DataBase.stickymessages.findUnique({
where: { channelid: cmd.channelId },
});

ch.DataBase.stickymessages
const res = await ch.DataBase.stickymessages
.upsert({
where: { channelid: cmd.channelId },
create: {
Expand All @@ -18,6 +14,7 @@ export default async (cmd: Discord.MessageContextMenuCommandInteraction) => {
userid: cmd.user.id,
},
update: {},
select: { lastmsgid: true },
})
.then();

Expand Down
3 changes: 3 additions & 0 deletions src/Events/messageEvents/messageCreate/stickyMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default async (msg: Discord.Message<true>) => {
ch.cache.stickyTimeouts.delete(msg.channelId);
ch.cache.stickyTimeouts.set(
msg.channelId,
stickyMessage.lastmsgid,
Jobs.scheduleJob(new Date(Date.now() + 60000), async () => {
const webhook = await ch.getChannelWebhook(msg.channel);
if (!webhook) ch.error(msg.guild, new Error('Webhook to post Message with not found'));
Expand Down Expand Up @@ -94,6 +95,8 @@ export default async (msg: Discord.Message<true>) => {
},
})
.then();

ch.cache.stickyTimeouts.delete(msg.channelId);
}),
);
};
10 changes: 7 additions & 3 deletions src/Events/messageEvents/messageDelete/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ export default (msg: Discord.Message) => {

ch.DataBase.giveawaycollection.delete({ where: { msgid: msg.id } }).then();
ch.DataBase.giveawaycollection.delete({ where: { msgid: msg.id } }).then();

ch.DataBase.stickymessages
.delete({
where: { lastmsgid: msg.id, channelid: msg.channel.id },
})
.delete({ where: { lastmsgid: msg.id, channelid: msg.channel.id } })
.then();
});
const stickyMsgJob = ch.cache.stickyTimeouts.find(msg.id);
if (stickyMsgJob) {
stickyMsgJob.job.cancel();
ch.cache.stickyTimeouts.delete(msg.channelId);
}

ch.DataBase.suggestionvotes.delete({ where: { msgid: msg.id } }).then();
ch.cache.deleteSuggestions.delete(msg.guildId, msg.id);
Expand Down

0 comments on commit c9d6207

Please sign in to comment.