Skip to content

Commit

Permalink
feature: Auto leave voice channel (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uo1428 authored Oct 7, 2024
1 parent 87be02d commit 7a75bf4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/clients/discord/discord.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export class DiscordClientModule implements OnModuleDestroy {
this.discordVoiceService.disconnectGracefully();
}
}

43 changes: 41 additions & 2 deletions src/clients/discord/discord.voice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
VoiceConnectionStatus,
} from '@discordjs/voice';

import { Injectable } from '@nestjs/common';
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { Logger } from '@nestjs/common/services';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import { Interval } from '@nestjs/schedule';
Expand All @@ -22,6 +22,7 @@ import {
InteractionEditReplyOptions,
InteractionReplyOptions,
MessagePayload,
VoiceChannel,
} from 'discord.js';

import { TryResult } from '../../models/TryResult';
Expand All @@ -33,11 +34,12 @@ import { JellyfinWebSocketService } from '../jellyfin/jellyfin.websocket.service
import { DiscordMessageService } from './discord.message.service';

@Injectable()
export class DiscordVoiceService {
export class DiscordVoiceService implements OnModuleDestroy {
private readonly logger = new Logger(DiscordVoiceService.name);
private audioPlayer: AudioPlayer | undefined;
private voiceConnection: VoiceConnection | undefined;
private audioResource: AudioResource | undefined;
private autoLeaveIntervalId: NodeJS.Timeout | null = null;

constructor(
private readonly discordMessageService: DiscordMessageService,
Expand All @@ -47,6 +49,18 @@ export class DiscordVoiceService {
private readonly eventEmitter: EventEmitter2,
) {}

onModuleDestroy() {
if (this.autoLeaveIntervalId) {
try {
clearInterval(this.autoLeaveIntervalId);
this.autoLeaveIntervalId = null;
this.logger.debug('autoLeaveIntervalId Cleared');
} catch (error) {
this.logger.error(`Error while clearing autoLeaveIntervalId: ${error}`);
}
}
}

@OnEvent('internal.audio.track.announce')
handleOnNewTrack(track: Track) {
const resource = createAudioResource(
Expand Down Expand Up @@ -108,6 +122,31 @@ export class DiscordVoiceService {
this.disconnect();
}
});

const voiceChannelId = channel.id;
this.autoLeaveIntervalId = setInterval(async () => {
const voiceChannel = (await member.guild.channels.fetch(
voiceChannelId,
)) as VoiceChannel | undefined;
if (voiceChannel === undefined) {
clearInterval(this.autoLeaveIntervalId);
return;
}
const voiceChannelMembersExpectBots = voiceChannel.members.filter(
(member) => !member.user.bot,
);

if (voiceChannelMembersExpectBots.size > 0) return;

try {
this.stop(true);
this.disconnect();
clearInterval(this.autoLeaveIntervalId);
} catch (error) {
this.logger.error(`Failed to disconnect voice channel ${error}`);
}
}, 5000);

return {
success: true,
reply: {},
Expand Down

0 comments on commit 7a75bf4

Please sign in to comment.