Comparing version 4.2.1 to 4.2.2
@@ -46,2 +46,7 @@ /// <reference types="node" /> | ||
/** | ||
* Resolve a track | ||
* @param {Track} track - Only for personal use | ||
*/ | ||
private resolveTrack; | ||
/** | ||
* | ||
@@ -48,0 +53,0 @@ * @param options To connect to voice channel |
@@ -12,2 +12,3 @@ "use strict"; | ||
const Response_1 = require("../guild/Response"); | ||
const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
class Player extends events_1.EventEmitter { | ||
@@ -58,2 +59,3 @@ data; | ||
this.data = {}; | ||
this.poru.emit("playerCreate", this); | ||
this.on("playerUpdate", (packet) => { | ||
@@ -78,3 +80,3 @@ (this.isConnected = packet.state.connected), | ||
if (!this.currentTrack.track) | ||
this.currentTrack = await this.currentTrack.resolve(this.poru); | ||
this.currentTrack = await this.resolveTrack(this.currentTrack); | ||
if (this.currentTrack.track) { | ||
@@ -95,2 +97,36 @@ this.node.rest.updatePlayer({ | ||
/** | ||
* Resolve a track | ||
* @param {Track} track - Only for personal use | ||
*/ | ||
async resolveTrack(track) { | ||
// console.log(track) | ||
const query = [track.info?.author, track.info?.title] | ||
.filter((x) => !!x) | ||
.join(" - "); | ||
const result = await this.resolve({ query, source: this.poru.options.defaultPlatform || "ytsearch", requester: track.info?.requester }); | ||
if (!result || !result.tracks.length) | ||
return; | ||
if (track.info?.author) { | ||
const author = [track.info.author, `${track.info.author} - Topic`]; | ||
const officialAudio = result.tracks.find((track) => author.some((name) => new RegExp(`^${escapeRegExp(name)}$`, "i").test(track.info.author)) || | ||
new RegExp(`^${escapeRegExp(track.info.title)}$`, "i").test(track.info.title)); | ||
if (officialAudio) { | ||
track.info.identifier = officialAudio.info.identifier; | ||
track.track = officialAudio.track; | ||
return track; | ||
} | ||
} | ||
if (track.info.length) { | ||
const sameDuration = result.tracks.find((track) => track.info.length >= (track.info.length ? track.info.length : 0) - 2000 && | ||
track.info.length <= (track.info.length ? track.info.length : 0) + 2000); | ||
if (sameDuration) { | ||
track.info.identifier = sameDuration.info.identifier; | ||
track.track = sameDuration.track; | ||
return track; | ||
} | ||
} | ||
track.info.identifier = result.tracks[0].info.identifier; | ||
return track; | ||
} | ||
/** | ||
* | ||
@@ -116,3 +152,2 @@ * @param options To connect to voice channel | ||
stop() { | ||
delete this.currentTrack; | ||
this.position = 0; | ||
@@ -318,2 +353,3 @@ this.isPlaying = false; | ||
return this.stop(); | ||
response.tracks.shift(); | ||
let track = response.tracks[Math.floor(Math.random() * Math.floor(response.tracks.length))]; | ||
@@ -320,0 +356,0 @@ this.queue.push(track); |
@@ -142,2 +142,10 @@ /// <reference types="node" /> | ||
/** | ||
* Emitted when a player got created | ||
* @eventProperty | ||
* @param player | ||
* @returns void | ||
*/ | ||
playerCreate: (player: Player) => void; | ||
/** | ||
* | ||
* Emitted when a player destroy | ||
@@ -144,0 +152,0 @@ * @eventProperty |
@@ -9,3 +9,2 @@ "use strict"; | ||
const Response_1 = require("./guild/Response"); | ||
const Plugin_1 = require("./Plugin"); | ||
class Poru extends events_1.EventEmitter { | ||
@@ -53,4 +52,2 @@ client; | ||
this.options.plugins.forEach((plugin) => { | ||
if (!(plugin instanceof Plugin_1.Plugin)) | ||
throw new RangeError(`Some of your Plugin does not extend Poru's Plugin.`); | ||
plugin.load(this); | ||
@@ -57,0 +54,0 @@ }); |
{ | ||
"name": "poru", | ||
"version": "4.2.1", | ||
"version": "4.2.2", | ||
"description": "A stable and powerful lavalink client around node.js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { Poru, ResolveOptions } from "../Poru"; | ||
import { Node } from "../Node/Node"; | ||
import { Track } from "../guild/Track"; | ||
import { Track, trackData } from "../guild/Track"; | ||
import { Connection } from "./Connection"; | ||
@@ -16,2 +16,3 @@ import Queue from "../guild/Queue"; | ||
type Loop = "NONE" | "TRACK" | "QUEUE"; | ||
const escapeRegExp = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
@@ -65,2 +66,3 @@ export class Player extends EventEmitter { | ||
this.poru.emit("playerCreate", this); | ||
this.on("playerUpdate", (packet) => { | ||
@@ -85,3 +87,3 @@ (this.isConnected = packet.state.connected), | ||
if (!this.currentTrack.track) | ||
this.currentTrack = await this.currentTrack.resolve(this.poru); | ||
this.currentTrack = await this.resolveTrack(this.currentTrack); | ||
if (this.currentTrack.track) { | ||
@@ -100,2 +102,54 @@ this.node.rest.updatePlayer({ | ||
} | ||
/** | ||
* Resolve a track | ||
* @param {Track} track - Only for personal use | ||
*/ | ||
private async resolveTrack(track:Track) { | ||
// console.log(track) | ||
const query = [track.info?.author, track.info?.title] | ||
.filter((x) => !!x) | ||
.join(" - "); | ||
const result: any = await this.resolve({ query, source: this.poru.options.defaultPlatform || "ytsearch", requester: track.info?.requester }); | ||
if (!result || !result.tracks.length) return; | ||
if (track.info?.author) { | ||
const author = [track.info.author, `${track.info.author} - Topic`]; | ||
const officialAudio = result.tracks.find( | ||
(track) => | ||
author.some((name) => | ||
new RegExp(`^${escapeRegExp(name)}$`, "i").test(track.info.author) | ||
) || | ||
new RegExp(`^${escapeRegExp(track.info.title)}$`, "i").test( | ||
track.info.title | ||
) | ||
); | ||
if (officialAudio) { | ||
track.info.identifier = officialAudio.info.identifier; | ||
track.track = officialAudio.track; | ||
return track; | ||
} | ||
} | ||
if (track.info.length) { | ||
const sameDuration = result.tracks.find( | ||
(track: trackData) => | ||
track.info.length >= (track.info.length ? track.info.length : 0) - 2000 && | ||
track.info.length <= (track.info.length ? track.info.length : 0) + 2000 | ||
); | ||
if (sameDuration) { | ||
track.info.identifier = sameDuration.info.identifier; | ||
track.track = sameDuration.track; | ||
return track; | ||
} | ||
} | ||
track.info.identifier = result.tracks[0].info.identifier; | ||
return track; | ||
} | ||
/** | ||
@@ -126,3 +180,2 @@ * | ||
public stop() { | ||
delete this.currentTrack; | ||
this.position = 0; | ||
@@ -292,3 +345,3 @@ this.isPlaying = false; | ||
} | ||
public restart() { | ||
@@ -351,2 +404,5 @@ if (!this.currentTrack.track && !this.queue.length) return; | ||
return this.stop(); | ||
response.tracks.shift(); | ||
let track = | ||
@@ -353,0 +409,0 @@ response.tracks[ |
@@ -158,3 +158,15 @@ import { Node } from "./Node/Node"; | ||
/** | ||
* Emitted when a player got created | ||
* @eventProperty | ||
* @param player | ||
* @returns void | ||
*/ | ||
playerCreate: (player: Player) => void; | ||
/** | ||
* | ||
* Emitted when a player destroy | ||
@@ -244,7 +256,3 @@ * @eventProperty | ||
this.options.plugins.forEach((plugin) => { | ||
if (!(plugin instanceof Plugin)) | ||
throw new RangeError( | ||
`Some of your Plugin does not extend Poru's Plugin.` | ||
); | ||
plugin.load(this); | ||
@@ -251,0 +259,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2624074
5351