From 13fd3d5812fc2051f9737b3e913e16df4b3ca07e Mon Sep 17 00:00:00 2001 From: mo Date: Tue, 17 Mar 2026 10:32:22 +0100 Subject: [PATCH] update custom channel discord --- .../channels/voice/voice-channels.service.ts | 1 - .../handle-dynamic-voice-channel-creation.ts | 2 +- .../handle-dynamic-voice-channel-deletion.ts | 10 ++++++++- .../channels/voice/voice-channels.service.ts | 2 +- .../dynamic-voice-channels.service.ts | 22 ++++++++++++++++--- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/adapters/discord/src/entities/channels/voice/voice-channels.service.ts b/adapters/discord/src/entities/channels/voice/voice-channels.service.ts index f22d81b..2f200bc 100644 --- a/adapters/discord/src/entities/channels/voice/voice-channels.service.ts +++ b/adapters/discord/src/entities/channels/voice/voice-channels.service.ts @@ -49,7 +49,6 @@ export class VoiceChannelsService }, ], }); - logger.info("DONE"); return clonedChannel; } diff --git a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts index b7d147f..bcf6561 100644 --- a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts +++ b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts @@ -25,7 +25,7 @@ export const handleDynamicVoiceChannelCreation = async ( // New channel is created if user joined one of the specific channels (e.g. "Join to Create") or switched to one of them const newChannel = await dynamicVoiceChannelService.createDynamicVoiceChannel( - member.id, + { name: member.displayName, id: member.id }, channel.id, ); logger.debug(`Channel found for VoiceStateUpdate event: ${channel.name}`); diff --git a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts index f6b6a5e..8bc7f4a 100644 --- a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts +++ b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts @@ -6,14 +6,22 @@ export const handleDynamicVoiceChannelDeletion = async ( oldState: VoiceState, newState: VoiceState, ) => { + if (!(oldState || newState)) return; + + const userSwitchedChannel = oldState.channelId !== newState.channelId; + const userLeftChannel = !oldState.channelId && newState.channelId; + // Check if user left a channel that was created by the bot and delete it if it's empty - if (oldState.channelId && !newState.channelId) { + if (userSwitchedChannel || userLeftChannel) { const channelId = oldState.channelId; + if (!channelId) return; + if (dynamicVoiceChannelService.createdChannelIdsSet.has(channelId)) { const channel = await voiceChannelsService.getVoiceChannelById(channelId); if (!channel) return; + if (channel.members.size === 0) { await dynamicVoiceChannelService.deleteDynamicVoiceChannel(channelId); } diff --git a/core/src/entities/channels/voice/voice-channels.service.ts b/core/src/entities/channels/voice/voice-channels.service.ts index a59a74b..77ff669 100644 --- a/core/src/entities/channels/voice/voice-channels.service.ts +++ b/core/src/entities/channels/voice/voice-channels.service.ts @@ -6,7 +6,7 @@ export interface VoiceChannelsServiceInterface< createVoiceChannel(name: string): Promise; cloneVoiceChannel( - user_id: string, + user: string, channel: C, options?: { name?: string; position?: number }, ): Promise; diff --git a/core/src/features/dynamic-voice-channels/dynamic-voice-channels.service.ts b/core/src/features/dynamic-voice-channels/dynamic-voice-channels.service.ts index 01d79a8..5b9f82b 100644 --- a/core/src/features/dynamic-voice-channels/dynamic-voice-channels.service.ts +++ b/core/src/features/dynamic-voice-channels/dynamic-voice-channels.service.ts @@ -7,12 +7,15 @@ import type { export class DynamicVoiceChannelsService< C extends { name: string; id: string }, + U extends { name: string; id: string }, > { private voiceChannelsService: VoiceChannelsServiceInterface; dynamicVoiceChannels: z.output; validChannelIds = new Set(); dynamicVoiceChannelsMap: z.output; createdChannelIdsSet = new Set(); + private channelNumberCounter = 1; + private usedNumbers = new Set(); constructor( voiceChannelsService: VoiceChannelsServiceInterface, @@ -30,8 +33,18 @@ export class DynamicVoiceChannelsService< ); } + private getNextChannelNumber(): number { + while (this.usedNumbers.has(this.channelNumberCounter)) { + this.channelNumberCounter++; + } + const number = this.channelNumberCounter; + this.usedNumbers.add(number); + this.channelNumberCounter++; + return number; + } + async createDynamicVoiceChannel( - user_id: string, + user: U, channel_id: string, ): Promise { if (!this.validChannelIds.has(channel_id)) return; @@ -42,11 +55,14 @@ export class DynamicVoiceChannelsService< if (!channel) return; + const channelNumber = this.getNextChannelNumber(); + const newChannel = await this.voiceChannelsService.cloneVoiceChannel( - user_id, + user.id, channel, { - name: channel.name, + name: `${channelNumber}`, + position: 99, }, );