update custom channel discord

This commit is contained in:
mo
2026-03-17 10:32:22 +01:00
parent e4425b23be
commit 13fd3d5812
5 changed files with 30 additions and 7 deletions

View File

@@ -49,7 +49,6 @@ export class VoiceChannelsService
}, },
], ],
}); });
logger.info("DONE");
return clonedChannel; return clonedChannel;
} }

View File

@@ -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 // 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 = const newChannel =
await dynamicVoiceChannelService.createDynamicVoiceChannel( await dynamicVoiceChannelService.createDynamicVoiceChannel(
member.id, { name: member.displayName, id: member.id },
channel.id, channel.id,
); );
logger.debug(`Channel found for VoiceStateUpdate event: ${channel.name}`); logger.debug(`Channel found for VoiceStateUpdate event: ${channel.name}`);

View File

@@ -6,14 +6,22 @@ export const handleDynamicVoiceChannelDeletion = async (
oldState: VoiceState, oldState: VoiceState,
newState: 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 // 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; const channelId = oldState.channelId;
if (!channelId) return;
if (dynamicVoiceChannelService.createdChannelIdsSet.has(channelId)) { if (dynamicVoiceChannelService.createdChannelIdsSet.has(channelId)) {
const channel = await voiceChannelsService.getVoiceChannelById(channelId); const channel = await voiceChannelsService.getVoiceChannelById(channelId);
if (!channel) return; if (!channel) return;
if (channel.members.size === 0) { if (channel.members.size === 0) {
await dynamicVoiceChannelService.deleteDynamicVoiceChannel(channelId); await dynamicVoiceChannelService.deleteDynamicVoiceChannel(channelId);
} }

View File

@@ -6,7 +6,7 @@ export interface VoiceChannelsServiceInterface<
createVoiceChannel(name: string): Promise<C>; createVoiceChannel(name: string): Promise<C>;
cloneVoiceChannel( cloneVoiceChannel(
user_id: string, user: string,
channel: C, channel: C,
options?: { name?: string; position?: number }, options?: { name?: string; position?: number },
): Promise<C>; ): Promise<C>;

View File

@@ -7,12 +7,15 @@ import type {
export class DynamicVoiceChannelsService< export class DynamicVoiceChannelsService<
C extends { name: string; id: string }, C extends { name: string; id: string },
U extends { name: string; id: string },
> { > {
private voiceChannelsService: VoiceChannelsServiceInterface<C>; private voiceChannelsService: VoiceChannelsServiceInterface<C>;
dynamicVoiceChannels: z.output<typeof DynamicVoiceChannels>; dynamicVoiceChannels: z.output<typeof DynamicVoiceChannels>;
validChannelIds = new Set<string>(); validChannelIds = new Set<string>();
dynamicVoiceChannelsMap: z.output<typeof DynamicVoiceChannelsMap>; dynamicVoiceChannelsMap: z.output<typeof DynamicVoiceChannelsMap>;
createdChannelIdsSet = new Set<string>(); createdChannelIdsSet = new Set<string>();
private channelNumberCounter = 1;
private usedNumbers = new Set<number>();
constructor( constructor(
voiceChannelsService: VoiceChannelsServiceInterface<C>, voiceChannelsService: VoiceChannelsServiceInterface<C>,
@@ -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( async createDynamicVoiceChannel(
user_id: string, user: U,
channel_id: string, channel_id: string,
): Promise<C | undefined> { ): Promise<C | undefined> {
if (!this.validChannelIds.has(channel_id)) return; if (!this.validChannelIds.has(channel_id)) return;
@@ -42,11 +55,14 @@ export class DynamicVoiceChannelsService<
if (!channel) return; if (!channel) return;
const channelNumber = this.getNextChannelNumber();
const newChannel = await this.voiceChannelsService.cloneVoiceChannel( const newChannel = await this.voiceChannelsService.cloneVoiceChannel(
user_id, user.id,
channel, channel,
{ {
name: channel.name, name: `${channelNumber}`,
position: 99,
}, },
); );