UNPKG

leref.ts

Version:

Something upcoming for aoi.js and npm packages uses

1 lines 7.12 kB
"use strict";var __decorate=this&&this.__decorate||function(e,t,s,o){var i,r=arguments.length,n=r<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,s,o);else for(var a=e.length-1;0<=a;a--)(i=e[a])&&(n=(r<3?i(n):3<r?i(t,s,n):i(t,s))||n);return 3<r&&n&&Object.defineProperty(t,s,n),n};Object.defineProperty(exports,"__esModule",{value:!0}),exports.LerefPlayer=void 0;const validators_1=require("../utils/decorators/validators"),constructs_1=require("../utils/decorators/constructs"),constants_1=require("../utils/constants"),lerefTracking_1=require("./lerefTracking"),rest_1=require("../utils/rest"),filterUtils_1=require("../utils/filterUtils");let LerefPlayer=class{constructor(e,t){this.position=0,this.replaying=!1,this.voiceConnected=!1,this.lastUpdated=Date.now(),this.filters={},this.loop=constants_1.LoopMode.None,this.state=constants_1.PlayerStates.Paused,this.voiceState=constants_1.PlayerVoiceStates.Disconnected,this.voice=Object.create(null),this.lava=e,this.queue=new e.options.structures.Queue,this.options={volume:100,selfMute:!1,selfDeaf:!0,node:this.lava.leastLoadNode?.options.name,...t},this.lava.players.set(this.options.guildID,this),this.lava.emit("playerCreate",this),this.setVolume()}get node(){var e=this.lava.nodes.get(this.options.node);return e&&e.connected||console.log("No node is connected!"),e}setNode(e){try{this.node.send({op:constants_1.OpCodes.Destroy,guildId:this.options.guildID})}catch{}this.options.node=e,["op","guildId","event","sessionId"].every(e=>e in this.voice)&&this.node.send(this.voice),this.queue.current&&this.resolveCurrent().then(()=>{var e={op:constants_1.OpCodes.Play,...this.playOptions,guildId:this.options.guildID,track:this.queue.current.base64,startTime:this.position};this.lava.emit("playerReplay",this),this.node.send(e),this.replaying=!0})}connect(){this.voiceState=constants_1.PlayerVoiceStates.Connecting,this.lava.options.send(this.options.guildID,{op:4,d:{guild_id:this.options.guildID,channel_id:this.options.voiceID,self_mute:this.options.selfMute,self_deaf:this.options.selfDeaf}}),this.voiceState=constants_1.PlayerVoiceStates.Connected}disconnect(){this.voiceState=constants_1.PlayerVoiceStates.Disconnecting,this.pause(!0),this.lava.options.send(this.options.guildID,{op:4,d:{guild_id:this.options.guildID,channel_id:null,self_mute:!1,self_deaf:!1}}),this.voiceState=constants_1.PlayerVoiceStates.Disconnected}async play(t){var s=this.queue.previous;if(this.loop===constants_1.LoopMode.Track||this.loop===constants_1.LoopMode.Queue&&!this.queue.length?this.queue.previous=this.queue.current:(this.queue.progress(),this.loop===constants_1.LoopMode.Queue&&this.queue.add(this.queue.previous)),this.queue.current){try{await this.resolveCurrent()}catch(e){return this.lava.emit("trackError",this,this.queue.current,e),this.queue.current=this.queue.previous,this.queue.previous=s,void(this.queue.length&&await this.play(t))}t={op:constants_1.OpCodes.Play,guildId:this.options.guildID,track:this.queue.current.base64,startTime:t.startTime,endTime:t.endTime};await this.node.send(t)}}patchFilters(){var e={op:constants_1.OpCodes.Filters,guildId:this.options.guildID,...this.filters};this.node.send(e)}setFilters(e){this.filters=e instanceof filterUtils_1.FilterUtils?e.build():e}setVolume(e){this.options.volume=Math.max(Math.min(e,1e3),0);e={op:constants_1.OpCodes.Volume,guildId:this.options.guildID,volume:this.options.volume};this.node.send(e)}setLoop(e){this.loop=e}stop(e){"number"==typeof e&&1<e&&this.queue.splice(0,e-1);e={op:constants_1.OpCodes.Stop,guildId:this.options.guildID};this.node.send(e)}pause(e){this.state=e?constants_1.PlayerStates.Paused:constants_1.PlayerStates.Playing;e={op:constants_1.OpCodes.Pause,guildId:this.options.guildID,pause:e};this.node.send(e)}seek(e){this.position=e;e={op:constants_1.OpCodes.Seek,guildId:this.options.guildID,position:e};this.node.send(e)}destroy(){this.state=constants_1.PlayerStates.Destroyed,this.disconnect(),this.node.send({op:constants_1.OpCodes.Destroy,guildId:this.options.guildID}),this.lava.players.delete(this.options.guildID),this.lava.emit("playerDestroy",this)}set(e,t){this.options.metadata&&(this.options.metadata[e]=t)}get(e){return this.options.metadata?.[e]}async resolveCurrent(){const t=this.queue.current;if(lerefTracking_1.UnresolvedTrack.isUnresolved(t)){var e=[t.author,t.title].filter(e=>e).join(" - ");const o=await this.lava.search({query:e},t.requester);if(o.loadType!==rest_1.LoadTypes.SearchResult)throw o.error??{message:"No tracks found.",severity:"COMMON"};if(t.author){const i=[t.author,t.author+" - Topic"];var s=o.tracks.find(t=>i.some(e=>e===t.author||e===t.title));if(s)return void(this.queue.current=s)}if(t.duration){s=o.tracks.find(e=>e.duration>=t.duration-1500&&e.duration<=t.duration+1500);if(s)return void(this.queue.current=s)}this.queue.current=o.tracks[0]}}};__decorate([validators_1.check(function(e,t){if(this.options.node!==t)return e(t)})],LerefPlayer.prototype,"setNode",null),__decorate([validators_1.check(function(e){if(!this.options.voiceID)throw new Error("No voice channel has been set");return e()})],LerefPlayer.prototype,"connect",null),__decorate([validators_1.check(function(e){if(this.voiceState===constants_1.PlayerVoiceStates.Connected)return e()})],LerefPlayer.prototype,"disconnect",null),__decorate([validators_1.check(function(e,t){if("object"!=typeof t||null===t)throw new TypeError("Parameter 'options' must be present and be an object");return e(t)})],LerefPlayer.prototype,"play",null),__decorate([validators_1.check(function(e){if(this.queue.current)return e()})],LerefPlayer.prototype,"patchFilters",null),__decorate([validators_1.check(function(e,t){if("object"!=typeof t||null===t)throw new TypeError("Parameter 'filters' must be present and be an object");return e(t)})],LerefPlayer.prototype,"setFilters",null),__decorate([validators_1.check(function(e,t){return t=Number(t),e(t=isNaN(t)?this.options.volume:t)})],LerefPlayer.prototype,"setVolume",null),__decorate([validators_1.check(function(e,t){return e(t=t<constants_1.LoopMode.None||t>constants_1.LoopMode.Queue?constants_1.LoopMode.None:t)})],LerefPlayer.prototype,"setLoop",null),__decorate([validators_1.check(function(e,t){if("number"==typeof t&&1<t&&t>this.queue.length)throw new RangeError("Cannot skip more than the queue length.");return e(t)})],LerefPlayer.prototype,"stop",null),__decorate([validators_1.check(function(e,t){if("boolean"!=typeof t)throw new TypeError("Parameter 'pause' must be present and be a boolean");if((!t||this.state!==constants_1.PlayerStates.Paused)&&this.queue.totalSize)return e(t)})],LerefPlayer.prototype,"pause",null),__decorate([validators_1.check(function(e,t){if(this.queue.current){if(t=Number(t),isNaN(t))throw new TypeError("Parameter 'position' must be present and be a number");return e(t=t<0||t>this.queue.current.duration?Math.max(Math.min(t,this.queue.current.duration),0):t)}})],LerefPlayer.prototype,"seek",null),LerefPlayer=__decorate([constructs_1.constructPlayer()],LerefPlayer),exports.LerefPlayer=LerefPlayer;