Skip to content

refactor(handlers): move conditional logic into handler files #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/handlers/handle-interaction-creation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Interaction } from 'discord.js';

import { createLobby } from '../create-lobby';

export const handleInteractionCreation = async (interaction: Interaction): Promise<void> => {
if (
!interaction.isCommand() ||
!interaction.inGuild() ||
!interaction.isChatInputCommand() ||
interaction.commandName !== 'voice-on-demand'
) {
return;
}

if (interaction.options.getSubcommand(true) !== 'create') {
await interaction.reply('Unknown subcommand');
return;
}

await createLobby(interaction);
};
11 changes: 9 additions & 2 deletions src/handlers/handle-voice-channel-deletion.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import type { VoiceChannel } from 'discord.js';
import type { DMChannel, NonThreadGuildBasedChannel } from 'discord.js';
import { ChannelType } from 'discord.js';

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

export const handleVoiceChannelDeletion = async (channel: VoiceChannel): Promise<void> => {
export const handleVoiceChannelDeletion = async (
channel: DMChannel | NonThreadGuildBasedChannel
): Promise<void> => {
if (channel.type !== ChannelType.GuildVoice) {
return;
}

const lobbyId = await cache.get('lobbyId');

const { guild, id } = channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { cache } from '../helpers/cache';

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

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

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

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

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

const { channel } = state;
Expand All @@ -35,3 +35,21 @@ export const handleLeave = async (state: CheckedVoiceState): Promise<void> => {
await cache.set('channels', filtered);
}
};

export const handleVoiceStateUpdate = async (
oldState: VoiceState,
newState: VoiceState
): Promise<void> => {
const lobbyId = await cache.get('lobbyId');
if (lobbyId === undefined) {
return;
}

if (isLeaveState(oldState)) {
await handleLeave(oldState);
}

if (isJoinState(newState)) {
await handleJoin(newState, lobbyId);
}
};
47 changes: 7 additions & 40 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { ChannelType, Client, REST, Routes } from 'discord.js';
import { Client, REST, Routes } from 'discord.js';

import { voiceOnDemandCommand } from './commands';
import { config } from './config';
import { createLobby } from './create-lobby';
import { deleteExistingCommands } from './delete-existing-commands';
import { handleInteractionCreation } from './handlers/handle-interaction-creation';
import { handleVoiceChannelDeletion } from './handlers/handle-voice-channel-deletion';
import {
handleJoin,
handleLeave,
isJoinState,
isLeaveState,
} from './handlers/voice-state-handlers';
import { cache } from './helpers/cache';
import { voiceOnDemandCommand } from './commands';
import { handleVoiceStateUpdate } from './handlers/handle-voice-state-update';

const { discord } = config;

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

client.on('channelDelete', async (channel) => {
if (channel.type === ChannelType.GuildVoice) {
await handleVoiceChannelDeletion(channel);
}
await handleVoiceChannelDeletion(channel);
});

client.on('voiceStateUpdate', async (oldState, newState) => {
const lobbyId = await cache.get('lobbyId');
if (lobbyId === undefined) {
return;
}

if (isLeaveState(oldState)) {
await handleLeave(oldState);
}

if (isJoinState(newState)) {
await handleJoin(newState, lobbyId);
}
await handleVoiceStateUpdate(oldState, newState);
});

client.on('interactionCreate', async (interaction) => {
if (
!interaction.isCommand() ||
!interaction.inGuild() ||
!interaction.isChatInputCommand() ||
interaction.commandName !== 'voice-on-demand'
) {
return;
}

if (interaction.options.getSubcommand(true) !== 'create') {
await interaction.reply('Unknown subcommand');
return;
}

await createLobby(interaction);
await handleInteractionCreation(interaction);
});

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