@@ -2,7 +2,12 @@ import { ChannelType, Guild, SlashCommandBuilder } from 'discord.js';
2
2
3
3
import { cache } from '../../core/cache' ;
4
4
import 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' ;
6
11
7
12
export const voiceOnDemand : BotModule = {
8
13
slashCommands : [
@@ -56,18 +61,21 @@ export const voiceOnDemand: BotModule = {
56
61
eventHandlers : {
57
62
voiceStateUpdate : async ( oldState , newState ) => {
58
63
const lobbyIds = await cache . get ( 'lobbyIds' , [ ] ) ;
59
- const lobbyId = lobbyIds . find ( ( lobbyId ) => newState . channelId === lobbyId ) ;
64
+ const onDemandChannels = await cache . get ( 'onDemandChannels' , [ ] ) ;
60
65
61
- if ( lobbyId === undefined ) {
66
+ const isLobbyChannel = lobbyIds . includes ( newState . channelId ?? '' ) ;
67
+ const isOnDemandChannel = onDemandChannels . includes ( newState . channelId ?? '' ) ;
68
+
69
+ if ( ! isOnDemandChannel && ! isLobbyChannel ) {
62
70
return ;
63
71
}
64
72
65
- if ( isLeaveState ( oldState ) ) {
66
- await handleLeave ( oldState ) ;
73
+ if ( isOnDemandChannel && isLeaveState ( oldState ) ) {
74
+ await handleLeaveOnDemand ( oldState ) ;
67
75
}
68
76
69
- if ( isJoinState ( newState ) ) {
70
- await handleJoin ( newState ) ;
77
+ if ( isLobbyChannel && isJoinState ( newState ) ) {
78
+ await handleJoinLobby ( newState ) ;
71
79
}
72
80
} ,
73
81
channelDelete : async ( channel ) => {
@@ -76,23 +84,26 @@ export const voiceOnDemand: BotModule = {
76
84
}
77
85
78
86
const lobbyIds = await cache . get ( 'lobbyIds' , [ ] ) ;
79
- const { guild , id } = channel ;
87
+ const onDemandChannels = await cache . get ( 'onDemandChannels' , [ ] ) ;
80
88
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
+ }
82
95
83
96
await cache . set (
84
97
'lobbyIds' ,
85
- lobbyIds . filter ( ( lobbyId ) => lobbyId !== id ) ,
98
+ lobbyIds . filter ( ( lobbyId ) => lobbyId !== channel . id ) ,
86
99
) ;
87
100
88
- const channels = await cache . get ( 'channels' , [ ] ) ;
89
-
90
101
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 ) ;
96
107
}
97
108
} ) ,
98
109
) ;
0 commit comments