Skip to content

Commit

Permalink
3.7.0-dev-4
Browse files Browse the repository at this point in the history
Added errors handler in /pl-remove
  • Loading branch information
AlexInCube committed Aug 19, 2024
1 parent 6162056 commit 67c95c1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
Binary file added icons/audioplayer/player/favorite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/audioplayer/LoadPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function setupYtCookieSchedule() {
const cookies = await getYoutubeCookie();
if (!cookies) return;
YtPlugin.cookies = cookies;
loggerSend('Cookies is fetched again through Google Auth', loggerPrefixAudioplayerPluginsLoader);
loggerSend('Cookies is fetched by cron job through Google Auth', loggerPrefixAudioplayerPluginsLoader);
});
}
}
Expand Down
38 changes: 37 additions & 1 deletion src/commands/audio/pl-remove.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { CommandArgument, ICommand } from '../../CommandTypes.js';
import { GroupAudio } from './AudioTypes.js';
import { ChatInputCommandInteraction, Message, PermissionsBitField, SlashCommandBuilder } from 'discord.js';
import i18next from 'i18next';
import { UserPlaylistNamesAutocomplete, UserPlaylistRemoveSong } from '../../schemas/SchemaPlaylist.js';
import {
PlaylistIsNotExists,
PlaylistSongNotExists,
UserPlaylistNamesAutocomplete,
UserPlaylistRemoveSong
} from '../../schemas/SchemaPlaylist.js';
import { generateSimpleEmbed } from '../../utilities/generateSimpleEmbed.js';
import { generateErrorEmbed } from '../../utilities/generateErrorEmbed.js';
import { ENV } from '../../EnvironmentVariables.js';
Expand Down Expand Up @@ -75,6 +80,37 @@ async function plRemoveAndReply(
ephemeral: true
});
} catch (e) {
if (e instanceof PlaylistIsNotExists) {
await ctx.reply({
embeds: [
generateErrorEmbed(
i18next.t('commands:pl_error_playlist_not_exists', {
name: playlistName,
interpolation: { escapeValue: false }
})
)
],
ephemeral: true
});
return;
}

if (e instanceof PlaylistSongNotExists) {
await ctx.reply({
embeds: [
generateErrorEmbed(
i18next.t('commands:pl_error_song_is_not_exists_in_playlist', {
name: playlistName,
id: songID + 1,
interpolation: { escapeValue: false }
})
)
],
ephemeral: true
});
return;
}

await ctx.reply({
embeds: [generateErrorEmbed(i18next.t('commands:pl-remove_error_unknown'))],
ephemeral: true
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"pl_arg_name": "playlist name",
"pl_arg_song_id": "song id in playlist",
"pl_error_playlist_not_exists": "Playlist ``{{name}}`` is not exists",
"pl_error_song_is_not_exists_in_playlist": "Song with ID ``{{id}}`` is not exists in playlist ``{{name}}``",
"pl-create_desc": "Create a empty playlist",
"pl-create_success": "Playlist ``{{name}}`` created",
"pl-create_error_duplicate": "Playlist with name ``{{name}}`` already exists",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ru/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"pl_arg_name": "имя плейлиста",
"pl_arg_song_id": "номер песни в плейлисте",
"pl_error_playlist_not_exists": "Плейлист ``{{name}}`` не существует",
"pl_error_song_is_not_exists_in_playlist": "Песня с ID ``{{id}}`` не существует в плейлисте ``{{name}}``",
"pl-create_desc": "Создание пустого плейлиста",
"pl-create_success": "Плейлист ``{{name}}`` создан",
"pl-create_error_duplicate": "Плейлист с именем ``{{name}}`` уже существует",
Expand Down
11 changes: 10 additions & 1 deletion src/schemas/SchemaPlaylist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,22 @@ export class PlaylistSongIsNotValid extends Error {
}
}

export class PlaylistSongNotExists extends Error {
constructor(playlistName: string, songID: number) {
super();
this.name = 'PlaylistSongNotExists';
this.message = `Song with id ${songID} not exists in playlist ${playlistName}`;
}
}
/*
export class PlaylistSongAlreadyInPlaylist extends Error {
constructor(playlistName: string, songName: string) {
super();
this.name = 'PlaylistSongAlreadyInPlaylist';
this.message = `Song ${songName} already in playlist ${playlistName}`;
}
}

*/
export async function UserPlaylistCreate(userID: string, name: string): Promise<void> {
const playlist = await UserPlaylistGet(userID, name);
if (playlist) throw new PlaylistAlreadyExists(name);
Expand Down Expand Up @@ -180,6 +188,7 @@ export async function UserPlaylistRemoveSong(
if (!playlist) throw new PlaylistIsNotExists(name);

const song = playlist.songs[index];
if (!song) throw new PlaylistSongNotExists(name, index);
playlist.songs.splice(index, 1);

await playlist.save();
Expand Down

0 comments on commit 67c95c1

Please sign in to comment.