Added support for youtube playlists, replaced multiple fetch requests with lavacord rest helpers

This commit is contained in:
Essem 2021-09-14 19:14:44 -05:00
parent c59a0bf0c4
commit 4caad42240
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
3 changed files with 9 additions and 8 deletions

View file

@ -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": {

View file

@ -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";

View file

@ -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();