Skip to content
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
25 changes: 24 additions & 1 deletion packages/backend/src/adapters/services/DiscordVoiceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@ import type { PlayedSoundsRepository } from "@adapters/repositories/PlayedSounds
import type { SoundRepository } from "@adapters/repositories/SoundRepository.js";
import { createPlayingSound } from "@core/entities/PlayingSound.js";
import type { SoundService } from "@core/services/SoundService.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import type { PlayedSoundSource } from "@db/types.js";
import { AudioPlayerStatus, type VoiceConnection, VoiceConnectionStatus, joinVoiceChannel } from "@discordjs/voice";
import type { Guild, VoiceChannel } from "discord.js";

export type VoiceService = {
readonly connect: (guild: Guild, channelId: string) => Promise<void>;
readonly disconnect: (serverId: string) => Promise<void>;
readonly isConnected: (serverId: string) => boolean;
readonly getVoiceChannelId: (serverId: string) => string | null;
readonly playAudio: (serverId: string, nameOrSource: string, userId: string, playedSoundSource: PlayedSoundSource, volume?: number) => Promise<string | null>;
readonly stopAudio: (serverId: string) => Promise<void>;
readonly stopAudioById: (serverId: string, audioId: string) => Promise<boolean>;
readonly stopAllAudio: (serverId: string) => Promise<void>;
readonly isPlaying: (serverId: string) => boolean;
readonly getActiveAudioCount: (serverId: string) => number;
readonly getActiveAudioIds: (serverId: string) => string[];
readonly getVolume: (serverId: string) => number;
readonly setVolume: (serverId: string, volume: number) => Promise<void>;
readonly pause: (serverId: string) => Promise<void>;
readonly resume: (serverId: string) => Promise<void>;
};

export type DiscordVoiceServiceDeps = {
readonly soundRepository: SoundRepository;
readonly playedSoundsRepository: PlayedSoundsRepository;
Expand Down Expand Up @@ -143,6 +160,11 @@ export const createDiscordVoiceService = ({ soundRepository, playedSoundsReposit
return connected;
};

const getVoiceChannelId = (serverId: string): string | null => {
const state = voiceStates.get(serverId);
return state?.connection.joinConfig.channelId ?? null;
};

const playAudio = async (serverId: string, nameOrSource: string, userId: string, playedSoundSource: PlayedSoundSource, volume?: number): Promise<string | null> => {
console.log(`[DiscordVoiceService] Playing audio: ${nameOrSource} in server ${serverId}`);

Expand Down Expand Up @@ -295,6 +317,7 @@ export const createDiscordVoiceService = ({ soundRepository, playedSoundsReposit
connect,
disconnect,
isConnected,
getVoiceChannelId,
playAudio,
stopAudio,
stopAudioById,
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/application/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PlayedSoundsRepository } from "@adapters/repositories/PlayedSounds
import type { ReactionRepository } from "@adapters/repositories/ReactionRepository.js";
import type { SoundRepository } from "@adapters/repositories/SoundRepository.js";
import type { VoiceEventSoundsRepository } from "@adapters/repositories/VoiceEventSoundsRepository.js";
import type { VoiceService } from "@adapters/services/DiscordVoiceService.js";
import { createBannedFeaturesCommands } from "@application/commands/BannedFeaturesCommands.js";
import { createPlayedSoundsCommands } from "@application/commands/PlayedSoundsCommands.js";
import { createReactionCommands } from "@application/commands/ReactionCommands.js";
Expand All @@ -11,7 +12,6 @@ import type { CommandChoicesService } from "@core/services/CommandChoicesService
import type { DiscordChatService } from "@core/services/DiscordChatService.js";
import type { SoundService } from "@core/services/SoundService.js";
import type { SoundTagService } from "@core/services/SoundTagService.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import { type ApplicationCommandOptionChoiceData, type AutocompleteFocusedOption, ChatInputCommandInteraction, Events, MessageFlags, REST, Routes, type SlashCommandOptionsOnlyBuilder } from "discord.js";

import type { DiscordBot } from "@/infrastructure/discord/DiscordBot.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/application/commands/VoiceCommands.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { BannedFeaturesRepository } from "@adapters/repositories/BannedFeaturesRepository.js";
import type { VoiceService } from "@adapters/services/DiscordVoiceService.js";
import { parseAudioSource } from "@core/services/AudioFetcherService.js";
import type { CommandChoicesService } from "@core/services/CommandChoicesService.js";
import type { SoundService } from "@core/services/SoundService.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import { randomArrayItem, randomInt } from "@core/utils/probabilityUtils.js";
import { parseTimeSpan } from "@core/utils/timeUtils.js";
import { ChannelType, ChatInputCommandInteraction, GuildMember, MessageFlags, SlashCommandBuilder } from "discord.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { VoiceService } from "@adapters/services/DiscordVoiceService.js";
import type { Config } from "@core/config/Config.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import type { DiscordBot } from "@infrastructure/discord/DiscordBot.js";
import { Events, VoiceChannel, VoiceState } from "discord.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BannedFeaturesRepository } from "@adapters/repositories/BannedFeaturesRepository.js";
import type { SoundRepository } from "@adapters/repositories/SoundRepository.js";
import type { VoiceService } from "@adapters/services/DiscordVoiceService.js";
import type { Config } from "@core/config/Config.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import { ChannelType, type Guild, type Message, MessageFlags, type TextChannel, ThreadAutoArchiveDuration, type ThreadChannel } from "discord.js";

export type SoundboardThreadService = {
Expand Down
13 changes: 9 additions & 4 deletions packages/backend/src/core/services/VoiceEventSoundsService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { VoiceEventSoundsRepository } from "@adapters/repositories/VoiceEventSoundsRepository.js";
import type { VoiceService } from "@adapters/services/DiscordVoiceService.js";
import type { Config } from "@core/config/Config.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import { randomArrayItem } from "@core/utils/probabilityUtils.js";
import { sleep } from "@core/utils/timeUtils.js";
import type { VoiceEventSoundType } from "@db/types.js";
Expand Down Expand Up @@ -37,14 +37,19 @@ export const createVoiceEventSoundsService = ({ config, voiceEventSoundsReposito

const guild = newState.guild;
const userId = newState.member!.id;
const availableSounds = await voiceEventSoundsRepository.getVoiceEventSounds({ userId, type });
const sound = randomArrayItem(availableSounds);
if (!sound) return;

if (!voiceService.isConnected(guild.id)) {
await voiceService.connect(guild, config.discord.defaultVoiceChannelId);
}

const userChannelId = type === "UserJoin" ? newState.channelId : oldState.channelId;
const botChannelId = voiceService.getVoiceChannelId(guild.id) ?? config.discord.defaultVoiceChannelId;
if (userChannelId !== botChannelId) return;

const availableSounds = await voiceEventSoundsRepository.getVoiceEventSounds({ userId, type });
const sound = randomArrayItem(availableSounds);
if (!sound) return;

const delay = soundDelays[type];
if (delay) {
await sleep(delay);
Expand Down
19 changes: 0 additions & 19 deletions packages/backend/src/core/services/VoiceService.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/backend/src/infrastructure/discord/DiscordBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ReactionEmoteRepository } from "@adapters/repositories/ReactionEmo
import type { ReactionRepository } from "@adapters/repositories/ReactionRepository.js";
import type { SoundRepository } from "@adapters/repositories/SoundRepository.js";
import type { VoiceEventSoundsRepository } from "@adapters/repositories/VoiceEventSoundsRepository.js";
import type { VoiceService } from "@adapters/services/DiscordVoiceService.js";
import { deployCommands, registerCommands } from "@application/commands/Commands.js";
import { registerAutoReactionEvents } from "@application/eventHandlers/AutoReaction.js";
import { registerDiscordUserSyncEvents } from "@application/eventHandlers/DiscordUserSyncService.js";
Expand All @@ -26,7 +27,6 @@ import type { SoundService } from "@core/services/SoundService.js";
import type { SoundTagService } from "@core/services/SoundTagService.js";
import type { SoundboardThreadService } from "@core/services/SoundboardThreadService.js";
import type { VoiceEventSoundsService } from "@core/services/VoiceEventSoundsService.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import { sleep } from "@core/utils/timeUtils.js";
import type { GeminiLlmService } from "@infrastructure/services/GeminiLlmService.js";
import { Client, type ClientEvents, Events, GatewayIntentBits, Partials, PresenceUpdateStatus, RESTEvents, type TextChannel } from "discord.js";
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/tests/utils/createMinimalTestBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createReactionRepository } from "@adapters/repositories/ReactionReposit
import type { SoundRepository } from "@adapters/repositories/SoundRepository.js";
import { createUserRepository } from "@adapters/repositories/UserRepository.js";
import type { VoiceEventSoundsRepository } from "@adapters/repositories/VoiceEventSoundsRepository.js";
import type { VoiceService } from "@adapters/services/DiscordVoiceService.js";
import type { Config } from "@core/config/Config.js";
import type { AutoReactionService } from "@core/services/AutoReactionService.js";
import { createAutoReactionService } from "@core/services/AutoReactionService.js";
Expand All @@ -24,7 +25,6 @@ import type { SoundService } from "@core/services/SoundService.js";
import type { SoundTagService } from "@core/services/SoundTagService.js";
import type { SoundboardThreadService } from "@core/services/SoundboardThreadService.js";
import type { VoiceEventSoundsService } from "@core/services/VoiceEventSoundsService.js";
import type { VoiceService } from "@core/services/VoiceService.js";
import { runMigrations } from "@db/migrations.js";
import type { DB } from "@db/types.js";
import { createDatabaseConnection } from "@infrastructure/database/DatabaseConnection.js";
Expand Down Expand Up @@ -169,6 +169,7 @@ export async function createMinimalTestBot(config: Config, schemaName: string, o
connect: async () => {},
disconnect: async () => {},
isConnected: () => false,
getVoiceChannelId: () => null,
playAudio: async () => "",
stopAudio: async () => {},
stopAudioById: async () => false,
Expand Down
Loading