11 lines
36 KiB
JavaScript
11 lines
36 KiB
JavaScript
![]() |
var ze=Object.create;var M=Object.defineProperty;var Ye=Object.getOwnPropertyDescriptor;var Xe=Object.getOwnPropertyNames;var Ze=Object.getPrototypeOf,Qe=Object.prototype.hasOwnProperty;var et=(i,e,t)=>e in i?M(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var re=i=>M(i,"__esModule",{value:!0});var tt=(i,e)=>()=>(e||i((e={exports:{}}).exports,e),e.exports),it=(i,e)=>{re(i);for(var t in e)M(i,t,{get:e[t],enumerable:!0})},st=(i,e,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Xe(e))!Qe.call(i,s)&&s!=="default"&&M(i,s,{get:()=>e[s],enumerable:!(t=Ye(e,s))||t.enumerable});return i},h=i=>st(re(M(i!=null?ze(Ze(i)):{},"default",i&&i.__esModule&&"default"in i?{get:()=>i.default,enumerable:!0}:{value:i,enumerable:!0})),i);var r=(i,e,t)=>(et(i,typeof e!="symbol"?e+"":e,t),t);var Fe=tt((Li,Pt)=>{Pt.exports={name:"@discordjs/voice",version:"0.7.4",description:"Implementation of the Discord Voice API for Node.js",scripts:{pretest:"npm run build",test:"jest --pass-with-no-tests","test:ci":"jest --no-stack-trace --verbose --pass-with-no-tests",prebuild:"npm run lint",build:"tsup",postbuild:"node scripts/postbuild.mjs",lint:"eslint src --ext mjs,js,ts","lint:fix":"eslint src --ext mjs,js,ts --fix",format:"prettier --write **/*.{ts,js,json,yml,yaml}",prepare:"is-ci || husky install",docs:"typedoc --json docs/typedoc-out.json src/index.ts && node scripts/docs.mjs",prepublishOnly:"npm run lint && npm run test",release:"standard-version --preset angular"},main:"./dist/index.js",module:"./dist/index.mjs",typings:"./dist/index.d.ts",exports:{import:"./dist/index.mjs",require:"./dist/index.js"},directories:{lib:"src",test:"__tests__"},files:["dist"],author:"Amish Shah <amish@shah.gg>",license:"Apache-2.0",keywords:["discord","discord.js","audio","voice","streaming"],repository:{type:"git",url:"git+https://github.com/discordjs/voice.git"},bugs:{url:"https://github.com/discordjs/voice/issues"},homepage:"https://github.com/discordjs/voice",dependencies:{"@types/ws":"^8.2.0","discord-api-types":"^0.24.0","prism-media":"^1.3.2","tiny-typed-emitter":"^2.1.0",tslib:"^2.3.1",ws:"^8.2.3"},devDependencies:{"@babel/core":"^7.16.0","@babel/preset-env":"^7.16.0","@babel/preset-typescript":"^7.16.0","@commitlint/cli":"^14.1.0","@commitlint/config-angular":"^14.1.0","@discordjs/ts-docgen":"^0.3.2","@types/jest":"^27.0.2","@types/node":"^16.11.7","@typescript-eslint/eslint-plugin":"^5.3.1","@typescript-eslint/parser":"^5.3.1",eslint:"^8.2.0","eslint-config-marine":"^9.0.6","eslint-config-prettier":"^8.3.0","eslint-plugin-prettier":"^4.0.0",husky:"^7.0.4","is-ci":"^3.0.1",jest:"^27.3.1","jest-websocket-mock":"^2.2.1","lint-staged":"^11.2.6","mock-socket":"^9.0.7",prettier:"^2.4.1","standard-version":"^9.3.2",tsup:"^5.7.0",typedoc:"^0.22.8",typescript:"^4.4.4"},engines:{node:">=16.0.0",npm:">=7.0.0"},publishConfig:{access:"public"}}});it(exports,{AudioPlayer:()=>H,AudioPlayerError:()=>N,AudioPlayerStatus:()=>d,AudioReceiveStream:()=>J,AudioResource:()=>j,EndBehaviorType:()=>I,NoSubscriberBehavior:()=>x,PlayerSubscription:()=>W,SSRCMap:()=>L,SpeakingMap:()=>T,StreamType:()=>p,VoiceConnection:()=>K,VoiceConnectionDisconnectReason:()=>y,VoiceConnectionStatus:()=>c,VoiceReceiver:()=>q,createAudioPlayer:()=>De,createAudioResource:()=>je,createDefaultAudioReceiveStreamOptions:()=>ie,demuxProbe:()=>xt,entersState:()=>wt,generateDependencyReport:()=>Ct,getGroups:()=>ce,getVoiceConnection:()=>B,getVoiceConnections:()=>G,joinVoiceChannel:()=>gt,validateDiscordOpusHead:()=>qe});var ae=h(require("discord-api-types/v9"));function R(i){return{op:ae.GatewayOpcodes.VoiceStateUpdate,d:{guild_id:i.guildId,channel_id:i.channelId,self_deaf:i.selfDeaf,self_mute:i.selfMute}}}var U=new Map;U.set("default",new Map);function ot(i){let e=U.get(i);if(e)return e;let t=new Map;return U.set(i,t),t}function ce(){return U}function G(i="default"){return U.get(i)}function B(i,e="default"){return G(e)?.get(i)}function de(i){return G(i.joinConfig.group)?.delete(i.joinConfig.guildId)}function ue(i){return ot(i.joinConfig.group).set(i.joinConfig.guildId,i)}var n
|
||
|
- Install sodium, libsodium-wrappers, or tweetnacl.
|
||
|
- Use the generateDependencyReport() function for more information.
|
||
|
`)},C={open:ee,close:ee,random:ee};(async()=>{for(let i of Object.keys(ke))try{let e=require(i);i==="libsodium-wrappers"&&e.ready&&await e.ready,Object.assign(C,ke[i](e));break}catch{}})();var g=()=>{};var ve=h(require("tiny-typed-emitter")),pt=2,lt=48e3/100*pt,ft=2**32-1,ht=["xsalsa20_poly1305_lite","xsalsa20_poly1305_suffix","xsalsa20_poly1305"],l;(function(a){a[a.OpeningWs=0]="OpeningWs",a[a.Identifying=1]="Identifying",a[a.UdpHandshaking=2]="UdpHandshaking",a[a.SelectingProtocol=3]="SelectingProtocol",a[a.Ready=4]="Ready",a[a.Resuming=5]="Resuming",a[a.Closed=6]="Closed"})(l||(l={}));var Pe=Buffer.alloc(24),te=class extends ve.TypedEmitter{constructor(e,t){super();r(this,"_state");r(this,"debug");this.onWsOpen=this.onWsOpen.bind(this),this.onChildError=this.onChildError.bind(this),this.onWsPacket=this.onWsPacket.bind(this),this.onWsClose=this.onWsClose.bind(this),this.onWsDebug=this.onWsDebug.bind(this),this.onUdpDebug=this.onUdpDebug.bind(this),this.onUdpClose=this.onUdpClose.bind(this),this.debug=t?s=>this.emit("debug",s):null,this._state={code:0,ws:this.createWebSocket(e.endpoint),connectionOptions:e}}destroy(){this.state={code:6}}get state(){return this._state}set state(e){let t=Reflect.get(this._state,"ws"),s=Reflect.get(e,"ws");t&&t!==s&&(t.off("debug",this.onWsDebug),t.on("error",g),t.off("error",this.onChildError),t.off("open",this.onWsOpen),t.off("packet",this.onWsPacket),t.off("close",this.onWsClose),t.destroy());let n=Reflect.get(this._state,"udp"),o=Reflect.get(e,"udp");n&&n!==o&&(n.on("error",g),n.off("error",this.onChildError),n.off("close",this.onUdpClose),n.off("debug",this.onUdpDebug),n.destroy());let u=this._state;this._state=e,this.emit("stateChange",u,e),this.debug?.(`state change:
|
||
|
from ${Ae(u)}
|
||
|
to ${Ae(e)}`)}createWebSocket(e){let t=new Q(`wss://${e}?v=4`,Boolean(this.debug));return t.on("error",this.onChildError),t.once("open",this.onWsOpen),t.on("packet",this.onWsPacket),t.once("close",this.onWsClose),t.on("debug",this.onWsDebug),t}onChildError(e){this.emit("error",e)}onWsOpen(){if(this.state.code===0){let e={op:k.VoiceOpcodes.Identify,d:{server_id:this.state.connectionOptions.serverId,user_id:this.state.connectionOptions.userId,session_id:this.state.connectionOptions.sessionId,token:this.state.connectionOptions.token}};this.state.ws.sendPacket(e),this.state={...this.state,code:1}}else if(this.state.code===5){let e={op:k.VoiceOpcodes.Resume,d:{server_id:this.state.connectionOptions.serverId,session_id:this.state.connectionOptions.sessionId,token:this.state.connectionOptions.token}};this.state.ws.sendPacket(e)}}onWsClose({code:e}){(e===4015||e<4e3)&&this.state.code===4?this.state={...this.state,code:5,ws:this.createWebSocket(this.state.connectionOptions.endpoint)}:this.state.code!==6&&(this.destroy(),this.emit("close",e))}onUdpClose(){this.state.code===4&&(this.state={...this.state,code:5,ws:this.createWebSocket(this.state.connectionOptions.endpoint)})}onWsPacket(e){if(e.op===k.VoiceOpcodes.Hello&&this.state.code!==6)this.state.ws.setHeartbeatInterval(e.d.heartbeat_interval);else if(e.op===k.VoiceOpcodes.Ready&&this.state.code===1){let{ip:t,port:s,ssrc:n,modes:o}=e.d,u=new X({ip:t,port:s});u.on("error",this.onChildError),u.on("debug",this.onUdpDebug),u.once("close",this.onUdpClose),u.performIPDiscovery(n).then(a=>{this.state.code===2&&(this.state.ws.sendPacket({op:k.VoiceOpcodes.SelectProtocol,d:{protocol:"udp",data:{address:a.ip,port:a.port,mode:mt(o)}}}),this.state={...this.state,code:3})}).catch(a=>this.emit("error",a)),this.state={...this.state,code:2,udp:u,connectionData:{ssrc:n}}}else if(e.op===k.VoiceOpcodes.SessionDescription&&this.state.code===3){let{mode:t,secret_key:s}=e.d;this.state={...this.state,code:4,connectionData:{...this.state.connectionData,encryptionMode:t,secretKey:new Uint8Array(s),sequence:Ce(16),timestamp:Ce(32),nonce:0,nonceBuffer:Buffer.alloc(24),speaking:!1,packetsPlayed:0}}}else e.op===k.VoiceOpcodes.Resumed&&this.state.code===5&&(this.state={...this.state,code:4},this.state.connectionData.speaking=!1)}onWsDebug(e){this.debug?.(`[WS] ${e}`)}onUdpDebug(e){this.debug?.(`[UDP] ${e}`)}prepareAudioPacket(e){let t=this.state;if(t.code===4)return t.preparedPacket=this.createAudioPacket(e,t.connectionData),t.preparedPacket}dispatchAudio(){let e=this.state;return e.code!==4?!1:typeof e.preparedPacket!="undefined"?(this.playAudioPacket(e.preparedPacket),e.preparedPacket=void 0,!0):!1}playAudioPacket(e){let t=this.state;if(t.code!==4)return;let{connectionData:s}=t;s.packetsPlayed++,s.sequence++,s.timestamp+=lt,s.sequence>=2**16&&(s.sequence=0),s.timestamp>=2**32&&(s.timestamp=0),this.setSpeaking(!0),t.udp.send(e)}setSpeaking(e){let t=this.state;t.code===4&&t.connectionData.speaking!==e&&(t.connectionData.speaking=e,t.ws.sendPacket({op:k.VoiceOpcodes.Speaking,d:{speaking:e?1:0,delay:0,ssrc:t.connectionData.ssrc}}))}createAudioPacket(e,t){let s=Buffer.alloc(12);s[0]=128,s[1]=120;let{sequence:n,timestamp:o,ssrc:u}=t;return s.writeUIntBE(n,2,2),s.writeUIntBE(o,4,4),s.writeUIntBE(u,8,4),s.copy(Pe,0,0,12),Buffer.concat([s,...this.encryptOpusPacket(e,t)])}encryptOpusPacket(e,t){let{secretKey:s,encryptionMode:n}=t;if(n==="xsalsa20_poly1305_lite")return t.nonce++,t.nonce>ft&&(t.nonce=0),t.nonceBuffer.writeUInt32BE(t.nonce,0),[C.close(e,t.nonceBuffer,s),t.nonceBuffer.slice(0,4)];if(n==="xsalsa20_poly1305_suffix"){let o=C.random(24,t.nonceBuffer);return[C.close(e,o,s),o]}return[C.close(e,Pe,s)]}};function Ce(i){return Math.floor(Math.random()*2**i)}function Ae(i){return JSON.stringify({...i,ws:Reflect.has(i,"ws"),udp:Reflect.has(i,"udp")})}function mt(i){let e=i.find(t=>ht.includes(t));if(!e)throw new Error(`No compatible encryption modes. Available include: ${i.join(", ")}`);return e}var Oe=h(require("tiny-typed-emitter"));var $=h(require("discord-api-types/voice/v4"));var Ee=h(require("node:stream"))
|
||
|
from ${xe(t)}
|
||
|
to ${xe(e)}`)}play(e){if(e.ended)throw new Error("Cannot play a resource that has already ended.");if(e.audioPlayer){if(e.audioPlayer===this)return;throw new Error("Resource is already being played by another audio player.")}e.audioPlayer=this;let t=s=>{this.state.status!==d.Idle&&this.emit("error",new N(s,this.state.resource)),this.state.status!==d.Idle&&this.state.resource===e&&(this.state={status:d.Idle})};if(e.playStream.once("error",t),e.started)this.state={status:d.Playing,missedFrames:0,playbackDuration:0,resource:e,onStreamError:t};else{let s=()=>{this.state.status===d.Buffering&&this.state.resource===e&&(this.state={status:d.Playing,missedFrames:0,playbackDuration:0,resource:e,onStreamError:t})},n=()=>{this.state.status===d.Buffering&&this.state.resource===e&&(this.state={status:d.Idle})};e.playStream.once("readable",s),e.playStream.once("end",n),e.playStream.once("close",n),e.playStream.once("finish",n),this.state={status:d.Buffering,resource:e,onReadableCallback:s,onFailureCallback:n,onStreamError:t}}}pause(e=!0){return this.state.status!==d.Playing?!1:(this.state={...this.state,status:d.Paused,silencePacketsRemaining:e?5:0},!0)}unpause(){return this.state.status!==d.Paused?!1:(this.state={...this.state,status:d.Playing,missedFrames:0},!0)}stop(e=!1){return this.state.status===d.Idle?!1:(e||this.state.resource.silencePaddingFrames===0?this.state={status:d.Idle}:this.state.resource.silenceRemaining===-1&&(this.state.resource.silenceRemaining=this.state.resource.silencePaddingFrames),!0)}checkPlayable(){let e=this._state;return e.status===d.Idle||e.status===d.Buffering?!1:e.resource.readable?!0:(this.state={status:d.Idle},!1)}_stepDispatch(){let e=this._state;e.status===d.Idle||e.status===d.Buffering||this.playable.forEach(t=>t.dispatchAudio())}_stepPrepare(){let e=this._state;if(e.status===d.Idle||e.status===d.Buffering)return;let t=this.playable;if(e.status===d.AutoPaused&&t.length>0&&(this.state={...e,status:d.Playing,missedFrames:0}),e.status===d.Paused||e.status===d.AutoPaused){e.silencePacketsRemaining>0&&(e.silencePacketsRemaining--,this._preparePacket(O,t,e),e.silencePacketsRemaining===0&&this._signalStopSpeaking());return}if(t.length===0)if(this.behaviors.noSubscriber===x.Pause){this.state={...e,status:d.AutoPaused,silencePacketsRemaining:5};return}else this.behaviors.noSubscriber===x.Stop&&this.stop(!0);let s=e.resource.read();e.status===d.Playing&&(s?(this._preparePacket(s,t,e),e.missedFrames=0):(this._preparePacket(O,t,e),e.missedFrames++,e.missedFrames>=this.behaviors.maxMissedFrames&&this.stop()))}_signalStopSpeaking(){return this.subscribers.forEach(({connection:e})=>e.setSpeaking(!1))}_preparePacket(e,t,s){s.playbackDuration+=20,t.forEach(n=>n.prepareAudioPacket(e))}};function xe(i){return JSON.stringify({...i,resource:Reflect.has(i,"resource"),stepTimeout:Reflect.has(i,"stepTimeout")})}function De(i){return new H(i)}var I;(function(s){s[s.Manual=0]="Manual",s[s.AfterSilence=1]="AfterSilence",s[s.AfterInactivity=2]="AfterInactivity"})(I||(I={}));function ie(){return{end:{behavior:0}}}var J=class extends Ee.Readable{constructor({end:e,...t}){super({...t,objectMode:!0});r(this,"end");r(this,"endTimeout");this.end=e}push(e){return e&&(this.end.behavior===2||this.end.behavior===1&&(e.compare(O)!==0||typeof this.endTimeout=="undefined"))&&this.renewEndTimeout(this.end),super.push(e)}renewEndTimeout(e){this.endTimeout&&clearTimeout(this.endTimeout),this.endTimeout=setTimeout(()=>this.push(null),e.duration)}_read(){}};var Re=h(require("tiny-typed-emitter")),se=class extends Re.TypedEmitter{constructor(){super();r(this,"users");r(this,"speakingTimeouts");this.users=new Map,this.speakingTimeouts=new Map}onPacket(e){let t=this.speakingTimeouts.get(e);t?clearTimeout(t):(this.users.set(e,Date.now()),this.emit("start",e)),this.startTimeout(e)}startTimeout(e){this.speakingTimeouts.set(e,setTimeout(()=>{this.emit("end",e),this.speakingTimeouts.delete(e),this.users.delete(e)},se.DELAY))}},T=se;r(T,"DELAY",100);var Ve=h(require("tiny-typed-emitter")),L=class extends Ve.TypedEmitter{constructor(){super();r(this,"
|
||
|
`)}function He(i,e,t){if(t===0)return;let s=(0,F.resolve)(i,"./package.json");try{let n=require(s);if(n.name!==e)throw new Error("package.json does not match");return n}catch{return He((0,F.resolve)(i,".."),e,t-1)}}function At(i){try{return(i==="@discordjs/voice"?Fe():He((0,F.dirname)(require.resolve(i)),i,3))?.version??"not found"}catch{return"not found"}}function Je(i){let e=new AbortController,t=setTimeout(()=>e.abort(),i);return e.signal.addEventListener("abort",()=>clearTimeout(t)),[e,e.signal]}var Le=h(require("node:events"));async function wt(i,e,t){if(i.state.status!==e){let[s,n]=typeof t=="number"?Je(t):[void 0,t];try{await(0,Le.once)(i,e,{signal:n})}finally{s?.abort()}}return i}var $e=h(require("node:stream")),ne=h(require("prism-media"));function qe(i){let e=i.readUInt8(9),t=i.readUInt32LE(12);return e===2&&t===48e3}function xt(i,e=1024,t=qe){return new Promise((s,n)=>{i.readableObjectMode&&n(new Error("Cannot probe a readable stream in object mode")),i.readableEnded&&n(new Error("Cannot probe a stream that has ended"));let o=Buffer.alloc(0),u,a=S=>{i.off("data",z),i.off("close",_),i.off("end",_),i.pause(),u=S,i.readableEnded?s({stream:$e.Readable.from(o),type:S}):(o.length>0&&i.push(o),s({stream:i,type:S}))},P=S=>Ke=>{t(Ke)&&a(S)},D=new ne.default.opus.WebmDemuxer;D.once("error",g),D.on("head",P(p.WebmOpus));let E=new ne.default.opus.OggDemuxer;E.once("error",g),E.on("head",P(p.OggOpus));let _=()=>{u||a(p.Arbitrary)},z=S=>{o=Buffer.concat([o,S]),D.write(S),E.write(S),o.length>=e&&(i.off("data",z),i.pause(),process.nextTick(_))};i.once("error",n),i.on("data",z),i.once("close",_),i.once("end",_)})}0&&(module.exports={AudioPlayer,AudioPlayerError,AudioPlayerStatus,AudioReceiveStream,AudioResource,EndBehaviorType,NoSubscriberBehavior,PlayerSubscription,SSRCMap,SpeakingMap,StreamType,VoiceConnection,VoiceConnectionDisconnectReason,VoiceConnectionStatus,VoiceReceiver,createAudioPlayer,createAudioResource,createDefaultAudioReceiveStreamOptions,demuxProbe,entersState,generateDependencyReport,getGroups,getVoiceConnection,getVoiceConnections,joinVoiceChannel,validateDiscordOpusHead});
|
||
|
//# sourceMappingURL=index.js.map
|