Skip to content

Commit

Permalink
Add 10-min job to cleanup inactive listening sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
taahamahdi committed Oct 23, 2023
1 parent 9e5bb7a commit f60d05d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/helpers/game_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ export async function cleanupInactiveGameSessions(): Promise<void> {
}
}

/** Cleans up inactive ListeningSessions */
export async function cleanupInactiveListeningSessions(): Promise<void> {
const { listeningSessions } = State;
let inactiveSessions = 0;
const totalSessions = Object.keys(listeningSessions).length;
await Promise.allSettled(
Object.keys(listeningSessions).map(async (guildID) => {
const listeningSession = listeningSessions[guildID];
if (listeningSession.getVoiceMembers().length === 0) {
await listeningSession.endSession("Empty listening session");
inactiveSessions++;
}
})
);

if (inactiveSessions > 0) {
logger.info(
`Ended ${inactiveSessions} inactive listening sessions out of ${totalSessions}`
);
}
}

/**
* @param userId - The user ID
* @returns whether the player has bonus active
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/management_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IPCLogger } from "../logger";
import { chooseRandom, delay, isPrimaryInstance, isWeekend } from "./utils";
import {
cleanupInactiveGameSessions,
cleanupInactiveListeningSessions,
getMatchingGroupNames,
isPowerHour,
} from "./game_utils";
Expand Down Expand Up @@ -452,6 +453,8 @@ export function registerIntervals(clusterID: number): void {
schedule.scheduleJob("*/10 * * * *", () => {
// Cleanup inactive game sessions
cleanupInactiveGameSessions();
// Cleanup inactive listening sessions
cleanupInactiveListeningSessions();
// Change bot's status (song playing, power hour, etc.)
updateBotStatus();
});
Expand Down

0 comments on commit f60d05d

Please sign in to comment.