Skip to content

Commit 1df47d0

Browse files
committed
fix: error handling
1 parent f20b4cd commit 1df47d0

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

examples/lavalink/src/app.dto.ts renamed to examples/lavalink/src/dtos/query.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SearchPlatform } from 'lavalink-client';
2-
import { StringOption } from '../../../packages';
2+
import { StringOption } from '../../../../packages';
33

44
export class QueryDto {
55
@StringOption({

examples/lavalink/src/source.autocomplete.ts renamed to examples/lavalink/src/interceptor/source-autocomplete.interceptor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable } from '@nestjs/common';
22
import { AutocompleteInteraction } from 'discord.js';
33
import { DefaultSources } from 'lavalink-client';
4-
import { AutocompleteInterceptor } from '../../../packages';
4+
import { AutocompleteInterceptor } from '../../../../packages';
55

66
@Injectable()
77
export class SourceAutocompleteInterceptor extends AutocompleteInterceptor {
@@ -12,7 +12,6 @@ export class SourceAutocompleteInterceptor extends AutocompleteInterceptor {
1212
if (focused.name === 'source') {
1313
choices = [DefaultSources.soundcloud]; // Note that some Sources needs extra plugins/configuration to property work
1414
}
15-
1615
return interaction.respond(
1716
choices
1817
.filter((choice) => choice.startsWith(focused.value.toString()))

examples/lavalink/src/app.gateway.ts renamed to examples/lavalink/src/levalink-example.gateway.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable, Logger } from '@nestjs/common';
1+
import { Injectable, Logger, UseInterceptors } from '@nestjs/common';
22
import {
33
Context,
44
OnNodeManager,
@@ -11,42 +11,57 @@ import {
1111
SlashCommandContext,
1212
Options,
1313
} from '../../../packages';
14-
import { QueryDto } from './app.dto';
14+
import { QueryDto } from './dtos/query.dto';
15+
import { SourceAutocompleteInterceptor } from './interceptor/source-autocomplete.interceptor';
1516

1617
@Injectable()
17-
export class AppGateway {
18+
export class LevalinkExampleGateway {
1819
constructor(
1920
private readonly playerManager: PlayerManager,
2021
private readonly lavalinkService: NestCordLavalinkService,
2122
) {}
2223

23-
private readonly logger = new Logger(AppGateway.name);
24+
private readonly logger = new Logger(LevalinkExampleGateway.name);
2425

2526
@OnNodeManager('connect')
2627
public onConnect(@Context() [node]: NodeManagerContextOf<'connect'>) {
2728
this.logger.log(`Node: ${node.options.host} Connected`);
2829
}
2930

31+
@OnNodeManager('disconnect')
32+
public onNodeDisconnect(@Context() [node]: NodeManagerContextOf<'disconnect'>) {
33+
this.logger.log(`Node: ${node.options.host} disconnected`);
34+
}
35+
3036
@OnLavalinkManager('playerCreate')
3137
public onPlayerCreate(@Context() [player]: LavalinkManagerContextOf<'playerCreate'>) {
3238
this.logger.log(`Player created at ${player.guildId}`);
3339
}
3440

41+
@UseInterceptors(SourceAutocompleteInterceptor)
3542
@SlashCommand({
3643
name: 'play',
3744
description: 'play a track',
3845
})
3946
async handlePlay(@Context() [interaction]: SlashCommandContext, @Options() { query, source }: QueryDto) {
47+
if (!interaction.inCachedGuild()) {
48+
return;
49+
}
50+
if (!interaction.member.voice.channel) {
51+
return interaction.reply({
52+
content: `You must located in the voice channel.`,
53+
ephemeral: true,
54+
});
55+
}
56+
4057
const player =
4158
this.playerManager.get(interaction.guild.id) ??
4259
this.playerManager.create({
4360
...this.lavalinkService.extractInfoForPlayer(interaction),
44-
// optional configurations:
4561
selfDeaf: true,
4662
selfMute: false,
4763
volume: 50,
4864
});
49-
5065
await player.connect();
5166

5267
const res = await player.search(
@@ -78,6 +93,11 @@ export class AppGateway {
7893
});
7994
}
8095

81-
player.stopPlaying();
96+
await player.stopPlaying();
97+
await player.disconnect();
98+
99+
return interaction.reply({
100+
content: 'Player has been stoped',
101+
});
82102
}
83103
}

examples/lavalink/src/app.module.ts renamed to examples/lavalink/src/levalink-example.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GatewayIntentBits, Partials } from 'discord.js';
22
import { NestCordModule } from '../../../packages/core';
33
import { Module } from '@nestjs/common';
4-
import { AppGateway } from './app.gateway';
4+
import { LevalinkExampleGateway } from './levalink-example.gateway';
55
import { NestCordLavalinkModule } from '../../../packages';
66

77
@Module({
@@ -28,6 +28,6 @@ import { NestCordLavalinkModule } from '../../../packages';
2828
],
2929
}),
3030
],
31-
providers: [AppGateway],
31+
providers: [LevalinkExampleGateway],
3232
})
33-
export class AppModule {}
33+
export class LevalinkExampleModule {}

examples/lavalink/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { NestFactory } from '@nestjs/core';
2-
import { AppModule } from './app.module';
2+
import { LevalinkExampleModule } from './levalink-example.module';
33

44
async function bootstrap() {
5-
const app = await NestFactory.create(AppModule);
5+
const app = await NestFactory.create(LevalinkExampleModule);
66
await app.listen(2400);
77
}
88
bootstrap();

packages/lavalink/src/nestcord-lavalink.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NestCordLavaLinkConfigurableModule, LAVALINK_MODULE_OPTIONS } from './nestcord-lavalink.module-definition';
22
import { Global, Inject, Logger, Module, OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
33
import * as ProvidersMap from './providers';
4-
import { DestroyReasons, LavalinkManager } from 'lavalink-client';
4+
import { DestroyReasons, LavalinkManager, NodeManager } from 'lavalink-client';
55
import { Client } from 'discord.js';
66
import { LavalinkListenersModule } from './listeners';
77
import { NestCordLavalinkModuleOptions } from './nestcord-lavalink-options.interface';
@@ -24,6 +24,7 @@ export class NestCordLavalinkModule
2424
public constructor(
2525
private readonly client: Client,
2626
private readonly lavalinkManager: LavalinkManager,
27+
private readonly nodeManager: NodeManager,
2728
@Inject(LAVALINK_MODULE_OPTIONS)
2829
private readonly options: NestCordLavalinkModuleOptions,
2930
) {
@@ -42,6 +43,10 @@ export class NestCordLavalinkModule
4243
this.logger.log('Lavalink Manager Initialized');
4344
});
4445

46+
this.nodeManager.on('error', (node, error) => {
47+
this.logger.error(`An error occured while connecting to the node: ${node.id}`, error.stack);
48+
});
49+
4550
this.client.on('raw', (data) => {
4651
this.lavalinkManager.sendRawData(data);
4752
});

0 commit comments

Comments
 (0)