fix(fishing): clear stale sessions for deleted threads #10

Merged
myong merged 1 commits from myong_dev into main 2026-04-09 04:49:53 +00:00
1 changed files with 35 additions and 2 deletions

View File

@ -148,7 +148,7 @@ export class FishingService {
}
const userKey = this.getUserKey(interaction.guildId, interaction.user.id);
const existing = this.sessionsByUser.get(userKey);
const existing = await this.getActiveSession(userKey);
if (existing) {
return { thread: existing.thread, existed: true };
}
@ -170,7 +170,7 @@ export class FishingService {
return false;
}
const session = this.sessionsByUser.get(this.getUserKey(interaction.guildId, interaction.user.id));
const session = await this.getActiveSession(this.getUserKey(interaction.guildId, interaction.user.id));
if (!session) {
const thread = await this.findOwnedFishingThread(interaction);
if (!thread) {
@ -394,6 +394,39 @@ export class FishingService {
}
}
private static async getActiveSession(userKey: string) {
const session = this.sessionsByUser.get(userKey);
if (!session) {
return null;
}
if (await this.isSessionStale(session)) {
this.clearStaleSession(session);
return null;
}
return session;
}
private static async isSessionStale(session: FishingSession) {
try {
await session.thread.fetch();
await session.controlMessage.fetch();
return false;
} catch (error) {
logger.info(
`[Fishing] Clearing stale session for ${session.userId} in thread ${session.threadId}: ${error instanceof Error ? error.message : String(error)}`,
);
return true;
}
}
private static clearStaleSession(session: FishingSession) {
this.clearTick(session);
this.sessionsByUser.delete(this.getUserKey(session.guildId, session.userId));
this.sessionsByThread.delete(session.threadId);
}
private static async deleteThread(thread: ThreadChannel) {
try {
await thread.delete();