🎨 Code cleanup, remove old api package

This commit is contained in:
Manuel Ruwe 2022-12-17 17:56:55 +01:00
parent fe5096acf7
commit c2c2a9d091
8 changed files with 117 additions and 120 deletions

View File

@ -33,7 +33,6 @@
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"date-fns": "^2.29.3", "date-fns": "^2.29.3",
"discord.js": "^14.7.1", "discord.js": "^14.7.1",
"jellyfin-apiclient": "^1.10.0",
"joi": "^17.7.0", "joi": "^17.7.0",
"libsodium-wrappers": "^0.7.10", "libsodium-wrappers": "^0.7.10",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",

View File

@ -1,5 +1,7 @@
import { import {
AudioPlayer, AudioPlayer,
AudioPlayerPausedState,
AudioPlayerStatus,
AudioResource, AudioResource,
createAudioPlayer, createAudioPlayer,
getVoiceConnection, getVoiceConnection,
@ -68,14 +70,67 @@ export class DiscordVoiceService {
this.createAndReturnOrGetAudioPlayer().play(resource); this.createAndReturnOrGetAudioPlayer().play(resource);
} }
/**
* Pauses the current audio player
*/
pause() { pause() {
this.createAndReturnOrGetAudioPlayer().pause(); this.createAndReturnOrGetAudioPlayer().pause();
} }
/**
* Unpauses the current audio player
*/
unpause() { unpause() {
this.createAndReturnOrGetAudioPlayer().unpause(); this.createAndReturnOrGetAudioPlayer().unpause();
} }
/**
* Check if the current state is paused
* @returns The current pause state as a boolean
*/
isPaused() {
return (
this.createAndReturnOrGetAudioPlayer().state.status ===
AudioPlayerStatus.Paused
);
}
/**
* Checks if the current state is paused or not and toggles the states to the opposite.
* @returns The new paused state - true: paused, false: unpaused
*/
togglePaused(): boolean {
if (this.isPaused()) {
this.unpause();
return true;
}
this.pause();
return false;
}
disconnect(): GenericTryHandler {
if (this.voiceConnection === undefined) {
return {
success: false,
reply: {
embeds: [
this.discordMessageService.buildErrorMessage({
title: 'Unable to disconnect from voice channel',
description: 'I am currently not connected to any voice channels',
}),
],
},
};
}
this.voiceConnection.destroy();
return {
success: true,
reply: {},
};
}
disconnectGracefully() { disconnectGracefully() {
const connections = getVoiceConnections(); const connections = getVoiceConnections();
this.logger.debug( this.logger.debug(

View File

@ -1,23 +1,25 @@
import { TransformPipe } from '@discord-nestjs/common'; import { TransformPipe } from '@discord-nestjs/common';
import { import { Command, DiscordCommand, UsePipes } from '@discord-nestjs/core';
Command, import { CommandInteraction } from 'discord.js';
DiscordTransformedCommand, import { DiscordMessageService } from '../clients/discord/discord.message.service';
TransformedCommandExecutionContext, import { GenericCustomReply } from '../models/generic-try-handler';
UsePipes,
} from '@discord-nestjs/core';
import { InteractionReplyOptions } from 'discord.js';
@Command({ @Command({
name: 'current', name: 'current',
description: 'Print the current track information', description: 'Print the current track information',
}) })
@UsePipes(TransformPipe) @UsePipes(TransformPipe)
export class CurrentTrackCommand implements DiscordTransformedCommand<unknown> { export class CurrentTrackCommand implements DiscordCommand {
handler( constructor(private readonly discordMessageService: DiscordMessageService) {}
dto: unknown,
executionContext: TransformedCommandExecutionContext<any>, handler(interaction: CommandInteraction): GenericCustomReply {
): InteractionReplyOptions | string { return {
return 'nice'; embeds: [
this.discordMessageService.buildErrorMessage({
title: 'NOT IMPLEMENTED',
}),
],
};
} }
} }

View File

@ -1,13 +1,10 @@
import { TransformPipe } from '@discord-nestjs/common'; import { TransformPipe } from '@discord-nestjs/common';
import { Command, DiscordCommand, UsePipes } from '@discord-nestjs/core'; import { Command, DiscordCommand, UsePipes } from '@discord-nestjs/core';
import { import { CommandInteraction } from 'discord.js';
CommandInteraction, import { DiscordMessageService } from '../clients/discord/discord.message.service';
EmbedBuilder, import { DiscordVoiceService } from '../clients/discord/discord.voice.service';
InteractionReplyOptions, import { GenericCustomReply } from '../models/generic-try-handler';
} from 'discord.js';
import { getVoiceConnection } from '@discordjs/voice';
import { DefaultJellyfinColor, ErrorJellyfinColor } from '../types/colors';
@Command({ @Command({
name: 'disconnect', name: 'disconnect',
@ -15,40 +12,23 @@ import { DefaultJellyfinColor, ErrorJellyfinColor } from '../types/colors';
}) })
@UsePipes(TransformPipe) @UsePipes(TransformPipe)
export class DisconnectCommand implements DiscordCommand { export class DisconnectCommand implements DiscordCommand {
handler(interaction: CommandInteraction): InteractionReplyOptions | string { constructor(
const connection = getVoiceConnection(interaction.guildId); private readonly discordVoiceService: DiscordVoiceService,
private readonly discordMessageService: DiscordMessageService,
) {}
if (!connection) { handler(interaction: CommandInteraction): GenericCustomReply {
return { const disconnect = this.discordVoiceService.disconnect();
embeds: [
new EmbedBuilder() if (!disconnect.success) {
.setColor(ErrorJellyfinColor) return disconnect.reply;
.setAuthor({
name: 'Unable to disconnect from voice channel',
iconURL:
'https://github.com/manuel-rw/jellyfin-discord-music-bot/blob/nestjs-migration/images/icons/alert-circle.png?raw=true',
})
.setDescription(
'I am currently not connected to any voice channels',
)
.toJSON(),
],
};
return;
} }
connection.destroy();
return { return {
embeds: [ embeds: [
new EmbedBuilder() this.discordMessageService.buildMessage({
.setColor(DefaultJellyfinColor) title: 'Disconnected from your channel',
.setAuthor({ }),
name: 'Disconnected from your channel',
iconURL:
'https://github.com/manuel-rw/jellyfin-discord-music-bot/blob/nestjs-migration/images/icons/circle-check.png?raw=true',
})
.toJSON(),
], ],
}; };
} }

View File

@ -1,30 +1,18 @@
import { TransformPipe } from '@discord-nestjs/common'; import { TransformPipe } from '@discord-nestjs/common';
import { import { Command, DiscordCommand, UsePipes } from '@discord-nestjs/core';
Command,
DiscordTransformedCommand,
TransformedCommandExecutionContext,
UsePipes,
} from '@discord-nestjs/core';
import { EmbedBuilder } from '@discordjs/builders'; import { EmbedBuilder } from '@discordjs/builders';
import { InteractionReplyOptions, MessagePayload } from 'discord.js'; import { CommandInteraction } from 'discord.js';
import { DefaultJellyfinColor } from 'src/types/colors'; import { DefaultJellyfinColor } from 'src/types/colors';
import { GenericCustomReply } from '../models/generic-try-handler';
@Command({ @Command({
name: 'help', name: 'help',
description: 'Get help if you&apos;re having problems with this bot', description: 'Get help if you&apos;re having problems with this bot',
}) })
@UsePipes(TransformPipe) @UsePipes(TransformPipe)
export class HelpCommand implements DiscordTransformedCommand<unknown> { export class HelpCommand implements DiscordCommand {
handler( handler(commandInteraction: CommandInteraction): GenericCustomReply {
dto: unknown,
executionContext: TransformedCommandExecutionContext<any>,
):
| string
| void
| MessagePayload
| InteractionReplyOptions
| Promise<string | void | MessagePayload | InteractionReplyOptions> {
return { return {
embeds: [ embeds: [
new EmbedBuilder() new EmbedBuilder()

View File

@ -1,15 +1,11 @@
import { TransformPipe } from '@discord-nestjs/common'; import { TransformPipe } from '@discord-nestjs/common';
import { Command, DiscordCommand, UsePipes } from '@discord-nestjs/core'; import { Command, DiscordCommand, UsePipes } from '@discord-nestjs/core';
import { joinVoiceChannel } from '@discordjs/voice';
import { Logger } from '@nestjs/common'; import { Logger } from '@nestjs/common';
import { import { CommandInteraction, GuildMember } from 'discord.js';
CommandInteraction, import { DiscordMessageService } from '../clients/discord/discord.message.service';
EmbedBuilder, import { DiscordVoiceService } from '../clients/discord/discord.voice.service';
GuildMember, import { GenericCustomReply } from '../models/generic-try-handler';
InteractionReplyOptions,
} from 'discord.js';
import { DefaultJellyfinColor, ErrorJellyfinColor } from '../types/colors';
@Command({ @Command({
name: 'summon', name: 'summon',
@ -19,45 +15,28 @@ import { DefaultJellyfinColor, ErrorJellyfinColor } from '../types/colors';
export class SummonCommand implements DiscordCommand { export class SummonCommand implements DiscordCommand {
private readonly logger = new Logger(SummonCommand.name); private readonly logger = new Logger(SummonCommand.name);
handler(interaction: CommandInteraction): InteractionReplyOptions | string { constructor(
private readonly discordVoiceService: DiscordVoiceService,
private readonly discordMessageService: DiscordMessageService,
) {}
handler(interaction: CommandInteraction): GenericCustomReply {
const guildMember = interaction.member as GuildMember; const guildMember = interaction.member as GuildMember;
if (guildMember.voice.channel === null) { const tryResult =
return { this.discordVoiceService.tryJoinChannelAndEstablishVoiceConnection(
embeds: [ guildMember,
new EmbedBuilder() );
.setColor(ErrorJellyfinColor)
.setAuthor({ if (!tryResult.success) {
name: 'Unable to join your channel', return tryResult.reply;
iconURL:
'https://github.com/manuel-rw/jellyfin-discord-music-bot/blob/nestjs-migration/images/icons/alert-circle.png?raw=true',
})
.setDescription(
'You are in a channel, I am either unabelt to connect to or you aren&apost in a channel yet',
)
.toJSON(),
],
};
} }
const channel = guildMember.voice.channel;
joinVoiceChannel({
channelId: channel.id,
adapterCreator: channel.guild.voiceAdapterCreator,
guildId: channel.guildId,
});
return { return {
embeds: [ embeds: [
new EmbedBuilder() this.discordMessageService.buildMessage({
.setColor(DefaultJellyfinColor) title: 'Joined your voicehannel',
.setAuthor({ }),
name: 'Joined your voicehannel',
iconURL:
'https://github.com/manuel-rw/jellyfin-discord-music-bot/blob/nestjs-migration/images/icons/circle-check.png?raw=true&test=a',
})
.toJSON(),
], ],
}; };
} }

View File

@ -2,8 +2,10 @@ import { InteractionReplyOptions } from 'discord.js';
export interface GenericTryHandler { export interface GenericTryHandler {
success: boolean; success: boolean;
reply: reply: GenericCustomReply;
| string
| InteractionReplyOptions
| Promise<string | InteractionReplyOptions>;
} }
export type GenericCustomReply =
| string
| InteractionReplyOptions
| Promise<string | InteractionReplyOptions>;

View File

@ -4437,13 +4437,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"jellyfin-apiclient@npm:^1.10.0":
version: 1.10.0
resolution: "jellyfin-apiclient@npm:1.10.0"
checksum: 494161b3acf2e17db22c50fd13c637890985fa95aced5f4161045ae7a1eb80374625e83f2bd286a5a0f06847e06fab1f4dcc2d93f603bc1b033344adb12a7f7b
languageName: node
linkType: hard
"jellyfin-discord-music-bot@workspace:.": "jellyfin-discord-music-bot@workspace:.":
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "jellyfin-discord-music-bot@workspace:." resolution: "jellyfin-discord-music-bot@workspace:."
@ -4472,7 +4465,6 @@ __metadata:
eslint: ^8.0.1 eslint: ^8.0.1
eslint-config-prettier: ^8.3.0 eslint-config-prettier: ^8.3.0
eslint-plugin-prettier: ^4.0.0 eslint-plugin-prettier: ^4.0.0
jellyfin-apiclient: ^1.10.0
jest: 28.1.3 jest: 28.1.3
joi: ^17.7.0 joi: ^17.7.0
libsodium-wrappers: ^0.7.10 libsodium-wrappers: ^0.7.10