discord-player 6.0.0-dev.4 → 6.0.0-dev.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -428,6 +428,12 @@ var Playlist = class {
428
428
  *[Symbol.iterator]() {
429
429
  yield* this.tracks;
430
430
  }
431
+ get estimatedDuration() {
432
+ return this.tracks.reduce((p, c) => p + c.durationMS, 0);
433
+ }
434
+ get durationFormatted() {
435
+ return Util.buildTimeCode(Util.parseMS(this.estimatedDuration));
436
+ }
431
437
  toJSON(withTracks = true) {
432
438
  const payload = {
433
439
  id: this.id,
@@ -466,6 +472,12 @@ var GuildQueueHistory = class {
466
472
  get disabled() {
467
473
  return this.queue.options.disableHistory;
468
474
  }
475
+ get size() {
476
+ return this.tracks.size;
477
+ }
478
+ getSize() {
479
+ return this.size;
480
+ }
469
481
  isEmpty() {
470
482
  return this.tracks.size < 1;
471
483
  }
@@ -485,14 +497,14 @@ var GuildQueueHistory = class {
485
497
  }
486
498
  this.queue.node.skip();
487
499
  }
488
- async previous() {
500
+ async previous(preserveCurrent = true) {
489
501
  const track = this.tracks.dispatch();
490
502
  if (!track) {
491
503
  throw new Error("No previous track in the queue");
492
504
  }
493
505
  const current = this.currentTrack;
494
506
  await this.queue.node.play(track, { queue: false });
495
- if (current)
507
+ if (current && preserveCurrent)
496
508
  this.queue.node.insert(current, 0);
497
509
  }
498
510
  back() {
@@ -750,6 +762,7 @@ var GuildQueuePlayerNode = class {
750
762
  if (!foundTrack)
751
763
  return null;
752
764
  this.queue.tracks.removeOne((t) => t.id === foundTrack.id);
765
+ this.queue.player.events.emit("audioTrackRemove", this.queue, foundTrack);
753
766
  return foundTrack;
754
767
  }
755
768
  jump(track) {
@@ -776,15 +789,20 @@ var GuildQueuePlayerNode = class {
776
789
  const removed = this.remove(idx);
777
790
  if (!removed)
778
791
  return false;
792
+ const toRemove = this.queue.tracks.store.filter((_, i) => i <= idx);
779
793
  this.queue.tracks.store.splice(0, idx, removed);
794
+ this.queue.player.events.emit("audioTracksRemove", this.queue, toRemove);
780
795
  return this.skip();
781
796
  }
782
797
  insert(track, index = 0) {
783
798
  if (!(track instanceof Track))
784
799
  throw new Error("invalid track");
785
800
  this.queue.tracks.store.splice(index, 0, track);
801
+ this.queue.player.events.emit("audioTrackAdd", this.queue, track);
786
802
  }
787
803
  stop(force = false) {
804
+ this.queue.tracks.clear();
805
+ this.queue.history.clear();
788
806
  if (!this.queue.dispatcher)
789
807
  return false;
790
808
  this.queue.dispatcher.end();
@@ -820,7 +838,7 @@ var GuildQueuePlayerNode = class {
820
838
  );
821
839
  if (res && options.queue) {
822
840
  this.queue.debug("Requested option requires to queue the track, adding the given track to queue instead...");
823
- return this.queue.tracks.add(res);
841
+ return this.queue.addTrack(res);
824
842
  }
825
843
  const track = res || this.queue.tracks.dispatch();
826
844
  if (!track) {
@@ -849,7 +867,7 @@ var GuildQueuePlayerNode = class {
849
867
  this.queue.player.events.emit("playerSkip", this.queue, track);
850
868
  this.queue.player.events.emit("playerError", this.queue, error, track);
851
869
  this.queue.initializing = false;
852
- this.play(this.queue.tracks.dispatch());
870
+ this.play(this.queue.tracks.dispatch(), { queue: false });
853
871
  return;
854
872
  }
855
873
  throw error;
@@ -1244,7 +1262,7 @@ var GuildQueueStatistics = class {
1244
1262
  memoryUsage: process.memoryUsage(),
1245
1263
  versions: {
1246
1264
  node: process.version,
1247
- player: "6.0.0-dev.4"
1265
+ player: "6.0.0-dev.5"
1248
1266
  }
1249
1267
  };
1250
1268
  }
@@ -1306,6 +1324,12 @@ var GuildQueue = class {
1306
1324
  }
1307
1325
  this.debug(`GuildQueue initialized for guild ${this.options.guild.name} (ID: ${this.options.guild.id})`);
1308
1326
  }
1327
+ get estimatedDuration() {
1328
+ return this.tracks.store.reduce((a, c) => a + c.durationMS, 0);
1329
+ }
1330
+ get durationFormatted() {
1331
+ return Util.buildTimeCode(Util.parseMS(this.estimatedDuration));
1332
+ }
1309
1333
  get voiceReceiver() {
1310
1334
  return this.dispatcher?.receiver ?? null;
1311
1335
  }
@@ -1365,6 +1389,16 @@ var GuildQueue = class {
1365
1389
  setRepeatMode(mode) {
1366
1390
  this.repeatMode = mode;
1367
1391
  }
1392
+ get size() {
1393
+ return this.tracks.size;
1394
+ }
1395
+ getSize() {
1396
+ return this.size;
1397
+ }
1398
+ clear() {
1399
+ this.tracks.clear();
1400
+ this.history.clear();
1401
+ }
1368
1402
  isEmpty() {
1369
1403
  return this.tracks.size < 1;
1370
1404
  }
@@ -1376,7 +1410,6 @@ var GuildQueue = class {
1376
1410
  this.tracks.add(toAdd);
1377
1411
  const isMulti = Array.isArray(toAdd);
1378
1412
  if (isMulti) {
1379
- this.player.events.emit("audioTrackAdd", this, toAdd[0]);
1380
1413
  this.player.events.emit("audioTracksAdd", this, toAdd);
1381
1414
  } else {
1382
1415
  this.player.events.emit("audioTrackAdd", this, toAdd);
@@ -1725,6 +1758,30 @@ var SearchResult = class {
1725
1758
  track.extractor = this._data.extractor || null;
1726
1759
  });
1727
1760
  }
1761
+ setQueryType(type) {
1762
+ this._data.queryType = type;
1763
+ return this;
1764
+ }
1765
+ setRequestedBy(user) {
1766
+ this._data.requestedBy = user;
1767
+ return this;
1768
+ }
1769
+ setExtractor(extractor) {
1770
+ this._data.extractor = extractor;
1771
+ return this;
1772
+ }
1773
+ setTracks(tracks) {
1774
+ this._data.tracks = tracks;
1775
+ return this;
1776
+ }
1777
+ setQuery(query) {
1778
+ this._data.query = query;
1779
+ return this;
1780
+ }
1781
+ setPlaylist(playlist) {
1782
+ this._data.playlist = playlist;
1783
+ return this;
1784
+ }
1728
1785
  get query() {
1729
1786
  return this._data.query;
1730
1787
  }
@@ -1819,7 +1876,7 @@ var PlayerError = class extends Error {
1819
1876
  __name(PlayerError, "PlayerError");
1820
1877
 
1821
1878
  // src/VoiceInterface/VoiceUtils.ts
1822
- import { joinVoiceChannel, getVoiceConnection } from "@discordjs/voice";
1879
+ import { joinVoiceChannel, getVoiceConnection, VoiceConnectionStatus as VoiceConnectionStatus2 } from "@discordjs/voice";
1823
1880
 
1824
1881
  // src/VoiceInterface/StreamDispatcher.ts
1825
1882
  import {
@@ -1866,7 +1923,8 @@ var StreamDispatcher = class extends EventEmitter2 {
1866
1923
  await entersState(this.voiceConnection, VoiceConnectionStatus.Connecting, this.connectionTimeout);
1867
1924
  } catch {
1868
1925
  try {
1869
- this.voiceConnection.destroy();
1926
+ if (this.voiceConnection.state.status !== VoiceConnectionStatus.Destroyed)
1927
+ this.voiceConnection.destroy();
1870
1928
  } catch (err) {
1871
1929
  this.emit("error", err);
1872
1930
  }
@@ -1876,7 +1934,8 @@ var StreamDispatcher = class extends EventEmitter2 {
1876
1934
  this.voiceConnection.rejoin();
1877
1935
  } else {
1878
1936
  try {
1879
- this.voiceConnection.destroy();
1937
+ if (this.voiceConnection.state.status !== VoiceConnectionStatus.Destroyed)
1938
+ this.voiceConnection.destroy();
1880
1939
  } catch (err) {
1881
1940
  this.emit("error", err);
1882
1941
  }
@@ -1913,6 +1972,8 @@ var StreamDispatcher = class extends EventEmitter2 {
1913
1972
  });
1914
1973
  this.audioPlayer.on("debug", (m) => void this.emit("debug", m));
1915
1974
  this.audioPlayer.on("error", (error) => void this.emit("error", error));
1975
+ this.voiceConnection.on("debug", (m) => void this.emit("debug", m));
1976
+ this.voiceConnection.on("error", (error) => void this.emit("error", error));
1916
1977
  this.voiceConnection.subscribe(this.audioPlayer);
1917
1978
  }
1918
1979
  get paused() {
@@ -1933,6 +1994,21 @@ var StreamDispatcher = class extends EventEmitter2 {
1933
1994
  isIdle() {
1934
1995
  return this.audioPlayer.state.status === AudioPlayerStatus.Idle;
1935
1996
  }
1997
+ isDestroyed() {
1998
+ return this.voiceConnection.state.status === VoiceConnectionStatus.Destroyed;
1999
+ }
2000
+ isDisconnected() {
2001
+ return this.voiceConnection.state.status === VoiceConnectionStatus.Disconnected;
2002
+ }
2003
+ isReady() {
2004
+ return this.voiceConnection.state.status === VoiceConnectionStatus.Ready;
2005
+ }
2006
+ isSignalling() {
2007
+ return this.voiceConnection.state.status === VoiceConnectionStatus.Signalling;
2008
+ }
2009
+ isConnecting() {
2010
+ return this.voiceConnection.state.status === VoiceConnectionStatus.Connecting;
2011
+ }
1936
2012
  async createStream(src, ops) {
1937
2013
  if (!ops?.disableFilters)
1938
2014
  this.queue.debug("Initiating DSP filters pipeline...");
@@ -1992,12 +2068,16 @@ var StreamDispatcher = class extends EventEmitter2 {
1992
2068
  try {
1993
2069
  if (this.audioPlayer)
1994
2070
  this.audioPlayer.stop(true);
1995
- this.voiceConnection.destroy();
2071
+ if (this.voiceConnection.state.status !== VoiceConnectionStatus.Destroyed)
2072
+ this.voiceConnection.destroy();
1996
2073
  } catch {
1997
2074
  }
1998
2075
  }
1999
2076
  end() {
2000
- this.audioPlayer.stop();
2077
+ try {
2078
+ this.audioPlayer.stop();
2079
+ } catch {
2080
+ }
2001
2081
  }
2002
2082
  pause(interpolateSilence) {
2003
2083
  const success = this.audioPlayer.pause(interpolateSilence);
@@ -2072,8 +2152,12 @@ var VoiceUtils = class {
2072
2152
  }
2073
2153
  disconnect(connection) {
2074
2154
  if (connection instanceof StreamDispatcher)
2075
- return connection.voiceConnection.destroy();
2076
- return connection.destroy();
2155
+ connection = connection.voiceConnection;
2156
+ try {
2157
+ if (connection.state.status !== VoiceConnectionStatus2.Destroyed)
2158
+ return connection.destroy();
2159
+ } catch {
2160
+ }
2077
2161
  }
2078
2162
  getConnection(guild) {
2079
2163
  return this.cache.get(guild) || getVoiceConnection(guild);
@@ -2082,7 +2166,6 @@ var VoiceUtils = class {
2082
2166
  __name(VoiceUtils, "VoiceUtils");
2083
2167
 
2084
2168
  // src/utils/QueryCache.ts
2085
- import Fuse from "fuse.js";
2086
2169
  var DEFAULT_EXPIRY_TIMEOUT = 18e6;
2087
2170
  var _defaultCache;
2088
2171
  var QueryCache = class {
@@ -2092,11 +2175,6 @@ var QueryCache = class {
2092
2175
  this.player = player;
2093
2176
  this.options = options;
2094
2177
  __privateAdd(this, _defaultCache, /* @__PURE__ */ new Map());
2095
- this.fuse = new Fuse([], {
2096
- keys: [
2097
- "url"
2098
- ]
2099
- });
2100
2178
  this.timer = setInterval(this.cleanup.bind(this), this.checkInterval).unref();
2101
2179
  }
2102
2180
  get checkInterval() {
@@ -2123,10 +2201,8 @@ var QueryCache = class {
2123
2201
  });
2124
2202
  }
2125
2203
  async resolve(context) {
2126
- const cacheData = await this.getData();
2127
- this.fuse.setCollection(cacheData.map((m) => m.data));
2128
- const result = this.fuse.search(context.query);
2129
- if (!result.length)
2204
+ const result = __privateGet(this, _defaultCache).get(context.query);
2205
+ if (!result)
2130
2206
  return new SearchResult(this.player, {
2131
2207
  query: context.query,
2132
2208
  requestedBy: context.requestedBy,
@@ -2134,7 +2210,7 @@ var QueryCache = class {
2134
2210
  });
2135
2211
  return new SearchResult(this.player, {
2136
2212
  query: context.query,
2137
- tracks: result.map((m) => m.item),
2213
+ tracks: [result.data],
2138
2214
  playlist: null,
2139
2215
  queryType: context.queryType,
2140
2216
  requestedBy: context.requestedBy
@@ -2192,6 +2268,8 @@ var kSingleton = Symbol("InstanceDiscordPlayerSingleton");
2192
2268
  var _lastLatency, _voiceStateUpdateListener, _lagMonitorTimeout, _lagMonitorInterval;
2193
2269
  var _Player = class extends PlayerEventsEmitter {
2194
2270
  constructor(client, options = {}) {
2271
+ if (!options.ignoreInstance && kSingleton in _Player)
2272
+ return _Player[kSingleton];
2195
2273
  super(["error"]);
2196
2274
  __privateAdd(this, _lastLatency, -1);
2197
2275
  __privateAdd(this, _voiceStateUpdateListener, this.handleVoiceState.bind(this));
@@ -2243,20 +2321,23 @@ ${this.scanDeps()}`);
2243
2321
  }, this.options.lagMonitor).unref());
2244
2322
  }
2245
2323
  addPlayer(this);
2246
- }
2247
- debug(m) {
2248
- return this.emit("debug", m);
2249
- }
2250
- static singleton(client, options = {}) {
2251
2324
  if (!(kSingleton in _Player)) {
2252
2325
  Object.defineProperty(_Player, kSingleton, {
2253
- value: new _Player(client, options),
2326
+ value: this,
2254
2327
  writable: true,
2255
2328
  configurable: true,
2256
2329
  enumerable: false
2257
2330
  });
2258
2331
  }
2259
- return _Player[kSingleton];
2332
+ }
2333
+ debug(m) {
2334
+ return this.emit("debug", m);
2335
+ }
2336
+ static singleton(client, options = {}) {
2337
+ return new _Player(client, {
2338
+ ...options,
2339
+ ignoreInstance: false
2340
+ });
2260
2341
  }
2261
2342
  static getAllPlayers() {
2262
2343
  return getPlayers();
@@ -2300,20 +2381,20 @@ ${this.scanDeps()}`);
2300
2381
  const wasHandled = this.events.emit("voiceStateUpdate", queue, oldState, newState);
2301
2382
  if (wasHandled && !this.options.lockVoiceStateHandler)
2302
2383
  return;
2303
- if (oldState.channelId && !newState.channelId && newState.member.id === newState.guild.members.me.id) {
2384
+ if (oldState.channelId && !newState.channelId && newState.member?.id === newState.guild.members.me?.id) {
2304
2385
  try {
2305
2386
  queue.delete();
2306
2387
  } catch {
2307
2388
  }
2308
2389
  return void this.events.emit("disconnect", queue);
2309
2390
  }
2310
- if (!oldState.channelId && newState.channelId && newState.member.id === newState.guild.members.me.id) {
2391
+ if (!oldState.channelId && newState.channelId && newState.member?.id === newState.guild.members.me?.id) {
2311
2392
  if (newState.serverMute != null && oldState.serverMute !== newState.serverMute) {
2312
2393
  queue.node.setPaused(newState.serverMute);
2313
2394
  } else if (newState.channel?.type === ChannelType2.GuildStageVoice && newState.suppress != null && oldState.suppress !== newState.suppress) {
2314
2395
  queue.node.setPaused(newState.suppress);
2315
2396
  if (newState.suppress) {
2316
- newState.guild.members.me.voice.setRequestToSpeak(true).catch(Util.noop);
2397
+ newState.guild.members.me?.voice.setRequestToSpeak(true).catch(Util.noop);
2317
2398
  }
2318
2399
  }
2319
2400
  }
@@ -2340,8 +2421,8 @@ ${this.scanDeps()}`);
2340
2421
  }
2341
2422
  }
2342
2423
  if (oldState.channelId && newState.channelId && oldState.channelId !== newState.channelId) {
2343
- if (newState.member.id === newState.guild.members.me.id) {
2344
- if (queue.connection && newState.member.id === newState.guild.members.me.id)
2424
+ if (newState.member?.id === newState.guild.members.me?.id) {
2425
+ if (queue.connection && newState.member?.id === newState.guild.members.me?.id)
2345
2426
  queue.channel = newState.channel;
2346
2427
  const emptyTimeout = queue.timeouts.get(`empty_${oldState.guild.id}`);
2347
2428
  const channelEmpty = Util.isVoiceEmpty(queue.channel);
@@ -2410,12 +2491,13 @@ ${this.scanDeps()}`);
2410
2491
  const queue = this.nodes.create(vc.guild, options.nodeOptions);
2411
2492
  if (!queue.channel)
2412
2493
  await queue.connect(vc, options.connectionOptions);
2413
- if (!result.hasPlaylist()) {
2414
- await queue.node.play(result.tracks[0]);
2494
+ if (!result.playlist) {
2495
+ queue.addTrack(result.tracks[0]);
2415
2496
  } else {
2416
2497
  queue.addTrack(result.playlist);
2417
- await queue.node.play();
2418
2498
  }
2499
+ if (!queue.node.isPlaying())
2500
+ await queue.node.play();
2419
2501
  return {
2420
2502
  track: result.tracks[0],
2421
2503
  extractor: result.extractor,
@@ -2545,6 +2627,44 @@ _lagMonitorTimeout = new WeakMap();
2545
2627
  _lagMonitorInterval = new WeakMap();
2546
2628
  Player._singletonKey = kSingleton;
2547
2629
 
2630
+ // src/hooks/common.ts
2631
+ var getPlayer = /* @__PURE__ */ __name(() => {
2632
+ return getPlayers()[0];
2633
+ }, "getPlayer");
2634
+ var getQueue = /* @__PURE__ */ __name((node) => {
2635
+ const player = getPlayer();
2636
+ if (!player)
2637
+ return null;
2638
+ return player.nodes.resolve(node) || null;
2639
+ }, "getQueue");
2640
+
2641
+ // src/hooks/useHistory.ts
2642
+ function useHistory(node) {
2643
+ const queue = getQueue(node);
2644
+ if (!queue)
2645
+ return null;
2646
+ return queue.history;
2647
+ }
2648
+ __name(useHistory, "useHistory");
2649
+
2650
+ // src/hooks/usePlayer.ts
2651
+ function usePlayer(node) {
2652
+ const queue = getQueue(node);
2653
+ if (!queue)
2654
+ return null;
2655
+ return queue.node;
2656
+ }
2657
+ __name(usePlayer, "usePlayer");
2658
+
2659
+ // src/hooks/useQueue.ts
2660
+ function useQueue(node) {
2661
+ const queue = getQueue(node);
2662
+ if (!queue)
2663
+ return null;
2664
+ return queue;
2665
+ }
2666
+ __name(useQueue, "useQueue");
2667
+
2548
2668
  // src/index.ts
2549
2669
  import {
2550
2670
  AudioFilters as AudioFilters2,
@@ -2556,7 +2676,7 @@ import {
2556
2676
  AF_VAPORWAVE_RATE,
2557
2677
  FiltersChain as FiltersChain2
2558
2678
  } from "@discord-player/equalizer";
2559
- var version = "6.0.0-dev.4";
2679
+ var version = "6.0.0-dev.5";
2560
2680
  if (!djsVersion.startsWith("14")) {
2561
2681
  process.emitWarning(`Discord.js v${djsVersion} is incompatible with Discord Player v${version}! Please use >=v14.x of Discord.js`);
2562
2682
  }
@@ -2589,6 +2709,7 @@ export {
2589
2709
  Playlist,
2590
2710
  Q_BUTTERWORTH,
2591
2711
  QueryCache,
2712
+ QueryResolver,
2592
2713
  QueryType,
2593
2714
  QueueRepeatMode,
2594
2715
  SearchResult,
@@ -2599,6 +2720,9 @@ export {
2599
2720
  VoiceUtils,
2600
2721
  VolumeTransformer,
2601
2722
  createFFmpegStream,
2723
+ useHistory,
2724
+ usePlayer,
2725
+ useQueue,
2602
2726
  version
2603
2727
  };
2604
2728
  //# sourceMappingURL=index.mjs.map