Skip to content

Commit 50af289

Browse files
neolectronpotb
authored andcommitted
fix: remove audio channel when last member leaves (#79)
1 parent aac52ff commit 50af289

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

src/core/cache.ts

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

2323
interface CacheEntries {
2424
lobbyIds: string[];
25-
channels: string[];
25+
onDemandChannels: string[];
2626
quoiFeurChannels: string[];
2727
recurringMessages: { id: string; channelId: string; frequency: Frequency; message: string }[];
2828
}

src/modules/voiceOnDemand/voiceOnDemand.helpers.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ type CheckedVoiceState = SetNonNullable<VoiceState, 'channel' | 'channelId' | 'm
1515
export const isJoinState = (newState: VoiceState): newState is CheckedVoiceState =>
1616
newState.channel !== null && newState.channelId !== null && newState.member !== null;
1717

18-
export const isLeaveState = (oldDate: VoiceState): oldDate is CheckedVoiceState =>
19-
oldDate.channel !== null && oldDate.channelId !== null && oldDate.member !== null;
18+
export const isLeaveState = (oldState: VoiceState): oldState is CheckedVoiceState =>
19+
oldState.channel !== null && oldState.channelId !== null && oldState.member !== null;
2020

21-
export const handleJoin = async (state: CheckedVoiceState): Promise<void> => {
21+
export const handleJoinLobby = async (state: CheckedVoiceState): Promise<void> => {
2222
const channel = await createUserVoiceChannel(state.channel.parent, state.member);
2323
await state.member.voice.setChannel(channel);
2424
};
2525

26-
export const handleLeave = async (state: CheckedVoiceState): Promise<void> => {
27-
const channels = await cache.get('channels', []);
26+
export const handleLeaveOnDemand = async (state: CheckedVoiceState): Promise<void> => {
27+
const channels = await cache.get('onDemandChannels', []);
2828

2929
const { channel } = state;
3030
const { id, members, guild } = channel;
@@ -34,7 +34,7 @@ export const handleLeave = async (state: CheckedVoiceState): Promise<void> => {
3434
guild.channels.cache.delete(id);
3535

3636
const filtered = channels.filter((channelId) => channelId !== id);
37-
await cache.set('channels', filtered);
37+
await cache.set('onDemandChannels', filtered);
3838
}
3939
};
4040

@@ -56,9 +56,9 @@ export const createUserVoiceChannel = async (
5656

5757
const channel = await guild.channels.create(parent === null ? options : { ...options, parent });
5858

59-
const channels = await cache.get('channels', []);
59+
const channels = await cache.get('onDemandChannels', []);
6060

61-
await cache.set('channels', [...channels, channel.id]);
61+
await cache.set('onDemandChannels', [...channels, channel.id]);
6262

6363
return channel.id;
6464
};

src/modules/voiceOnDemand/voiceOnDemand.module.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { ChannelType, Guild, SlashCommandBuilder } from 'discord.js';
22

33
import { cache } from '../../core/cache';
44
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';
611

712
export 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

Comments
 (0)