Skip to content

Commit

Permalink
Merge pull request #50 from RainyXeon/dev
Browse files Browse the repository at this point in the history
Prefinal beta version for production running
  • Loading branch information
RainyXeon authored May 12, 2024
2 parents a08fe96 + 6c7a5c1 commit 941ba4e
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 122 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rainlink",
"version": "0.9.3",
"version": "0.9.4",
"description": "Another lavalink wrapper but focus on stability and rich features",
"repository": {
"type": "git",
Expand Down
3 changes: 0 additions & 3 deletions src/Manager/RainlinkPlayerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { RainlinkEvents, RainlinkPlayerState, VoiceState } from '../Interface/Co
import { VoiceChannelOptions } from '../Interface/Player';
import { RainlinkPlayer } from '../Player/RainlinkPlayer';
import { Rainlink } from '../Rainlink';
import { RainlinkPlugin } from '../Plugin/VoiceReceiver/Plugin';
import { RainlinkDatabase } from '../Utilities/RainlinkDatabase';

export class RainlinkPlayerManager extends RainlinkDatabase<RainlinkPlayer> {
Expand Down Expand Up @@ -50,8 +49,6 @@ export class RainlinkPlayerManager extends RainlinkDatabase<RainlinkPlayer> {
player.state = RainlinkPlayerState.CONNECTED;
this.debug('Player created at ' + options.guildId);
this.manager.emit(RainlinkEvents.PlayerCreate, player);
const voiceReceiver = this.manager.plugins.get('rainlink-voiceReceiver') as RainlinkPlugin;
if (voiceReceiver && node.driver.id.includes('nodelink')) voiceReceiver.open(node, options);
return player;
}

Expand Down
3 changes: 0 additions & 3 deletions src/Player/RainlinkPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { RainlinkTrack } from './RainlinkTrack.js';
import { UpdatePlayerInfo, UpdatePlayerOptions } from '../Interface/Rest.js';
import { RainlinkSearchOptions, RainlinkSearchResult } from '../Interface/Manager.js';
import { RainlinkPlugin } from '../Plugin/VoiceReceiver/Plugin.js';
import { ServerUpdate, StateUpdatePartial } from '../Interface/Connection.js';
import { EventEmitter } from 'node:events';
import { RainlinkDatabase } from '../Utilities/RainlinkDatabase.js';
Expand Down Expand Up @@ -199,8 +198,6 @@ export class RainlinkPlayer extends EventEmitter {
this.sudoDestroy = true;
this.clear(false);
this.disconnect();
const voiceReceiver = this.manager.plugins.get('rainlink-voiceReceiver') as RainlinkPlugin;
if (voiceReceiver && this.node.driver.id.includes('nodelink')) voiceReceiver.close(this.guildId);
this.node.rest.updatePlayer({
guildId: this.guildId,
playerOptions: {
Expand Down
71 changes: 0 additions & 71 deletions src/Plugin/PlayerMoved/Plugin.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/Plugin/RainlinkPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Rainlink } from '../Rainlink';

/** The interface class for another rainlink plugin, extend it to use */
export class RainlinkPlugin {
public readonly isRainlinkPlugin: boolean = true;
/** Name function for getting plugin name */
public name(): string {
throw new Error('Plugin must implement name() and return a plguin name string');
Expand Down
32 changes: 21 additions & 11 deletions src/Plugin/VoiceReceiver/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { RainlinkWebsocket } from '../../Utilities/RainlinkWebsocket';
import { RainlinkDatabase } from '../../Utilities/RainlinkDatabase';

export class RainlinkPlugin extends Plugin {
protected manager?: Rainlink;
/** Whenever the plugin is enabled or not */
public enabled: boolean = false;
protected manager: Rainlink | null = null;
protected runningWs: RainlinkDatabase<RainlinkWebsocket> = new RainlinkDatabase<RainlinkWebsocket>();

constructor() {
Expand All @@ -29,9 +27,7 @@ export class RainlinkPlugin extends Plugin {

/** Open the ws voice reciver client */
public open(node: RainlinkNode, voiceOptions: VoiceChannelOptions): void {
if (!this.enabled) throw new Error('This plugin is unloaded!');
if (!node.driver.id.includes('nodelink'))
throw new Error('This node not support voice receiver, please use Nodelink2 to use this feature!');
if (!this.manager) throw new Error('This plugin is unloaded!');
const wsUrl = `${node.options.secure ? 'wss' : 'ws'}://${node.options.host}:${node.options.port}`;
const ws = new RainlinkWebsocket(wsUrl + '/connection/data', {
headers: {
Expand Down Expand Up @@ -61,6 +57,7 @@ export class RainlinkPlugin extends Plugin {

/** Open the ws voice reciver client */
public close(guildId: string): void {
if (!this.manager) throw new Error('This plugin is unloaded!');
const targetWs = this.runningWs.get(guildId);
if (!targetWs) return;
targetWs.close(1006, 'Self closed');
Expand All @@ -70,6 +67,7 @@ export class RainlinkPlugin extends Plugin {
}

protected wsMessageEvent(node: RainlinkNode, data: string) {
if (!this.manager) throw new Error('This plugin is unloaded!');
const wsData = JSON.parse(data.toString());
this.debug(String(data));
switch (wsData.type) {
Expand All @@ -88,19 +86,31 @@ export class RainlinkPlugin extends Plugin {
break;
}
}
// this.node.wsMessageEvent(wsData);
}

/** Load function for make the plugin working */
public load(manager: Rainlink): void {
this.manager = manager;
this.enabled = true;
this.manager.on('playerCreate', player => {
if (!player.node.driver.id.includes('nodelink')) return;
this.open(player.node, {
guildId: player.guildId,
shardId: player.shardId,
voiceId: player.voiceId || '',
textId: player.textId,
});
});
this.manager.on('playerDestroy', player => {
if (!player.node.driver.id.includes('nodelink')) return;
this.close(player.guildId);
});
}

/** unload function for make the plugin stop working */
public unload(manager: Rainlink): void {
this.manager = manager;
this.enabled = false;
public unload(): void {
this.manager?.removeListener('playerCreate', () => {});
this.manager?.removeListener('playerDestroy', () => {});
this.manager = null;
}

protected debug(logs: string) {
Expand Down
5 changes: 1 addition & 4 deletions src/Plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RainlinkPlugin as PlayerMoved } from './PlayerMoved/Plugin.js';
import { RainlinkPlugin as VoiceReceiver } from './VoiceReceiver/Plugin.js';

/**
Expand All @@ -13,8 +12,7 @@ import { RainlinkPlugin as VoiceReceiver } from './VoiceReceiver/Plugin.js';
* nodes: Nodes,
* library: new Library.DiscordJS(client),
* plugins: [
* new Plugin.PlayerMoved(client),
* * // About voice receiver plugin:
* // About voice receiver plugin:
* // This plugin only works with node use Nodelink2 driver.
* new Plugin.VoiceReceiver()
* ],
Expand All @@ -24,6 +22,5 @@ import { RainlinkPlugin as VoiceReceiver } from './VoiceReceiver/Plugin.js';
*/

export default {
PlayerMoved,
VoiceReceiver,
};
20 changes: 1 addition & 19 deletions src/Rainlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ export declare interface Rainlink {
* @event Rainlink#playerUpdate
*/
on(event: 'playerUpdate', listener: (player: RainlinkPlayer, data: Record<string, any>) => void): this;
/**
* Emitted when a playuer is moved into another channel. [Require plugin]
* @event Rainlink#playerMoved
*/
on(
event: 'playerMoved',
listener: (player: RainlinkPlayer, oldChannelId: string, newChannelId: string) => void,
): this;
/**
* Emitted when a track paused.
* @event Rainlink#playerPause
Expand Down Expand Up @@ -226,11 +218,6 @@ export declare interface Rainlink {
/** @ignore */
once(event: 'playerUpdate', listener: (player: RainlinkPlayer, data: Record<string, any>) => void): this;
/** @ignore */
once(
event: 'playerMoved',
listener: (player: RainlinkPlayer, oldChannelId: string, newChannelId: string) => void,
): this;
/** @ignore */
once(event: 'playerPause', listener: (player: RainlinkPlayer, track: RainlinkTrack) => void): this;
/** @ignore */
once(event: 'playerResume', listener: (player: RainlinkPlayer, data: RainlinkTrack) => void): this;
Expand Down Expand Up @@ -318,11 +305,6 @@ export declare interface Rainlink {
/** @ignore */
off(event: 'playerUpdate', listener: (player: RainlinkPlayer, data: Record<string, any>) => void): this;
/** @ignore */
off(
event: 'playerMoved',
listener: (player: RainlinkPlayer, oldChannelId: string, newChannelId: string) => void,
): this;
/** @ignore */
off(event: 'playerPause', listener: (player: RainlinkPlayer, track: RainlinkTrack) => void): this;
/** @ignore */
off(event: 'playerResume', listener: (player: RainlinkPlayer, data: RainlinkTrack) => void): this;
Expand Down Expand Up @@ -464,7 +446,7 @@ export class Rainlink extends EventEmitter {

if (this.rainlinkOptions.plugins) {
for (const [, plugin] of this.rainlinkOptions.plugins.entries()) {
if (plugin.constructor.name !== 'RainlinkPlugin')
if (!plugin.isRainlinkPlugin)
throw new Error('Plugin must be an instance of RainlinkPlugin or SourceRainlinkPlugin');
plugin.load(this);

Expand Down
14 changes: 8 additions & 6 deletions test/fq.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,26 @@ async function run() {
await tester.testCase('Search tracks (title)', async () => {
const data = await client.rainlink.search("Primary/yuiko - in the Garden")
tester.debug(`<DATA> | Title: ${data.tracks[0].title}, Author: ${data.tracks[0].author}, URI: ${data.tracks[0].uri}`)
return data.tracks[0].raw.info.identifier
}, "5Cof9rP7TEQ")
return data.tracks.length !== 0 ? "localPass" : false
})

await tester.testCase('Decode track (server side)', async () => {
const data = await client.rainlink.search("Primary/yuiko - in the Garden")
const encoded = data.tracks[0].raw.encoded
const testingId = data.tracks[0].identifier
const res = await client.rainlink.nodes.full.at(0)[1].rest.decodeTrack(encoded)
tester.debug(`<DATA> | Title: ${res.info.title}, Author: ${res.info.author}, URI: ${res.info.uri}`)
return res.info.identifier
}, "5Cof9rP7TEQ")
return res.info.identifier === testingId ? "localPass" : false
})

await tester.testCase('Decode track (client side)', async () => {
const data = await client.rainlink.search("Primary/yuiko - in the Garden")
const encoded = data.tracks[0].raw.encoded
const testingId = data.tracks[0].identifier
const res = await client.rainlink.nodes.full.at(0)[1].driver.functions.get("decode")(encoded)
tester.debug(`<DATA> | Title: ${res.info.title}, Author: ${res.info.author}, URI: ${res.info.uri}`)
return res.info.identifier
}, "5Cof9rP7TEQ")
return res.info.identifier === testingId ? "localPass" : false
})

tester.printSummary()
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions test/lavalink4.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ async function run() {

await tester.testCase('GET /info', async () => {
const data = await client.rainlink.nodes.full.at(0)[1].rest.getInfo()
return "localPass" ?? true
return data ? "localPass" : true
})

await tester.testCase('GET /status', async () => {
const data = await client.rainlink.nodes.full.at(0)[1].rest.getStatus()
return "localPass" ?? true
return data ? "localPass" : true
})

await tester.testCase('GET /sessions/{id}/players', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ module.exports = class Tester {
return true
}
if (data !== expected) {
this.debug(`<FAIL> | #${this.count}. ${title} | Expected: ${expected} | Actural: ${data}`)
this.debug(`!FAIL! | #${this.count}. ${title} | Expected: ${expected} | Actural: ${data}`)
this.failed = this.failed + 1
return false
}
this.debug(`PASS | #${this.count}. ${title}`)
this.pass = this.pass + 1
return true
} catch (err) {
this.debug(`<FAIL> | #${this.count}. ${title} | error logs:`)
this.debug(`!FAIL! | #${this.count}. ${title} | error logs:`)
this.failed = this.failed + 1
console.error(err)
process.exit()
Expand Down

0 comments on commit 941ba4e

Please sign in to comment.