Skip to content

Commit

Permalink
[New] Added play command functionality with messageCreate event, refa…
Browse files Browse the repository at this point in the history
…ctoring will be done later...
  • Loading branch information
naseif committed Oct 3, 2021
1 parent 7d32943 commit d2540df
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
122 changes: 120 additions & 2 deletions commands/music/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,127 @@ module.exports = {
name: "play",
aliases: ["p"],
description: "Plays music from Youtube",
args: true,
usage: "<YouTube URL | Song Name | Spotify URL | Soundcloud URL |>",
async run(message, args, client, prefix) {
if (!args[0])
return message.channel.send("You did not provide a song name!");
const songString = args.join(" ");
if (!songString)
return message.channel.send("You have to provide a song name or URL");
if (!message.member.voice.channelId)
return message.channel.send({
embeds: [
embedMessage(
"#9dcc37",
`❌ | 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 message.channel.send({
embeds: [
embedMessage("#9dcc37", `❌ | You must be in my voice channel!`),
],
});

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

if (!searchSong.tracks.length || !searchSong)
return message.channel.send({
embeds: [embedMessage("#9dcc37", `❌ | Song not found`)],
});

let queue;
client.player.getQueue(message.guild)
? (queue = client.player.getQueue(message.guild))
: (queue = client.player.createQueue(message.guildId, {
leaveOnEnd: false,
leaveOnStop: true,
initialVolume: 80,
leaveOnEmptyCooldown: 60 * 1000 * 3,
bufferingTimeout: 200,
leaveOnEmpty: true,
async onBeforeCreateStream(track, source, _queue) {
if (source === "youtube") {
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()}`,
},
description: `Song: **${searchSong.tracks[0].title}**`,
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],
});
} catch (err) {
client.logger(err.message, "error");
await message.channel.send(
"There was an error playing this song, please try again"
);
}
}

if (queue.playing) {
searchSong.playlist
? await message.channel.send({ embeds: [playlistEmbed, musicEmbed] })
: await message.channel.send({ embeds: [musicEmbed] });
}
},
data: new SlashCommandBuilder()
.setName("play")
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const player = new Player(client);
client.player = player;
client.commands = new Collection();
client.logger = logger;
client.db = new Database(mongourl)
// client.db = new Database(mongourl);

// Register everything...
commandsHelper.registerAllCommands(__dirname + "/commands", client);
commandsHelper.registerAllEvents(__dirname + "/events", client);
playerEvents(client.player);

// Connect to DATABASE
connectDatabase(mongourl, client);
// connectDatabase(mongourl, client);
// ... and go!
client.login(token);

0 comments on commit d2540df

Please sign in to comment.