Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasB25 committed Aug 22, 2024
1 parent 11a4ca6 commit 1a1e4d3
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 92 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
88 changes: 68 additions & 20 deletions src/commands/playlist/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class GetPlaylists extends Command {

public async run(client: Lavamusic, ctx: Context): Promise<any> {
try {
let userId: any;
let userId: string | null = null;
let targetUser = ctx.args[0];

if (targetUser?.startsWith("<@") && targetUser.endsWith(">")) {
Expand All @@ -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
*/
163 changes: 92 additions & 71 deletions src/commands/playlist/Steal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default class StealPlaylist extends Command {
name: "steal",
description: {
content: "cmd.steal.description",
examples: ["steal <playlist_name> <@user>"],
usage: "steal <playlist_name> <@user>",
examples: ["steal <@user> <playlist_name>"],
usage: "steal <@user> <playlist_name>",
},
category: "playlist",
aliases: ["st"],
Expand All @@ -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<any> {
const playlistName = args.shift();
let targetUserId: string | null = null;
public async run(client: Lavamusic, ctx: Context): Promise<any> {
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);
Expand All @@ -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,
},
],
});
}
}

Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 1a1e4d3

Please sign in to comment.