@@ -16,23 +16,24 @@ export const voiceOnDemand: BotModule = {
16
16
. toJSON ( ) ,
17
17
handler : {
18
18
create : async ( interaction ) : Promise < void > => {
19
- const guild = interaction . guild as Guild ;
19
+ const { guild } = interaction ;
20
20
21
- const lobbyId = await cache . get ( 'lobbyId' ) ;
21
+ if ( ! ( guild instanceof Guild ) ) {
22
+ await interaction . reply ( {
23
+ content : 'This command is only available in guilds' ,
24
+ ephemeral : true ,
25
+ } ) ;
22
26
23
- if ( lobbyId !== undefined && guild . channels . cache . has ( lobbyId ) ) {
24
- guild . channels . cache . delete ( lobbyId ) ;
27
+ return ;
25
28
}
29
+ const lobbyIds = await cache . get ( 'lobbyIds' , [ ] ) ;
30
+ const lobbyId = lobbyIds . find ( ( lobbyId ) => guild . channels . cache . has ( lobbyId ) ) ;
26
31
27
- const channel =
28
- lobbyId === undefined ? null : await guild . channels . fetch ( lobbyId ) . catch ( ( ) => null ) ;
29
-
30
- if ( channel !== null ) {
32
+ if ( lobbyId !== undefined ) {
31
33
await interaction . reply ( {
32
34
content : 'Voice on demand voice lobby already exists.' ,
33
35
ephemeral : true ,
34
36
} ) ;
35
-
36
37
return ;
37
38
}
38
39
@@ -41,7 +42,8 @@ export const voiceOnDemand: BotModule = {
41
42
type : ChannelType . GuildVoice ,
42
43
} ) ;
43
44
44
- await cache . set ( 'lobbyId' , id ) ;
45
+ //NOTES: this is a potential race condition.
46
+ await cache . set ( 'lobbyIds' , [ ...lobbyIds , id ] ) ;
45
47
46
48
await interaction . reply ( {
47
49
content : 'Created voice on demand voice channel.' ,
@@ -53,7 +55,9 @@ export const voiceOnDemand: BotModule = {
53
55
] ,
54
56
eventHandlers : {
55
57
voiceStateUpdate : async ( oldState , newState ) => {
56
- const lobbyId = await cache . get ( 'lobbyId' ) ;
58
+ const lobbyIds = await cache . get ( 'lobbyIds' , [ ] ) ;
59
+ const lobbyId = lobbyIds . find ( ( lobbyId ) => newState . channelId === lobbyId ) ;
60
+
57
61
if ( lobbyId === undefined ) {
58
62
return ;
59
63
}
@@ -63,34 +67,35 @@ export const voiceOnDemand: BotModule = {
63
67
}
64
68
65
69
if ( isJoinState ( newState ) ) {
66
- await handleJoin ( newState , lobbyId ) ;
70
+ await handleJoin ( newState ) ;
67
71
}
68
72
} ,
69
73
channelDelete : async ( channel ) => {
70
74
if ( channel . type !== ChannelType . GuildVoice ) {
71
75
return ;
72
76
}
73
77
74
- const lobbyId = await cache . get ( 'lobbyId' ) ;
75
-
78
+ const lobbyIds = await cache . get ( 'lobbyIds' , [ ] ) ;
76
79
const { guild, id } = channel ;
77
80
78
- if ( id === lobbyId ) {
79
- await cache . delete ( 'lobbyId' ) ;
80
- guild . channels . cache . delete ( lobbyId ) ;
81
-
82
- const channels = await cache . get ( 'channels' , [ ] ) ;
83
-
84
- await Promise . all (
85
- channels . map ( async ( id ) => {
86
- const channel = await guild . channels . fetch ( id ) . catch ( ( ) => null ) ;
87
- if ( channel !== null ) {
88
- await guild . channels . delete ( id ) ;
89
- guild . channels . cache . delete ( id ) ;
90
- }
91
- } ) ,
92
- ) ;
93
- }
81
+ if ( lobbyIds . includes ( id ) ) return ;
82
+
83
+ await cache . set (
84
+ 'lobbyIds' ,
85
+ lobbyIds . filter ( ( lobbyId ) => lobbyId !== id ) ,
86
+ ) ;
87
+
88
+ const channels = await cache . get ( 'channels' , [ ] ) ;
89
+
90
+ 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 ) ;
96
+ }
97
+ } ) ,
98
+ ) ;
94
99
} ,
95
100
} ,
96
101
intents : [ 'GuildVoiceStates' , 'GuildMembers' ] ,
0 commit comments