Skip to content

Commit 1c96850

Browse files
feat: better queue management in verification service
1 parent cd81d24 commit 1c96850

File tree

3 files changed

+85
-21
lines changed

3 files changed

+85
-21
lines changed

src/main/typescript/automod/VerificationService.ts

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Events } from "@framework/types/ClientEvents";
66
import { fetchMember, fetchUser } from "@framework/utils/entities";
77
import { Colors } from "@main/constants/Colors";
88
import { env } from "@main/env/env";
9+
import VerificationExpiredQueue from "@main/queues/VerificationExpiredQueue";
910
import type ConfigurationManager from "@main/services/ConfigurationManager";
1011
import type ModerationActionService from "@main/services/ModerationActionService";
1112
import { VerificationEntry, VerificationMethod } from "@prisma/client";
@@ -64,6 +65,8 @@ class VerificationService extends Service {
6465
guildId: member.guild.id
6566
}
6667
});
68+
69+
await this.clearVerificationQueues(member.guild.id, member.id);
6770
}
6871

6972
public async startVerification(member: GuildMember, reason?: string) {
@@ -99,9 +102,29 @@ class VerificationService extends Service {
99102
}
100103
});
101104

105+
await this.application
106+
.service("queueService")
107+
.create(VerificationExpiredQueue, {
108+
data: {
109+
guildId: member.guild.id,
110+
memberId: member.id
111+
},
112+
guildId: member.guild.id,
113+
runsAt: entry.expiresAt
114+
})
115+
.schedule();
116+
102117
await this.sendVerificationMessage(member, entry.code).catch(this.application.logger.error);
103118
}
104119

120+
private async clearVerificationQueues(guildId: Snowflake, userId: Snowflake) {
121+
await this.application
122+
.service("queueService")
123+
.bulkCancel(VerificationExpiredQueue, queue => {
124+
return queue.data.guildId === guildId && queue.data.memberId === userId;
125+
});
126+
}
127+
105128
private async sendVerificationMessage(member: GuildMember, code: string) {
106129
const config = this.configFor(member.guild.id);
107130

@@ -139,6 +162,32 @@ class VerificationService extends Service {
139162
});
140163
}
141164

165+
public async onVerificationExpire(guildId: Snowflake, userId: Snowflake) {
166+
const { count } = await this.application.prisma.verificationEntry.deleteMany({
167+
where: {
168+
userId,
169+
guildId
170+
}
171+
});
172+
173+
if (count === 0) {
174+
return;
175+
}
176+
177+
const actions = this.configFor(guildId)?.expired_actions;
178+
const guild = this.application.client.guilds.cache.get(guildId);
179+
180+
if (actions?.length && guild) {
181+
const memberOrUser =
182+
(await fetchMember(guild, userId)) ??
183+
(await fetchUser(this.application.client, userId));
184+
185+
if (memberOrUser) {
186+
await this.moderationActionService.takeActions(guild, memberOrUser, actions);
187+
}
188+
}
189+
}
190+
142191
public async getVerificationEntry(code: string) {
143192
const entry = await this.application.prisma.verificationEntry.findUnique({
144193
where: {
@@ -151,25 +200,8 @@ class VerificationService extends Service {
151200
}
152201

153202
if (entry.expiresAt.getTime() < Date.now()) {
154-
await this.application.prisma.verificationEntry.delete({
155-
where: {
156-
id: entry.id
157-
}
158-
});
159-
160-
const actions = this.configFor(entry.guildId)?.expired_actions;
161-
const guild = this.application.client.guilds.cache.get(entry.guildId);
162-
163-
if (actions?.length && guild) {
164-
const memberOrUser =
165-
(await fetchMember(guild, entry.userId)) ??
166-
(await fetchUser(this.application.client, entry.userId));
167-
168-
if (memberOrUser) {
169-
await this.moderationActionService.takeActions(guild, memberOrUser, actions);
170-
}
171-
}
172-
203+
await this.onVerificationExpire(entry.guildId, entry.userId);
204+
await this.clearVerificationQueues(entry.guildId, entry.userId);
173205
return null;
174206
}
175207

@@ -262,6 +294,7 @@ class VerificationService extends Service {
262294
}
263295
});
264296

297+
await this.clearVerificationQueues(entry.guildId, entry.userId);
265298
await member
266299
.send({
267300
embeds: [
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Queue from "@framework/queues/Queue";
2+
import type { Snowflake } from "discord.js";
3+
4+
type VerificationExpiredQueuePayload = {
5+
memberId: Snowflake;
6+
guildId: Snowflake;
7+
};
8+
9+
class VerificationExpiredQueue extends Queue<VerificationExpiredQueuePayload> {
10+
public static override readonly uniqueName = "verification_expired";
11+
12+
public async execute({ guildId, memberId }: VerificationExpiredQueuePayload) {
13+
const guild = this.application.client.guilds.cache.get(guildId);
14+
15+
if (!guild) {
16+
return;
17+
}
18+
19+
const config =
20+
this.application.service("configManager").config[guildId]?.member_verification;
21+
22+
if (!config?.enabled) {
23+
return;
24+
}
25+
26+
await this.application
27+
.service("verificationService")
28+
.onVerificationExpire(guildId, memberId);
29+
}
30+
}
31+
32+
export default VerificationExpiredQueue;

src/main/typescript/schemas/GuildConfigSchema.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ export const GuildConfigSchema = z.object({
218218
.number()
219219
.describe("Max verification duration (in seconds)")
220220
.int()
221-
.optional(),
222-
max_attempts: z.number().int().optional()
221+
.optional()
223222
})
224223
.optional()
225224
/*

0 commit comments

Comments
 (0)