From f60d05d061d891a94b3e6010a7372c912938a776 Mon Sep 17 00:00:00 2001 From: Taaha Mahdi Date: Mon, 23 Oct 2023 19:56:36 -0400 Subject: [PATCH] Add 10-min job to cleanup inactive listening sessions --- src/helpers/game_utils.ts | 22 ++++++++++++++++++++++ src/helpers/management_utils.ts | 3 +++ 2 files changed, 25 insertions(+) diff --git a/src/helpers/game_utils.ts b/src/helpers/game_utils.ts index 4ab1a7bdf..789f0188d 100644 --- a/src/helpers/game_utils.ts +++ b/src/helpers/game_utils.ts @@ -149,6 +149,28 @@ export async function cleanupInactiveGameSessions(): Promise { } } +/** Cleans up inactive ListeningSessions */ +export async function cleanupInactiveListeningSessions(): Promise { + 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 diff --git a/src/helpers/management_utils.ts b/src/helpers/management_utils.ts index 57cb32bea..2e48dee32 100644 --- a/src/helpers/management_utils.ts +++ b/src/helpers/management_utils.ts @@ -4,6 +4,7 @@ import { IPCLogger } from "../logger"; import { chooseRandom, delay, isPrimaryInstance, isWeekend } from "./utils"; import { cleanupInactiveGameSessions, + cleanupInactiveListeningSessions, getMatchingGroupNames, isPowerHour, } from "./game_utils"; @@ -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(); });