From 4caad422408ef5cda6220abfa516a04bb9c9d1fd Mon Sep 17 00:00:00 2001 From: Essem Date: Tue, 14 Sep 2021 19:14:44 -0500 Subject: [PATCH] Added support for youtube playlists, replaced multiple fetch requests with lavacord rest helpers --- commands/music/nowplaying.js | 4 ++-- commands/music/play.js | 2 +- utils/soundplayer.js | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/commands/music/nowplaying.js b/commands/music/nowplaying.js index b949c52..d3ee0ae 100644 --- a/commands/music/nowplaying.js +++ b/commands/music/nowplaying.js @@ -1,4 +1,4 @@ -import fetch from "node-fetch"; +import { Rest } from "lavacord"; import format from "format-duration"; import MusicCommand from "../../classes/musicCommand.js"; @@ -11,7 +11,7 @@ class NowPlayingCommand extends MusicCommand { if (!this.message.channel.guild.members.get(this.client.user.id).voiceState.channelID) return "I'm not in a voice channel!"; const player = this.connection.player; if (!player) return "I'm not playing anything!"; - const track = await fetch(`http://${player.node.host}:${player.node.port}/decodetrack?track=${encodeURIComponent(player.track)}`, { headers: { Authorization: player.node.password } }).then(res => res.json()); + const track = await Rest.decode(player.node, player.track); const parts = Math.floor((player.state.position / track.length) * 10); return { "embed": { diff --git a/commands/music/play.js b/commands/music/play.js index bbc4f8a..f19b052 100644 --- a/commands/music/play.js +++ b/commands/music/play.js @@ -10,7 +10,7 @@ class PlayCommand extends MusicCommand { if (!this.args[0]) return "You need to provide what you want to play!"; const query = this.args.join(" ").trim(); const search = urlRegex.test(query) ? query : (searchRegex.test(query) ? query : `ytsearch:${query}`); - return await play(this.client, encodeURIComponent(search), this.message, true); + return await play(this.client, search, this.message, true); } static description = "Plays a song or adds it to the queue"; diff --git a/utils/soundplayer.js b/utils/soundplayer.js index f62bc96..30b3026 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -2,7 +2,7 @@ import * as logger from "./logger.js"; import fetch from "node-fetch"; import fs from "fs"; import format from "format-duration"; -import { Manager } from "lavacord"; +import { Manager, Rest } from "lavacord"; let nodes; @@ -63,11 +63,12 @@ export async function play(client, sound, message, music = false) { if (!music && !nodes.filter(obj => obj.host === node.host)[0].local) { sound = sound.replace(/\.\//, "https://raw.githubusercontent.com/esmBot/esmBot/master/"); } - const { tracks } = await fetch(`http://${node.host}:${node.port}/loadtracks?identifier=${sound}`, { headers: { Authorization: node.password } }).then(res => res.json()); + const { tracks, playlistInfo } = await Rest.load(node, sound); const oldQueue = queues.get(voiceChannel.guild.id); if (!tracks || tracks.length === 0) return "I couldn't find that song!"; if (music) { - queues.set(voiceChannel.guild.id, oldQueue ? [...oldQueue, tracks[0].track] : [tracks[0].track]); + const playlistTracks = tracks.map((val) => { return val.track; }); + queues.set(voiceChannel.guild.id, oldQueue ? [...oldQueue, ...playlistTracks] : playlistTracks); } const connection = await manager.join({ guild: voiceChannel.guild.id, @@ -76,7 +77,7 @@ export async function play(client, sound, message, music = false) { }); if (oldQueue && music) { - return `Your tune \`${tracks[0].info.title}\` has been added to the queue!`; + return `Your ${playlistInfo.name ? "playlist" : "tune"} \`${playlistInfo.name ? playlistInfo.name : tracks[0].info.title}\` has been added to the queue!`; } else { nextSong(client, message, connection, tracks[0].track, tracks[0].info, music, voiceChannel, player ? player.loop : false); return; @@ -165,7 +166,7 @@ export async function nextSong(client, message, connection, track, info, music, // no-op } } else { - const newTrack = await fetch(`http://${connection.node.host}:${connection.node.port}/decodetrack?track=${encodeURIComponent(newQueue[0])}`, { headers: { Authorization: connection.node.password } }).then(res => res.json()); + const newTrack = await Rest.decode(connection.node, newQueue[0]); nextSong(client, message, connection, newQueue[0], newTrack, music, voiceChannel, player.loop, track); try { if (newQueue[0] !== track && playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete();