@@ -2,7 +2,12 @@ import { ChannelType, Guild, SlashCommandBuilder } from 'discord.js';
22
33import { cache } from '../../core/cache' ;
44import type { BotModule } from '../../types/bot' ;
5- import { handleJoin , handleLeave , isJoinState , isLeaveState } from './voiceOnDemand.helpers' ;
5+ import {
6+ handleJoinLobby ,
7+ handleLeaveOnDemand ,
8+ isJoinState ,
9+ isLeaveState ,
10+ } from './voiceOnDemand.helpers' ;
611
712export const voiceOnDemand : BotModule = {
813 slashCommands : [
@@ -56,18 +61,21 @@ export const voiceOnDemand: BotModule = {
5661 eventHandlers : {
5762 voiceStateUpdate : async ( oldState , newState ) => {
5863 const lobbyIds = await cache . get ( 'lobbyIds' , [ ] ) ;
59- const lobbyId = lobbyIds . find ( ( lobbyId ) => newState . channelId === lobbyId ) ;
64+ const onDemandChannels = await cache . get ( 'onDemandChannels' , [ ] ) ;
6065
61- if ( lobbyId === undefined ) {
66+ const isLobbyChannel = lobbyIds . includes ( newState . channelId ?? '' ) ;
67+ const isOnDemandChannel = onDemandChannels . includes ( newState . channelId ?? '' ) ;
68+
69+ if ( ! isOnDemandChannel && ! isLobbyChannel ) {
6270 return ;
6371 }
6472
65- if ( isLeaveState ( oldState ) ) {
66- await handleLeave ( oldState ) ;
73+ if ( isOnDemandChannel && isLeaveState ( oldState ) ) {
74+ await handleLeaveOnDemand ( oldState ) ;
6775 }
6876
69- if ( isJoinState ( newState ) ) {
70- await handleJoin ( newState ) ;
77+ if ( isLobbyChannel && isJoinState ( newState ) ) {
78+ await handleJoinLobby ( newState ) ;
7179 }
7280 } ,
7381 channelDelete : async ( channel ) => {
@@ -76,23 +84,26 @@ export const voiceOnDemand: BotModule = {
7684 }
7785
7886 const lobbyIds = await cache . get ( 'lobbyIds' , [ ] ) ;
79- const { guild , id } = channel ;
87+ const onDemandChannels = await cache . get ( 'onDemandChannels' , [ ] ) ;
8088
81- if ( lobbyIds . includes ( id ) ) return ;
89+ const isLobbyChannel = lobbyIds . includes ( channel . id ) ;
90+ const isOnDemandChannel = onDemandChannels . includes ( channel . id ) ;
91+
92+ if ( ! isOnDemandChannel && ! isLobbyChannel ) {
93+ return ;
94+ }
8295
8396 await cache . set (
8497 'lobbyIds' ,
85- lobbyIds . filter ( ( lobbyId ) => lobbyId !== id ) ,
98+ lobbyIds . filter ( ( lobbyId ) => lobbyId !== channel . id ) ,
8699 ) ;
87100
88- const channels = await cache . get ( 'channels' , [ ] ) ;
89-
90101 await Promise . all (
91- channels . map ( async ( id ) => {
92- const channel = await guild . channels . fetch ( id ) . catch ( ( ) => null ) ;
93- if ( channel !== null ) {
94- await guild . channels . delete ( id ) ;
95- guild . channels . cache . delete ( id ) ;
102+ onDemandChannels . map ( async ( id ) => {
103+ const updatedChannel = await channel . guild . channels . fetch ( id ) . catch ( ( ) => null ) ;
104+ if ( updatedChannel !== null ) {
105+ await channel . guild . channels . delete ( id ) ;
106+ channel . guild . channels . cache . delete ( id ) ;
96107 }
97108 } ) ,
98109 ) ;
0 commit comments