From 1a1e4d32db8e3b97d0ac63c561ae1d33cfe6df34 Mon Sep 17 00:00:00 2001 From: LucasB25 <50886682+LucasB25@users.noreply.github.com> Date: Thu, 22 Aug 2024 21:41:31 +0200 Subject: [PATCH] fix --- package.json | 2 +- src/commands/playlist/List.ts | 88 ++++++++++++++---- src/commands/playlist/Steal.ts | 163 +++++++++++++++++++-------------- 3 files changed, 161 insertions(+), 92 deletions(-) diff --git a/package.json b/package.json index 08bc38115..86d6be70e 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "devDependencies": { "@biomejs/biome": "^1.8.3", "@types/i18n": "^0.13.12", - "@types/node": "^22.4.2", + "@types/node": "^22.5.0", "@types/signale": "^1.4.7", "prisma": "^5.18.0", "typescript": "^5.5.4" diff --git a/src/commands/playlist/List.ts b/src/commands/playlist/List.ts index eeba5c708..73e136c91 100644 --- a/src/commands/playlist/List.ts +++ b/src/commands/playlist/List.ts @@ -39,7 +39,7 @@ export default class GetPlaylists extends Command { public async run(client: Lavamusic, ctx: Context): Promise { try { - let userId: any; + let userId: string | null = null; let targetUser = ctx.args[0]; if (targetUser?.startsWith("<@") && targetUser.endsWith(">")) { @@ -52,38 +52,86 @@ export default class GetPlaylists extends Command { targetUser = await client.users.fetch(targetUser); userId = targetUser.id; } else if (targetUser) { - targetUser = await client.users.fetch(ctx.args[0]); - userId = targetUser.id; + try { + targetUser = await client.users.fetch(targetUser); + userId = targetUser.id; + } catch (_error) { + const users = client.users.cache.filter((user) => user.username.toLowerCase() === targetUser.toLowerCase()); + + if (users.size > 0) { + targetUser = users.first(); + userId = targetUser?.id ?? null; + } else { + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.list.messages.invalid_username"), + color: this.client.color.red, + }, + ], + }); + } + } } else { userId = ctx.author.id; targetUser = ctx.author; } + if (!userId) { + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.list.messages.invalid_userid"), + color: this.client.color.red, + }, + ], + }); + } + const playlists = await client.db.getUserPlaylists(userId); if (!playlists || playlists.length === 0) { - const noPlaylistsMessage = this.client - .embed() - .setDescription(ctx.locale("cmd.list.messages.no_playlists")) - .setColor(this.client.color.red); - return await ctx.sendMessage({ embeds: [noPlaylistsMessage] }); + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.list.messages.no_playlists"), + color: this.client.color.red, + }, + ], + }); } const targetUsername = targetUser ? targetUser.username : ctx.locale("cmd.list.messages.your"); - const successMessage = this.client - .embed() - .setTitle( - ctx.locale("cmd.list.messages.playlists_title", { - username: targetUsername, - }), - ) - .setDescription(playlists.map((playlist: any) => playlist.name).join("\n")) - .setColor(this.client.color.green); - await ctx.sendMessage({ embeds: [successMessage] }); + return await ctx.sendMessage({ + embeds: [ + { + title: ctx.locale("cmd.list.messages.playlists_title", { username: targetUsername }), + description: playlists.map((playlist: any) => playlist.name).join("\n"), + color: this.client.color.main, + }, + ], + }); } catch (error) { console.error(error); - const errorMessage = this.client.embed().setDescription(ctx.locale("cmd.list.messages.error")).setColor(this.client.color.red); - await ctx.sendMessage({ embeds: [errorMessage] }); + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.list.messages.error"), + color: this.client.color.red, + }, + ], + }); } } } + +/** + * Project: lavamusic + * Author: Appu + * Main Contributor: LucasB25 + * Company: Coders + * Copyright (c) 2024. All rights reserved. + * This code is the property of Coder and may not be reproduced or + * modified without permission. For more information, contact us at + * https://discord.gg/ns8CTk9J3e + */ diff --git a/src/commands/playlist/Steal.ts b/src/commands/playlist/Steal.ts index 1f272e8f7..c780c7a21 100644 --- a/src/commands/playlist/Steal.ts +++ b/src/commands/playlist/Steal.ts @@ -6,8 +6,8 @@ export default class StealPlaylist extends Command { name: "steal", description: { content: "cmd.steal.description", - examples: ["steal <@user>"], - usage: "steal <@user>", + examples: ["steal <@user> "], + usage: "steal <@user> ", }, category: "playlist", aliases: ["st"], @@ -27,26 +27,27 @@ export default class StealPlaylist extends Command { }, slashCommand: true, options: [ - { - name: "playlist", - description: "cmd.steal.options.playlist", - type: 3, - required: true, - }, { name: "user", description: "cmd.steal.options.user", type: 6, required: true, }, + { + name: "playlist", + description: "cmd.steal.options.playlist", + type: 3, + required: true, + autocomplete: true, + }, ], }); } - public async run(client: Lavamusic, ctx: Context, args: string[]): Promise { - const playlistName = args.shift(); - let targetUserId: string | null = null; + public async run(client: Lavamusic, ctx: Context): Promise { let targetUser = ctx.args[0]; + const playlistName = ctx.args[1]; + let targetUserId: string | null = null; if (targetUser?.startsWith("<@") && targetUser.endsWith(">")) { targetUser = targetUser.slice(2, -1); @@ -56,59 +57,101 @@ export default class StealPlaylist extends Command { targetUser = await client.users.fetch(targetUser); targetUserId = targetUser.id; } else if (targetUser) { - targetUser = await client.users.fetch(ctx.args[0]); - targetUserId = targetUser.id; + try { + targetUser = await client.users.fetch(targetUser); + targetUserId = targetUser.id; + } catch (_error) { + const users = client.users.cache.filter((user) => user.username.toLowerCase() === targetUser.toLowerCase()); + + if (users.size > 0) { + targetUser = users.first(); + targetUserId = targetUser.id; + } else { + return await ctx.sendMessage({ + embeds: [ + { + description: "Invalid username or user not found.", + color: this.client.color.red, + }, + ], + }); + } + } } if (!playlistName) { - const errorMessage = this.client - .embed() - .setDescription(ctx.locale("cmd.steal.messages.provide_playlist")) - .setColor(this.client.color.red); - return await ctx.sendMessage({ embeds: [errorMessage] }); + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.steal.messages.provide_playlist"), + color: this.client.color.red, + }, + ], + }); } if (!targetUserId) { - const errorMessage = this.client - .embed() - .setDescription(ctx.locale("cmd.steal.messages.provide_user")) - .setColor(this.client.color.red); - return await ctx.sendMessage({ embeds: [errorMessage] }); + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.steal.messages.provide_user"), + color: this.client.color.red, + }, + ], + }); } try { const targetPlaylist = await client.db.getPlaylist(targetUserId, playlistName); if (!targetPlaylist) { - const playlistNotFoundError = this.client - .embed() - .setDescription(ctx.locale("cmd.steal.messages.playlist_not_exist")) - .setColor(this.client.color.red); return await ctx.sendMessage({ - embeds: [playlistNotFoundError], + embeds: [ + { + description: ctx.locale("cmd.steal.messages.playlist_not_exist"), + color: this.client.color.red, + }, + ], }); } const targetSongs = await client.db.getSongs(targetUserId, playlistName); + + const existingPlaylist = await client.db.getPlaylist(ctx.author.id, playlistName); + if (existingPlaylist) { + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.steal.messages.playlist_exists", { playlist: playlistName }), + color: this.client.color.red, + }, + ], + }); + } + await client.db.createPlaylistWithSongs(ctx.author.id, playlistName, targetSongs); - const successMessage = this.client - .embed() - .setDescription( - ctx.locale("cmd.steal.messages.playlist_stolen", { - playlist: playlistName, - user: targetUser.username, - }), - ) - .setColor(this.client.color.green); - await ctx.sendMessage({ embeds: [successMessage] }); + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.steal.messages.playlist_stolen", { + playlist: playlistName, + user: targetUser.username, + }), + color: this.client.color.main, + }, + ], + }); } catch (error) { console.error(error); - const errorMessage = this.client - .embed() - .setDescription(ctx.locale("cmd.steal.messages.error_occurred")) - .setColor(this.client.color.red); - await ctx.sendMessage({ embeds: [errorMessage] }); + return await ctx.sendMessage({ + embeds: [ + { + description: ctx.locale("cmd.steal.messages.error_occurred"), + color: this.client.color.red, + }, + ], + }); } } @@ -119,12 +162,7 @@ export default class StealPlaylist extends Command { if (!userOptionId) { await interaction - .respond([ - { - name: "Please specify a user to search their playlists.", - value: "NoUser", - }, - ]) + .respond([{ name: "Please specify a user to search their playlists.", value: "NoUser" }]) .catch(console.error); return; } @@ -138,36 +176,19 @@ export default class StealPlaylist extends Command { const playlists = await this.client.db.getUserPlaylists(user.id); if (!playlists || playlists.length === 0) { - await interaction - .respond([ - { - name: "No playlists found for this user.", - value: "NoPlaylists", - }, - ]) - .catch(console.error); + await interaction.respond([{ name: "No playlists found for this user.", value: "NoPlaylists" }]).catch(console.error); return; } const filtered = playlists.filter((playlist) => playlist.name.toLowerCase().startsWith(focusedValue.toLowerCase())); - await interaction - .respond( - filtered.map((playlist) => ({ - name: playlist.name, - value: playlist.name, - })), - ) + return await interaction + .respond(filtered.map((playlist) => ({ name: playlist.name, value: playlist.name }))) .catch(console.error); } catch (error) { console.error("Error in autocomplete interaction:", error); - await interaction - .respond([ - { - name: "An error occurred while fetching playlists.", - value: "Error", - }, - ]) + return await interaction + .respond([{ name: "An error occurred while fetching playlists.", value: "Error" }]) .catch(console.error); } }