Skip to content

Commit

Permalink
[New] Added playlist command and fixed template literals in help command
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Nov 15, 2021
1 parent b8d4261 commit 81c28a0
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 10 deletions.
16 changes: 6 additions & 10 deletions commands/Misc/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,9 @@ module.exports = {
const embed = {
color: "#9dcc37",
title: `${client.user.username}'s Categories!`,
description:
`These are my command categories ${categories.join(
", "
)}. To get more Information about each category, use ` +
"`" +
`${prefix}help <category> || <command>` +
"`",
description: `These are my command categories ${categories.join(
", "
)}. To get more Information about each category, use \`${prefix}help <category> || <command>\``,
timestamp: new Date(),
footer: {
text: `Requested by ${message.member.user.username}`,
Expand Down Expand Up @@ -87,19 +83,19 @@ module.exports = {
return await message.channel.send({ embeds: [embed] });
}

if (!command && !categories.includes("`" + searchCommand + "`")) {
if (!command && !categories.includes(`\`${searchCommand}\``)) {
return await message.channel.send(
"What you provided is neither a command nor a category"
);
}

if (!categories.includes("`" + searchCommand + "`")) {
if (!categories.includes(`\`${searchCommand}\``)) {
const embed = {
color: "#9dcc37",
fields: [
{
name: "Command",
value: "`" + `${command.name}` + "`",
value: `\`${command.name}\``,
inline: true,
},
{
Expand Down
235 changes: 235 additions & 0 deletions commands/Music/playlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { embedMessage } = require("../../modules/embedSimple");
const playdl = require("play-dl");

module.exports = {
name: "playlist",
aliases: ["pl"],
description: "Plays a saved playlist or adds one",
args: true,
usage: "pl add <custom name > <playlist link> || pl name",
async run(message, args, client, prefix) {
const checkdj = await client.db.get(`djRole_${message.guildId}`);
const userRoles = await message.member.roles.cache.map((role) => role.id);

if (
checkdj &&
!userRoles.includes(checkdj) &&
message.guild.ownerId !== message.author.id
) {
return await message.channel.send({
embeds: [
embedMessage(
"RED",
`You are not allowed to use this command.\n This command is only available for users with the DJ Role: <@&${checkdj}>`
),
],
});
}

if (!args[0])
return await message.channel.send({
embeds: [
embedMessage(
"RED",
`Please specify whether you want to play or add a new custom playlist!\n Use \`${prefix}h pl\` for more info`
),
],
});

if (args[0] === "add" && !args[1] && !args[2])
return await message.channel.send({
embeds: [
embedMessage(
"RED",
`You have to provide the link of the playlist and a custom name! \n Use \`${prefix}h pl\` for more info`
),
],
});

if (args[0] === "add" && args[1] && args[2]) {
await client.db.set(`${args[1]}`, args[2]);
return await message.channel.send({
embeds: [
embedMessage(
"#9dcc37",
`✅ | The playlist has been saved!\n Use ` +
"`" +
`${prefix}pl ${args[1]}` +
"`" +
` to start playing it!`
),
],
});
}
const customPlaylist = await client.db.get(`${args[0]}`);

if (!customPlaylist)
return await message.channel.send({
embeds: [embedMessage("RED", `❌ | No Playlist found with this name!`)],
});

if (!message.member.voice.channelId)
return message.channel.send({
embeds: [
embedMessage(
"RED",
`❌ | You must be in a voice channel to play music!`
),
],
});

if (
message.guild.me.voice.channelId &&
message.member.voice.channelId !== message.guild.me.voice.channelId
)
return await message.channel.send({
embeds: [embedMessage("RED", `❌ | You must be in my voice channel!`)],
});

const searchSong = await client.player.search(customPlaylist, {
requestedBy: message.member.user,
});

if (!searchSong.tracks.length || !searchSong)
return message.channel.send({
embeds: [
embedMessage(
"RED",
`❌ | Song not found, Maybe its age restricted or flagged as offensive by Youtube`
),
],
});

let queue = await client.player.createQueue(message.guildId, {
leaveOnEnd: false,
leaveOnStop: true,
initialVolume: 80,
leaveOnEmptyCooldown: 60 * 1000 * 3,
bufferingTimeout: 200,
leaveOnEmpty: true,
metadata: {
channel: message,
},
async onBeforeCreateStream(track, source, _queue) {
if (source === "soundcloud") {
const client_id = await playdl.getFreeClientID();
playdl.setToken({
soundcloud: {
client_id: client_id,
},
});
if (await playdl.so_validate(track.url)) {
let soundCloudInfo = await playdl.soundcloud(track.url);
return (await playdl.stream_from_info(soundCloudInfo)).stream;
}
return;
}

if (source === "youtube") {
const validateSP = playdl.sp_validate(track.url);
const spotifyList = ["track", "album", "playlist"];
if (spotifyList.includes(validateSP)) {
if (playdl.is_expired()) {
await playdl.refreshToken();
}
let spotifyInfo = await playdl.spotify(track.url);
let youtube = await playdl.search(`${spotifyInfo.name}`, {
limit: 2,
});
return (await playdl.stream(youtube[0].url)).stream;
}

return (await playdl.stream(track.url)).stream;
}
},
});

try {
if (!queue.connection) await queue.connect(message.member.voice.channel);
} catch {
client.player.deleteQueue(message.guildId);
queue.destroy(true);
return await message.channel.send({
content: "Could not join your voice channel!",
});
}

searchSong.playlist
? queue.addTracks(searchSong.tracks)
: queue.addTrack(searchSong.tracks[0]);

const musicEmbed = {
color: "#9dcc37",
title: `${queue.playing ? "✅ Added to Queue" : "🎵 Playing"}`,
author: {
name: `${message.member.user.username}`,
icon_url: `${
message.member.user.avatarURL() || client.user.avatarURL()
}`,
},
description: `Song: **[${searchSong.tracks[0].title}](${searchSong.tracks[0].url})**`,
thumbnail: {
url: `${searchSong.tracks[0].thumbnail}`,
},
fields: [
{
name: "Author",
value: `${searchSong.tracks[0].author}`,
inline: true,
},
{
name: "🕓 Duration",
value: `${searchSong.tracks[0].duration}`,
inline: true,
},
],

timestamp: new Date(),
};

let playlistEmbed = {
color: "#9dcc37",
description: `✅ | Queued ${queue.tracks.length} Songs`,
};

if (!queue.playing) {
try {
await queue.play();
searchSong.playlist
? await message.channel.send({
embeds: [playlistEmbed, musicEmbed],
})
: await message.channel.send({
embeds: [musicEmbed],
});
return;
} catch (err) {
client.logger(err.message, "error");
console.log(err);
await message.channel.send({
embeds: [
embedMessage(
"RED",
`❌ | An error occurred while trying to play this song! \nError Message: ${err.message}`
),
],
});
}
}

if (queue.playing) {
searchSong.playlist
? await message.channel.send({ embeds: [playlistEmbed, musicEmbed] })
: await message.channel.send({ embeds: [musicEmbed] });
return;
}
},
data: new SlashCommandBuilder()
.setName("playlist")
.setDescription("Plays a saved playlist or adds one")
.addStringOption((option) =>
option.setName("link").setDescription("playlist Link").setRequired(true)
),
async execute(interaction, client) {},
};

0 comments on commit 81c28a0

Please sign in to comment.