Skip to content

Commit cfe2416

Browse files
authored
fix: voice lobby now handles multiple guilds (#77)
1 parent 9ec51b3 commit cfe2416

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

src/core/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface Cache<Entries extends Record<string, any>> {
2121
}
2222

2323
interface CacheEntries {
24-
lobbyId: string;
24+
lobbyIds: string[];
2525
channels: string[];
2626
quoiFeurChannels: string[];
2727
recurringMessages: { id: string; channelId: string; frequency: Frequency; message: string }[];

src/modules/voiceOnDemand/voiceOnDemand.helpers.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ export const isJoinState = (newState: VoiceState): newState is CheckedVoiceState
1818
export const isLeaveState = (oldDate: VoiceState): oldDate is CheckedVoiceState =>
1919
oldDate.channel !== null && oldDate.channelId !== null && oldDate.member !== null;
2020

21-
export const handleJoin = async (state: CheckedVoiceState, lobbyId: string): Promise<void> => {
22-
if (state.channelId !== lobbyId) {
23-
return;
24-
}
25-
21+
export const handleJoin = async (state: CheckedVoiceState): Promise<void> => {
2622
const channel = await createUserVoiceChannel(state.channel.parent, state.member);
2723
await state.member.voice.setChannel(channel);
2824
};

src/modules/voiceOnDemand/voiceOnDemand.module.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@ export const voiceOnDemand: BotModule = {
1616
.toJSON(),
1717
handler: {
1818
create: async (interaction): Promise<void> => {
19-
const guild = interaction.guild as Guild;
19+
const { guild } = interaction;
2020

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+
});
2226

23-
if (lobbyId !== undefined && guild.channels.cache.has(lobbyId)) {
24-
guild.channels.cache.delete(lobbyId);
27+
return;
2528
}
29+
const lobbyIds = await cache.get('lobbyIds', []);
30+
const lobbyId = lobbyIds.find((lobbyId) => guild.channels.cache.has(lobbyId));
2631

27-
const channel =
28-
lobbyId === undefined ? null : await guild.channels.fetch(lobbyId).catch(() => null);
29-
30-
if (channel !== null) {
32+
if (lobbyId !== undefined) {
3133
await interaction.reply({
3234
content: 'Voice on demand voice lobby already exists.',
3335
ephemeral: true,
3436
});
35-
3637
return;
3738
}
3839

@@ -41,7 +42,8 @@ export const voiceOnDemand: BotModule = {
4142
type: ChannelType.GuildVoice,
4243
});
4344

44-
await cache.set('lobbyId', id);
45+
//NOTES: this is a potential race condition.
46+
await cache.set('lobbyIds', [...lobbyIds, id]);
4547

4648
await interaction.reply({
4749
content: 'Created voice on demand voice channel.',
@@ -53,7 +55,9 @@ export const voiceOnDemand: BotModule = {
5355
],
5456
eventHandlers: {
5557
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+
5761
if (lobbyId === undefined) {
5862
return;
5963
}
@@ -63,34 +67,35 @@ export const voiceOnDemand: BotModule = {
6367
}
6468

6569
if (isJoinState(newState)) {
66-
await handleJoin(newState, lobbyId);
70+
await handleJoin(newState);
6771
}
6872
},
6973
channelDelete: async (channel) => {
7074
if (channel.type !== ChannelType.GuildVoice) {
7175
return;
7276
}
7377

74-
const lobbyId = await cache.get('lobbyId');
75-
78+
const lobbyIds = await cache.get('lobbyIds', []);
7679
const { guild, id } = channel;
7780

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+
);
9499
},
95100
},
96101
intents: ['GuildVoiceStates', 'GuildMembers'],

0 commit comments

Comments
 (0)