yukimu 1.3.0 → 2.0.0

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.
Files changed (135) hide show
  1. package/dist/BitrateOptimizer.d.ts +52 -0
  2. package/dist/BitrateOptimizer.d.ts.map +1 -0
  3. package/dist/BitrateOptimizer.js +115 -0
  4. package/dist/BitrateOptimizer.js.map +1 -0
  5. package/dist/ConnectionPool.d.ts +52 -3
  6. package/dist/ConnectionPool.d.ts.map +1 -1
  7. package/dist/ConnectionPool.js +124 -32
  8. package/dist/ConnectionPool.js.map +1 -1
  9. package/dist/Constants.d.ts +27 -2
  10. package/dist/Constants.d.ts.map +1 -1
  11. package/dist/Constants.js +133 -11
  12. package/dist/Constants.js.map +1 -1
  13. package/dist/Logger.d.ts +22 -0
  14. package/dist/Logger.d.ts.map +1 -0
  15. package/dist/Logger.js +74 -0
  16. package/dist/Logger.js.map +1 -0
  17. package/dist/Node.d.ts +17 -4
  18. package/dist/Node.d.ts.map +1 -1
  19. package/dist/Node.js +226 -88
  20. package/dist/Node.js.map +1 -1
  21. package/dist/Player.d.ts +74 -7
  22. package/dist/Player.d.ts.map +1 -1
  23. package/dist/Player.js +317 -92
  24. package/dist/Player.js.map +1 -1
  25. package/dist/Plugin.d.ts +19 -1
  26. package/dist/Plugin.d.ts.map +1 -1
  27. package/dist/Plugin.js +8 -0
  28. package/dist/Plugin.js.map +1 -1
  29. package/dist/Queue.d.ts +69 -2
  30. package/dist/Queue.d.ts.map +1 -1
  31. package/dist/Queue.js +138 -17
  32. package/dist/Queue.js.map +1 -1
  33. package/dist/Resolver.d.ts +33 -2
  34. package/dist/Resolver.d.ts.map +1 -1
  35. package/dist/Resolver.js +225 -33
  36. package/dist/Resolver.js.map +1 -1
  37. package/dist/Rest.d.ts +27 -4
  38. package/dist/Rest.d.ts.map +1 -1
  39. package/dist/Rest.js +157 -25
  40. package/dist/Rest.js.map +1 -1
  41. package/dist/TrackCache.d.ts +30 -5
  42. package/dist/TrackCache.d.ts.map +1 -1
  43. package/dist/TrackCache.js +119 -15
  44. package/dist/TrackCache.js.map +1 -1
  45. package/dist/WsQueue.d.ts +31 -1
  46. package/dist/WsQueue.d.ts.map +1 -1
  47. package/dist/WsQueue.js +70 -12
  48. package/dist/WsQueue.js.map +1 -1
  49. package/dist/Yukimu.d.ts +38 -5
  50. package/dist/Yukimu.d.ts.map +1 -1
  51. package/dist/Yukimu.js +150 -57
  52. package/dist/Yukimu.js.map +1 -1
  53. package/dist/connector/Connector.d.ts +26 -0
  54. package/dist/connector/Connector.d.ts.map +1 -1
  55. package/dist/connector/Connector.js +28 -0
  56. package/dist/connector/Connector.js.map +1 -1
  57. package/dist/connector/DiscordJS.d.ts +20 -1
  58. package/dist/connector/DiscordJS.d.ts.map +1 -1
  59. package/dist/connector/DiscordJS.js +44 -2
  60. package/dist/connector/DiscordJS.js.map +1 -1
  61. package/dist/connector/Eris.d.ts +12 -1
  62. package/dist/connector/Eris.d.ts.map +1 -1
  63. package/dist/connector/Eris.js +40 -6
  64. package/dist/connector/Eris.js.map +1 -1
  65. package/dist/connector/Oceanic.d.ts +12 -1
  66. package/dist/connector/Oceanic.d.ts.map +1 -1
  67. package/dist/connector/Oceanic.js +39 -4
  68. package/dist/connector/Oceanic.js.map +1 -1
  69. package/dist/errors/YukimuError.d.ts +40 -4
  70. package/dist/errors/YukimuError.d.ts.map +1 -1
  71. package/dist/errors/YukimuError.js +79 -8
  72. package/dist/errors/YukimuError.js.map +1 -1
  73. package/dist/index.d.ts +13 -4
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +30 -16
  76. package/dist/index.js.map +1 -1
  77. package/dist/plugins/AutoResume.d.ts +18 -1
  78. package/dist/plugins/AutoResume.d.ts.map +1 -1
  79. package/dist/plugins/AutoResume.js +109 -21
  80. package/dist/plugins/AutoResume.js.map +1 -1
  81. package/dist/plugins/AutoplayPlugin.d.ts +35 -0
  82. package/dist/plugins/AutoplayPlugin.d.ts.map +1 -0
  83. package/dist/plugins/AutoplayPlugin.js +111 -0
  84. package/dist/plugins/AutoplayPlugin.js.map +1 -0
  85. package/dist/plugins/InactivityPlugin.d.ts +30 -0
  86. package/dist/plugins/InactivityPlugin.d.ts.map +1 -0
  87. package/dist/plugins/InactivityPlugin.js +86 -0
  88. package/dist/plugins/InactivityPlugin.js.map +1 -0
  89. package/dist/plugins/PlayerMoved.d.ts +23 -1
  90. package/dist/plugins/PlayerMoved.d.ts.map +1 -1
  91. package/dist/plugins/PlayerMoved.js +57 -12
  92. package/dist/plugins/PlayerMoved.js.map +1 -1
  93. package/dist/types.d.ts +198 -71
  94. package/dist/types.d.ts.map +1 -1
  95. package/dist/types.js +1 -0
  96. package/dist/types.js.map +1 -1
  97. package/package.json +49 -5
  98. package/.cache/replit/env/latest +0 -88
  99. package/.cache/replit/env/latest.json +0 -1
  100. package/.cache/replit/modules/nodejs-20.res +0 -1
  101. package/.cache/replit/modules/python-3.11.res +0 -1
  102. package/.cache/replit/modules/replit.res +0 -1
  103. package/.cache/replit/modules.stamp +0 -0
  104. package/.cache/replit/nix/dotreplitenv.json +0 -1
  105. package/.cache/replit/toolchain.json +0 -1
  106. package/.local/state/workflow-logs/7zVU0iVo-fBL1ccMCmELy/configure_your_app.packager.installForAll.0 +0 -9
  107. package/.local/state/workflow-logs/7zVU0iVo-fBL1ccMCmELy/configure_your_app.shell.exec.1 +0 -1
  108. package/.local/state/workflow-logs/KRgHXizaECjWI5nWtS7Dj/configure_your_app.packager.installForAll.0 +0 -1
  109. package/.local/state/workflow-logs/KRgHXizaECjWI5nWtS7Dj/configure_your_app.shell.exec.1 +0 -1
  110. package/.local/state/workflow-logs/U0AinJQVHonnwGjj0RXLn/configure_your_app.packager.installForAll.0 +0 -2
  111. package/.local/state/workflow-logs/jVavLOnv1MqxUvxhMmqER/configure_your_app.packager.installForAll.0 +0 -1
  112. package/.local/state/workflow-logs/jVavLOnv1MqxUvxhMmqER/configure_your_app.shell.exec.1 +0 -1
  113. package/.replit +0 -7
  114. package/.upm/store.json +0 -1
  115. package/src/ConnectionPool.ts +0 -114
  116. package/src/Constants.ts +0 -45
  117. package/src/Node.ts +0 -302
  118. package/src/Player.ts +0 -332
  119. package/src/Plugin.ts +0 -7
  120. package/src/Queue.ts +0 -66
  121. package/src/Resolver.ts +0 -90
  122. package/src/Rest.ts +0 -127
  123. package/src/TrackCache.ts +0 -46
  124. package/src/WsQueue.ts +0 -58
  125. package/src/Yukimu.ts +0 -213
  126. package/src/connector/Connector.ts +0 -13
  127. package/src/connector/DiscordJS.ts +0 -26
  128. package/src/connector/Eris.ts +0 -24
  129. package/src/connector/Oceanic.ts +0 -22
  130. package/src/errors/YukimuError.ts +0 -31
  131. package/src/index.ts +0 -24
  132. package/src/plugins/AutoResume.ts +0 -37
  133. package/src/plugins/PlayerMoved.ts +0 -26
  134. package/src/types.ts +0 -145
  135. package/tsconfig.json +0 -22
package/src/Player.ts DELETED
@@ -1,332 +0,0 @@
1
- import { Queue } from "./Queue";
2
- import { PlayerState } from "./Constants";
3
- import { PlayerError } from "./errors/YukimuError";
4
- import type { Yukimu } from "./Yukimu";
5
- import type { Node } from "./Node";
6
- import type { Track, PlayerOptions, FilterOptions } from "./types";
7
-
8
- export class Player {
9
- public readonly manager: Yukimu;
10
- public node: Node;
11
- public readonly queue: Queue;
12
-
13
- public readonly guildId: string;
14
- public voiceChannelId: string | null;
15
- public textChannelId?: string;
16
-
17
- public state: PlayerState = PlayerState.DISCONNECTED;
18
- public playing: boolean = false;
19
- public paused: boolean = false;
20
- public position: number = 0;
21
- public ping: number = -1;
22
- public lastUpdated: number = Date.now();
23
- public volume: number;
24
- public loop: "none" | "track" | "queue" = "none";
25
- public autoplay: boolean = false;
26
-
27
- public sessionId: string | null = null;
28
- public voiceToken: string | null = null;
29
- public voiceEndpoint: string | null = null;
30
- public selfDeaf: boolean;
31
- public selfMute: boolean;
32
-
33
- public filters: FilterOptions = {};
34
- public readonly data: Map<string, any> = new Map();
35
-
36
- constructor(manager: Yukimu, node: Node, options: PlayerOptions) {
37
- this.manager = manager;
38
- this.node = node;
39
- this.queue = new Queue();
40
- this.guildId = options.guildId;
41
- this.voiceChannelId = options.voiceChannelId;
42
- this.textChannelId = options.textChannelId;
43
- this.volume = options.volume ?? 100;
44
- this.selfDeaf = options.selfDeaf ?? true;
45
- this.selfMute = options.selfMute ?? false;
46
- this.sendVoicePayload(options.voiceChannelId, this.selfDeaf, this.selfMute);
47
- this.state = PlayerState.CONNECTING;
48
- }
49
-
50
- get connected(): boolean { return this.state === PlayerState.CONNECTED; }
51
-
52
- get realPosition(): number {
53
- if (!this.playing || this.paused) return this.position;
54
- return Math.min(this.position + (Date.now() - this.lastUpdated), this.queue.current?.info?.length ?? this.position);
55
- }
56
-
57
- public sendVoicePayload(channelId: string | null, selfDeaf: boolean, selfMute: boolean): void {
58
- this.manager.connector.sendPayload(this.guildId, {
59
- op: 4,
60
- d: { guild_id: this.guildId, channel_id: channelId, self_deaf: selfDeaf, self_mute: selfMute },
61
- });
62
- }
63
-
64
- public checkVoiceReady(): void {
65
- if (!this.sessionId || !this.voiceToken || !this.voiceEndpoint) return;
66
-
67
- const voiceState: any = {
68
- token: this.voiceToken,
69
- endpoint: this.voiceEndpoint,
70
- sessionId: this.sessionId,
71
- channelId: this.voiceChannelId,
72
- };
73
-
74
- if (this.node.version === 4) {
75
- this.node.rest.updatePlayer(this.guildId, { voice: voiceState }).catch(console.error);
76
- } else {
77
- this.node.send({
78
- op: "voiceUpdate",
79
- guildId: this.guildId,
80
- sessionId: this.sessionId,
81
- event: { token: this.voiceToken, guild_id: this.guildId, endpoint: this.voiceEndpoint },
82
- });
83
- }
84
-
85
- this.state = PlayerState.CONNECTED;
86
- }
87
-
88
- public async play(track?: Track, options?: { startTime?: number; endTime?: number; noReplace?: boolean }): Promise<void> {
89
- const toPlay = track ?? this.queue.current;
90
- if (!toPlay) throw new PlayerError("No track to play");
91
- this.queue.current = toPlay;
92
-
93
- if (this.node.version === 4) {
94
- await this.node.rest.updatePlayer(this.guildId, {
95
- track: { encoded: toPlay.encoded },
96
- volume: this.volume,
97
- ...(options?.startTime !== undefined && { position: options.startTime }),
98
- ...(options?.endTime !== undefined && { endTime: options.endTime }),
99
- }, options?.noReplace ?? false);
100
- } else {
101
- this.node.send({
102
- op: "play",
103
- guildId: this.guildId,
104
- track: toPlay.encoded,
105
- volume: String(this.volume),
106
- ...(options?.startTime !== undefined && { startTime: String(options.startTime) }),
107
- ...(options?.endTime !== undefined && { endTime: String(options.endTime) }),
108
- });
109
- }
110
-
111
- this.playing = true;
112
- this.paused = false;
113
- this.lastUpdated = Date.now();
114
- }
115
-
116
- public async pause(state: boolean = true): Promise<void> {
117
- if (this.node.version === 4) {
118
- await this.node.rest.updatePlayer(this.guildId, { paused: state });
119
- } else {
120
- this.node.send({ op: "pause", guildId: this.guildId, pause: state });
121
- }
122
- this.paused = state;
123
- this.playing = !state;
124
- }
125
-
126
- public async resume(): Promise<void> { return this.pause(false); }
127
-
128
- public async stop(): Promise<void> {
129
- if (this.node.version === 4) {
130
- await this.node.rest.updatePlayer(this.guildId, { track: { encoded: null } });
131
- } else {
132
- this.node.send({ op: "stop", guildId: this.guildId });
133
- }
134
- this.playing = false;
135
- this.paused = false;
136
- this.position = 0;
137
- this.queue.current = null;
138
- }
139
-
140
- public async skip(): Promise<Track | null> {
141
- const next = this.queue.next();
142
- if (next) {
143
- await this.play(next);
144
- } else {
145
- await this.stop();
146
- this.manager.emit("queueEnd", this);
147
- }
148
- return next;
149
- }
150
-
151
- public async seek(position: number): Promise<void> {
152
- if (!this.queue.current?.info?.isSeekable) throw new PlayerError("Current track is not seekable");
153
- if (this.node.version === 4) {
154
- await this.node.rest.updatePlayer(this.guildId, { position });
155
- } else {
156
- this.node.send({ op: "seek", guildId: this.guildId, position });
157
- }
158
- this.position = position;
159
- this.lastUpdated = Date.now();
160
- }
161
-
162
- public async setVolume(volume: number): Promise<void> {
163
- if (volume < 0 || volume > 1000) throw new PlayerError("Volume must be between 0 and 1000");
164
- if (this.node.version === 4) {
165
- await this.node.rest.updatePlayer(this.guildId, { volume });
166
- } else {
167
- this.node.send({ op: "volume", guildId: this.guildId, volume });
168
- }
169
- this.volume = volume;
170
- }
171
-
172
- public async setFilters(filters: FilterOptions): Promise<void> {
173
- this.filters = { ...this.filters, ...filters };
174
- if (this.node.version === 4) {
175
- await this.node.rest.updatePlayer(this.guildId, { filters: this.filters });
176
- } else {
177
- if (filters.equalizer) this.node.send({ op: "equalizer", guildId: this.guildId, bands: filters.equalizer });
178
- const rest = { ...filters };
179
- delete rest.equalizer;
180
- if (Object.keys(rest).length > 0) this.node.send({ op: "filters", guildId: this.guildId, ...rest });
181
- }
182
- }
183
-
184
- public async clearFilters(): Promise<void> {
185
- this.filters = {};
186
- if (this.node.version === 4) {
187
- await this.node.rest.updatePlayer(this.guildId, { filters: {} });
188
- } else {
189
- this.node.send({ op: "filters", guildId: this.guildId });
190
- }
191
- }
192
-
193
- public async setBassBoost(level: "off" | "low" | "medium" | "high" | "extreme"): Promise<void> {
194
- const presets: Record<string, number[]> = {
195
- off: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
196
- low: [0.2,0.15,0.1,0.05,0,-0.05,0,0,0,0,0,0,0,0,0],
197
- medium: [0.4,0.3,0.2,0.1,0.05,-0.05,0,0,0,0,0,0,0,0,0],
198
- high: [0.6,0.5,0.4,0.25,0.1,-0.1,0,0,0,0,0,0,0,0,0],
199
- extreme: [1.0,0.8,0.6,0.4,0.2,-0.2,0,0,0,0,0,0,0,0,0],
200
- };
201
- await this.setFilters({ equalizer: presets[level].map((gain, band) => ({ band, gain })) });
202
- }
203
-
204
- public async setNightcore(enabled: boolean): Promise<void> {
205
- await this.setFilters({ timescale: enabled ? { speed: 1.2, pitch: 1.2, rate: 1.0 } : {} });
206
- }
207
-
208
- public async setVaporwave(enabled: boolean): Promise<void> {
209
- await this.setFilters({ timescale: enabled ? { speed: 0.8, pitch: 0.8, rate: 1.0 } : {} });
210
- }
211
-
212
- public async set8D(enabled: boolean): Promise<void> {
213
- await this.setFilters({ rotation: enabled ? { rotationHz: 0.2 } : {} });
214
- }
215
-
216
- public async setKaraoke(enabled: boolean): Promise<void> {
217
- await this.setFilters({ karaoke: enabled ? { level: 1.0, monoLevel: 1.0, filterBand: 220.0, filterWidth: 100.0 } : {} });
218
- }
219
-
220
- public async setTremolo(enabled: boolean, frequency = 2.0, depth = 0.5): Promise<void> {
221
- await this.setFilters({ tremolo: enabled ? { frequency, depth } : {} });
222
- }
223
-
224
- public async setVibrato(enabled: boolean, frequency = 2.0, depth = 0.5): Promise<void> {
225
- await this.setFilters({ vibrato: enabled ? { frequency, depth } : {} });
226
- }
227
-
228
- public async setLowPass(enabled: boolean, smoothing = 20.0): Promise<void> {
229
- await this.setFilters({ lowPass: enabled ? { smoothing } : {} });
230
- }
231
-
232
- public async add(tracks: Track | Track[], requester?: any): Promise<void> {
233
- const arr = Array.isArray(tracks) ? tracks : [tracks];
234
- if (requester) arr.forEach(t => { (t as any).requester = requester; });
235
- if (!this.queue.current && arr.length > 0) {
236
- const first = arr.shift()!;
237
- this.queue.add(arr);
238
- await this.play(first);
239
- } else {
240
- this.queue.add(arr);
241
- }
242
- }
243
-
244
- public setLoop(mode: "none" | "track" | "queue"): void { this.loop = mode; }
245
- public setAutoplay(enabled: boolean): void { this.autoplay = enabled; }
246
-
247
- public async moveToNode(node: Node): Promise<void> {
248
- if (this.node === node) return;
249
- const oldNode = this.node;
250
- this.node = node;
251
- await oldNode.rest.destroyPlayer(this.guildId).catch(() => {});
252
- if (this.queue.current) {
253
- await this.play(this.queue.current, { startTime: this.realPosition });
254
- }
255
- if (Object.keys(this.filters).length > 0) await this.setFilters(this.filters).catch(() => {});
256
- if (this.volume !== 100) await this.setVolume(this.volume).catch(() => {});
257
- }
258
-
259
- public async move(channelId: string): Promise<void> {
260
- this.voiceChannelId = channelId;
261
- this.sendVoicePayload(channelId, this.selfDeaf, this.selfMute);
262
- }
263
-
264
- public async destroy(): Promise<void> {
265
- this.state = PlayerState.DISCONNECTING;
266
- this.sendVoicePayload(null, false, false);
267
- await this.node.rest.destroyPlayer(this.guildId).catch(() => {});
268
- this.playing = false;
269
- this.paused = false;
270
- this.queue.clear();
271
- this.data.clear();
272
- this.state = PlayerState.DISCONNECTED;
273
- }
274
-
275
- // ─── Audio Quality Presets ────────────────────────────────────────
276
-
277
- /** Maximum audio quality — crystal clear, enhanced bass & treble */
278
- public async setHighQuality(): Promise<void> {
279
- await this.setFilters({
280
- equalizer: [
281
- { band: 0, gain: 0.25 }, // Sub bass boost
282
- { band: 1, gain: 0.20 }, // Bass boost
283
- { band: 2, gain: 0.15 }, // Low mid bass
284
- { band: 3, gain: 0.05 }, // Mid bass
285
- { band: 4, gain: 0.0 }, // Low mid flat
286
- { band: 5, gain: 0.0 }, // Mid flat
287
- { band: 6, gain: 0.0 }, // Upper mid flat
288
- { band: 7, gain: 0.05 }, // Presence slight boost
289
- { band: 8, gain: 0.10 }, // Upper presence
290
- { band: 9, gain: 0.15 }, // Treble boost
291
- { band: 10, gain: 0.15 }, // Upper treble
292
- { band: 11, gain: 0.10 }, // Air
293
- { band: 12, gain: 0.05 }, // Brilliance
294
- { band: 13, gain: 0.0 }, // Ultra high
295
- { band: 14, gain: 0.0 }, // Ultra high
296
- ],
297
- lowPass: { smoothing: 5.0 }, // Very light low pass for clarity
298
- });
299
- }
300
-
301
- /** Studio quality — flat, neutral, most accurate */
302
- public async setStudioQuality(): Promise<void> {
303
- await this.clearFilters();
304
- await this.setFilters({
305
- equalizer: Array.from({ length: 15 }, (_, i) => ({ band: i, gain: 0 })),
306
- });
307
- }
308
-
309
- /** Concert hall effect */
310
- public async setConcertHall(): Promise<void> {
311
- await this.setFilters({
312
- equalizer: [
313
- { band: 0, gain: 0.1 },
314
- { band: 1, gain: 0.1 },
315
- { band: 2, gain: 0.05 },
316
- { band: 3, gain: 0.0 },
317
- { band: 4, gain: -0.05 },
318
- { band: 5, gain: 0.0 },
319
- { band: 6, gain: 0.05 },
320
- { band: 7, gain: 0.1 },
321
- { band: 8, gain: 0.1 },
322
- { band: 9, gain: 0.05 },
323
- { band: 10, gain: 0.0 },
324
- { band: 11, gain: 0.0 },
325
- { band: 12, gain: 0.0 },
326
- { band: 13, gain: 0.0 },
327
- { band: 14, gain: 0.0 },
328
- ],
329
- rotation: { rotationHz: 0.05 },
330
- });
331
- }
332
- }
package/src/Plugin.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { Yukimu } from "./Yukimu";
2
-
3
- export abstract class Plugin {
4
- public abstract name: string;
5
- public abstract load(manager: Yukimu): void;
6
- public unload?(manager: Yukimu): void;
7
- }
package/src/Queue.ts DELETED
@@ -1,66 +0,0 @@
1
- import type { Track } from "./types";
2
-
3
- export class Queue {
4
- public current: Track | null = null;
5
- public previous: Track[] = [];
6
- private tracks: Track[] = [];
7
-
8
- public add(tracks: Track | Track[], position?: number): void {
9
- const arr = Array.isArray(tracks) ? tracks : [tracks];
10
- if (position !== undefined) this.tracks.splice(position, 0, ...arr);
11
- else this.tracks.push(...arr);
12
- }
13
-
14
- public next(): Track | null {
15
- if (this.current) {
16
- this.previous.unshift(this.current);
17
- if (this.previous.length > 10) this.previous.pop();
18
- }
19
- this.current = this.tracks.shift() ?? null;
20
- return this.current;
21
- }
22
-
23
- public remove(index: number): Track | null {
24
- if (index < 0 || index >= this.tracks.length) return null;
25
- const [removed] = this.tracks.splice(index, 1);
26
- return removed;
27
- }
28
-
29
- public removeRange(start: number, end: number): Track[] {
30
- return this.tracks.splice(start, end - start);
31
- }
32
-
33
- public clear(): void {
34
- this.tracks = [];
35
- this.current = null;
36
- this.previous = [];
37
- }
38
-
39
- public shuffle(): void {
40
- for (let i = this.tracks.length - 1; i > 0; i--) {
41
- const j = Math.floor(Math.random() * (i + 1));
42
- [this.tracks[i], this.tracks[j]] = [this.tracks[j], this.tracks[i]];
43
- }
44
- }
45
-
46
- public move(from: number, to: number): void {
47
- if (from < 0 || to < 0 || from >= this.tracks.length || to >= this.tracks.length) return;
48
- const [track] = this.tracks.splice(from, 1);
49
- this.tracks.splice(to, 0, track);
50
- }
51
-
52
- public skipto(index: number): Track | null {
53
- if (index < 0 || index >= this.tracks.length) return null;
54
- this.tracks.splice(0, index);
55
- return this.next();
56
- }
57
-
58
- public peek(count: number = 10): Track[] { return this.tracks.slice(0, count); }
59
- public find(predicate: (track: Track) => boolean): Track | undefined { return this.tracks.find(predicate); }
60
- public filter(predicate: (track: Track) => boolean): Track[] { return this.tracks.filter(predicate); }
61
-
62
- get size(): number { return this.tracks.length; }
63
- get isEmpty(): boolean { return this.tracks.length === 0; }
64
- get totalDuration(): number { return this.tracks.reduce((acc, t) => acc + (t.info?.length ?? 0), 0); }
65
- get list(): ReadonlyArray<Track> { return this.tracks; }
66
- }
package/src/Resolver.ts DELETED
@@ -1,90 +0,0 @@
1
- import { SOURCE_PREFIXES, URL_PATTERNS } from "./Constants";
2
- import type { Yukimu } from "./Yukimu";
3
- import type { SearchResult, SearchSource, Track } from "./types";
4
-
5
- export class Resolver {
6
- private manager: Yukimu;
7
- private spotifyToken: string | null = null;
8
- private spotifyExpiry: number = 0;
9
-
10
- constructor(manager: Yukimu) { this.manager = manager; }
11
-
12
- public async resolve(query: string, source: SearchSource, requester?: any): Promise<SearchResult> {
13
- const node = this.manager.getBestNode();
14
- const isUrl = /^https?:\/\//.test(query);
15
- const identifier = isUrl ? query : `${SOURCE_PREFIXES[source] ?? "ytsearch"}:${query}`;
16
-
17
- let result: SearchResult;
18
- try {
19
- result = await node.loadTracks(identifier);
20
- } catch (err: any) {
21
- console.error("[Resolver] loadTracks error:", err.message);
22
- return { loadType: "error", tracks: [] };
23
- }
24
-
25
- if (!result) return { loadType: "empty", tracks: [] };
26
- if (!result.tracks) result.tracks = [];
27
-
28
- // Attach requester to all tracks
29
- if (requester && result.tracks.length > 0) {
30
- result.tracks.forEach((t: any) => { t.requester = requester; });
31
- }
32
-
33
- // Spotify fallback — if LavaSrc not installed
34
- if ((result.loadType === "error" || result.loadType === "empty") && isUrl) {
35
- const spotifyMatch = query.match(/spotify\.com\/track\/([a-zA-Z0-9]+)/);
36
- if (spotifyMatch) {
37
- const meta = await this.resolveSpotifyMeta(spotifyMatch[1]);
38
- if (meta) return this.resolve(`${meta.title} ${meta.artist}`, "youtube", requester);
39
- }
40
- }
41
-
42
- // Cache tracks
43
- if (result.tracks.length > 0) {
44
- for (const track of result.tracks) {
45
- if (track?.encoded) this.manager.trackCache.set(track.encoded, track);
46
- }
47
- }
48
-
49
- return result;
50
- }
51
-
52
- public detectSource(url: string): SearchSource | null {
53
- for (const [source, patterns] of Object.entries(URL_PATTERNS)) {
54
- if (patterns.some(p => p.test(url))) return source as SearchSource;
55
- }
56
- return null;
57
- }
58
-
59
- private async getSpotifyToken(): Promise<string | null> {
60
- const opts = this.manager.options.spotify;
61
- if (!opts) return null;
62
- if (this.spotifyToken && Date.now() < this.spotifyExpiry) return this.spotifyToken;
63
- try {
64
- const res = await (globalThis as any).fetch("https://accounts.spotify.com/api/token", {
65
- method: "POST",
66
- headers: {
67
- "Content-Type": "application/x-www-form-urlencoded",
68
- Authorization: `Basic ${Buffer.from(`${opts.clientId}:${opts.clientSecret}`).toString("base64")}`,
69
- },
70
- body: "grant_type=client_credentials",
71
- });
72
- const data = await res.json();
73
- this.spotifyToken = data.access_token;
74
- this.spotifyExpiry = Date.now() + data.expires_in * 1000 - 5000;
75
- return this.spotifyToken;
76
- } catch { return null; }
77
- }
78
-
79
- private async resolveSpotifyMeta(trackId: string): Promise<{ title: string; artist: string } | null> {
80
- const token = await this.getSpotifyToken();
81
- if (!token) return null;
82
- try {
83
- const res = await (globalThis as any).fetch(`https://api.spotify.com/v1/tracks/${trackId}`, {
84
- headers: { Authorization: `Bearer ${token}` },
85
- });
86
- const data = await res.json();
87
- return { title: data.name, artist: data.artists[0]?.name ?? "Unknown" };
88
- } catch { return null; }
89
- }
90
- }
package/src/Rest.ts DELETED
@@ -1,127 +0,0 @@
1
- import { RestError } from "./errors/YukimuError";
2
- import type { Node } from "./Node";
3
- import type { SearchResult, Track, NodeStats, NodeInfo } from "./types";
4
-
5
- const TIMEOUT_MS = 30000;
6
-
7
- export class Rest {
8
- private node: Node;
9
-
10
- constructor(node: Node) { this.node = node; }
11
-
12
- get baseUrl(): string {
13
- const protocol = this.node.options.secure ? "https" : "http";
14
- return `${protocol}://${this.node.options.host}:${this.node.options.port}`;
15
- }
16
-
17
- get prefix(): string { return this.node.version === 4 ? "/v4" : ""; }
18
-
19
- get headers(): Record<string, string> {
20
- return {
21
- Authorization: this.node.options.password,
22
- "User-Id": this.node.manager.options.clientId,
23
- "Client-Name": "Yukimu/1.3.0",
24
- "Content-Type": "application/json",
25
- ...(this.node.version === 3 ? { "Num-Shards": "1" } : {}),
26
- };
27
- }
28
-
29
- public async request<T = any>(method: string, path: string, body?: any): Promise<T> {
30
- const url = `${this.baseUrl}${this.prefix}${path}`;
31
- const controller = new AbortController();
32
- const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS);
33
-
34
- try {
35
- const res = await (globalThis as any).fetch(url, {
36
- method,
37
- headers: this.headers,
38
- body: body ? JSON.stringify(body) : undefined,
39
- signal: controller.signal,
40
- });
41
-
42
- clearTimeout(timeout);
43
-
44
- if (!res.ok) {
45
- const text = await res.text().catch(() => "Unknown error");
46
- throw new RestError(`REST ${res.status} on ${method} ${path}: ${text}`, res.status, path);
47
- }
48
-
49
- if (res.status === 204) return undefined as T;
50
-
51
- const text = await res.text();
52
- let parsed: any;
53
- try { parsed = JSON.parse(text); } catch { return null as T; }
54
-
55
- // Normalize Lavalink v4.2+ response — uses "data" instead of "tracks"
56
- if (parsed && parsed.data !== undefined && !parsed.tracks) {
57
- parsed.tracks = Array.isArray(parsed.data) ? parsed.data : [parsed.data];
58
- }
59
-
60
- return parsed as T;
61
- } catch (err: any) {
62
- clearTimeout(timeout);
63
- if (err.name === "AbortError") throw new RestError(`REST timeout on ${method} ${path}`, 408, path);
64
- throw err;
65
- }
66
- }
67
-
68
- public async loadTracks(identifier: string): Promise<SearchResult> {
69
- const raw = await this.request<any>("GET", `/loadtracks?identifier=${encodeURIComponent(identifier)}`);
70
- if (!raw) return { loadType: "empty", tracks: [] };
71
- return this.node.version === 3 ? this.normalizeV3(raw) : raw;
72
- }
73
-
74
- public async decodeTrack(encoded: string): Promise<Track> {
75
- return this.request("GET", `/decodetrack?encodedTrack=${encodeURIComponent(encoded)}`);
76
- }
77
-
78
- public async decodeTracks(encoded: string[]): Promise<Track[]> {
79
- return this.request("POST", `/decodetracks`, encoded);
80
- }
81
-
82
- public async getInfo(): Promise<NodeInfo> {
83
- return this.request("GET", "/info");
84
- }
85
-
86
- public async getStats(): Promise<NodeStats> {
87
- return this.request("GET", "/stats");
88
- }
89
-
90
- public async updatePlayer(guildId: string, body: any, noReplace = false): Promise<any> {
91
- const path = this.node.version === 4
92
- ? `/sessions/${this.node.sessionId}/players/${guildId}?noReplace=${noReplace}`
93
- : `/players/${guildId}`;
94
- return this.request("PATCH", path, body);
95
- }
96
-
97
- public async destroyPlayer(guildId: string): Promise<void> {
98
- const path = this.node.version === 4
99
- ? `/sessions/${this.node.sessionId}/players/${guildId}`
100
- : `/players/${guildId}`;
101
- await this.request("DELETE", path).catch(() => {});
102
- }
103
-
104
- public async updateSession(body: { resuming?: boolean; timeout?: number }): Promise<any> {
105
- if (this.node.version !== 4) return;
106
- return this.request("PATCH", `/sessions/${this.node.sessionId}`, body);
107
- }
108
-
109
- private normalizeV3(raw: any): SearchResult {
110
- const loadType = (raw?.loadType ?? "").toLowerCase();
111
- const norm = (tracks: any[]): Track[] =>
112
- (tracks ?? []).map((t: any) => ({
113
- encoded: t.track ?? t.encoded,
114
- info: t.info,
115
- pluginInfo: t.pluginInfo ?? {},
116
- }));
117
-
118
- switch (loadType) {
119
- case "track_loaded": return { loadType: "track", tracks: norm(raw.tracks) };
120
- case "playlist_loaded": return { loadType: "playlist", tracks: norm(raw.tracks), playlistInfo: raw.playlistInfo };
121
- case "search_result": return { loadType: "search", tracks: norm(raw.tracks) };
122
- case "no_matches": return { loadType: "empty", tracks: [] };
123
- case "load_failed": return { loadType: "error", tracks: [], exception: raw.exception };
124
- default: return { loadType: "empty", tracks: [] };
125
- }
126
- }
127
- }
package/src/TrackCache.ts DELETED
@@ -1,46 +0,0 @@
1
- import type { Track } from "./types";
2
-
3
- interface CacheEntry {
4
- track: Track;
5
- expiresAt: number;
6
- }
7
-
8
- export class TrackCache {
9
- private cache: Map<string, CacheEntry> = new Map();
10
- private readonly ttl: number;
11
- private sweepInterval: ReturnType<typeof setInterval>;
12
-
13
- constructor(ttlMs: number = 3600000) {
14
- this.ttl = ttlMs;
15
- this.sweepInterval = setInterval(() => this.sweep(), 600000);
16
- }
17
-
18
- public set(encoded: string, track: Track): void {
19
- this.cache.set(encoded, { track, expiresAt: Date.now() + this.ttl });
20
- }
21
-
22
- public get(encoded: string): Track | null {
23
- const entry = this.cache.get(encoded);
24
- if (!entry) return null;
25
- if (Date.now() > entry.expiresAt) { this.cache.delete(encoded); return null; }
26
- return entry.track;
27
- }
28
-
29
- public has(encoded: string): boolean { return this.get(encoded) !== null; }
30
- public delete(encoded: string): void { this.cache.delete(encoded); }
31
- public clear(): void { this.cache.clear(); }
32
-
33
- private sweep(): void {
34
- const now = Date.now();
35
- for (const [key, entry] of this.cache) {
36
- if (now > entry.expiresAt) this.cache.delete(key);
37
- }
38
- }
39
-
40
- public destroy(): void {
41
- clearInterval(this.sweepInterval);
42
- this.cache.clear();
43
- }
44
-
45
- get size(): number { return this.cache.size; }
46
- }