Skip to content

Commit 05f0826

Browse files
authored
refactor(handlers): move conditional logic into handler files (#4)
1 parent d659f6f commit 05f0826

File tree

4 files changed

+59
-46
lines changed

4 files changed

+59
-46
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Interaction } from 'discord.js';
2+
3+
import { createLobby } from '../create-lobby';
4+
5+
export const handleInteractionCreation = async (interaction: Interaction): Promise<void> => {
6+
if (
7+
!interaction.isCommand() ||
8+
!interaction.inGuild() ||
9+
!interaction.isChatInputCommand() ||
10+
interaction.commandName !== 'voice-on-demand'
11+
) {
12+
return;
13+
}
14+
15+
if (interaction.options.getSubcommand(true) !== 'create') {
16+
await interaction.reply('Unknown subcommand');
17+
return;
18+
}
19+
20+
await createLobby(interaction);
21+
};

src/handlers/handle-voice-channel-deletion.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import type { VoiceChannel } from 'discord.js';
1+
import type { DMChannel, NonThreadGuildBasedChannel } from 'discord.js';
2+
import { ChannelType } from 'discord.js';
23

34
import { cache } from '../helpers/cache';
45

5-
export const handleVoiceChannelDeletion = async (channel: VoiceChannel): Promise<void> => {
6+
export const handleVoiceChannelDeletion = async (
7+
channel: DMChannel | NonThreadGuildBasedChannel
8+
): Promise<void> => {
9+
if (channel.type !== ChannelType.GuildVoice) {
10+
return;
11+
}
12+
613
const lobbyId = await cache.get('lobbyId');
714

815
const { guild, id } = channel;

src/handlers/voice-state-handlers.ts renamed to src/handlers/handle-voice-state-update.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { cache } from '../helpers/cache';
66

77
type CheckedVoiceState = SetNonNullable<VoiceState, 'channel' | 'channelId' | 'member'>;
88

9-
export const isJoinState = (newState: VoiceState): newState is CheckedVoiceState =>
9+
const isJoinState = (newState: VoiceState): newState is CheckedVoiceState =>
1010
newState.channel !== null && newState.channelId !== null && newState.member !== null;
1111

12-
export const isLeaveState = (oldDate: VoiceState): oldDate is CheckedVoiceState =>
12+
const isLeaveState = (oldDate: VoiceState): oldDate is CheckedVoiceState =>
1313
oldDate.channel !== null && oldDate.channelId !== null && oldDate.member !== null;
1414

15-
export const handleJoin = async (state: CheckedVoiceState, lobbyId: string): Promise<void> => {
15+
const handleJoin = async (state: CheckedVoiceState, lobbyId: string): Promise<void> => {
1616
if (state.channelId !== lobbyId) {
1717
return;
1818
}
@@ -21,7 +21,7 @@ export const handleJoin = async (state: CheckedVoiceState, lobbyId: string): Pro
2121
await state.member.voice.setChannel(channel);
2222
};
2323

24-
export const handleLeave = async (state: CheckedVoiceState): Promise<void> => {
24+
const handleLeave = async (state: CheckedVoiceState): Promise<void> => {
2525
const channels = await cache.get('channels', []);
2626

2727
const { channel } = state;
@@ -35,3 +35,21 @@ export const handleLeave = async (state: CheckedVoiceState): Promise<void> => {
3535
await cache.set('channels', filtered);
3636
}
3737
};
38+
39+
export const handleVoiceStateUpdate = async (
40+
oldState: VoiceState,
41+
newState: VoiceState
42+
): Promise<void> => {
43+
const lobbyId = await cache.get('lobbyId');
44+
if (lobbyId === undefined) {
45+
return;
46+
}
47+
48+
if (isLeaveState(oldState)) {
49+
await handleLeave(oldState);
50+
}
51+
52+
if (isJoinState(newState)) {
53+
await handleJoin(newState, lobbyId);
54+
}
55+
};

src/main.ts

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import { ChannelType, Client, REST, Routes } from 'discord.js';
1+
import { Client, REST, Routes } from 'discord.js';
22

3+
import { voiceOnDemandCommand } from './commands';
34
import { config } from './config';
4-
import { createLobby } from './create-lobby';
55
import { deleteExistingCommands } from './delete-existing-commands';
6+
import { handleInteractionCreation } from './handlers/handle-interaction-creation';
67
import { handleVoiceChannelDeletion } from './handlers/handle-voice-channel-deletion';
7-
import {
8-
handleJoin,
9-
handleLeave,
10-
isJoinState,
11-
isLeaveState,
12-
} from './handlers/voice-state-handlers';
13-
import { cache } from './helpers/cache';
14-
import { voiceOnDemandCommand } from './commands';
8+
import { handleVoiceStateUpdate } from './handlers/handle-voice-state-update';
159

1610
const { discord } = config;
1711

@@ -36,42 +30,15 @@ const client = new Client({
3630
await bootstrap(client);
3731

3832
client.on('channelDelete', async (channel) => {
39-
if (channel.type === ChannelType.GuildVoice) {
40-
await handleVoiceChannelDeletion(channel);
41-
}
33+
await handleVoiceChannelDeletion(channel);
4234
});
4335

4436
client.on('voiceStateUpdate', async (oldState, newState) => {
45-
const lobbyId = await cache.get('lobbyId');
46-
if (lobbyId === undefined) {
47-
return;
48-
}
49-
50-
if (isLeaveState(oldState)) {
51-
await handleLeave(oldState);
52-
}
53-
54-
if (isJoinState(newState)) {
55-
await handleJoin(newState, lobbyId);
56-
}
37+
await handleVoiceStateUpdate(oldState, newState);
5738
});
5839

5940
client.on('interactionCreate', async (interaction) => {
60-
if (
61-
!interaction.isCommand() ||
62-
!interaction.inGuild() ||
63-
!interaction.isChatInputCommand() ||
64-
interaction.commandName !== 'voice-on-demand'
65-
) {
66-
return;
67-
}
68-
69-
if (interaction.options.getSubcommand(true) !== 'create') {
70-
await interaction.reply('Unknown subcommand');
71-
return;
72-
}
73-
74-
await createLobby(interaction);
41+
await handleInteractionCreation(interaction);
7542
});
7643

7744
const rest = new REST({ version: '10' }).setToken(discord.token);

0 commit comments

Comments
 (0)