Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initialize module #2

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

![Kirishima Banner](https://cdn.discordapp.com/attachments/891939988088975372/931079377771450388/kirishima-ship-banner.png)

# @kirishima/template
# @kirishima/redis-queue

</div>

# Instalation
```
npm install @kirishima/redis-queue @kirishima/core
```

# Features
- lorem
- ipsum
- Written in TypeScript
- Support ESM & CommonJS

# Example
```ts
import { KirishimaQueue } from "@kirishima/redis-queue";
import { Kirishima } from "@kirishima/core";

const kirishima = new Kirishima({
plugins: [
new KirishimaQueue()
]
});
```
21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "kirishima-template",
"name": "@kirishima/redis-queue",
"author": {
"name": "KagChi"
},
"version": "0.1.0",
"license": "GPL-3.0",
"repository": {
"url": "https://github.com/kirishima-ship/kirishima-template"
"url": "https://github.com/kirishima-ship/redis-queue"
},
"bugs": {
"url": "https://github.com/kirishima-ship/kirishima-template/issues"
"url": "https://github.com/kirishima-ship/redis-queue/issues"
},
"readme": "https://github.com/kirishima-ship/kirishima-template/blob/main/README.md",
"readme": "https://github.com/kirishima-ship/redis-queue/blob/main/README.md",
"engines": {
"node": ">=14.0.0",
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"scripts": {
Expand All @@ -27,11 +27,20 @@
"@sapphire/eslint-config": "4.3.3",
"@sapphire/prettier-config": "1.4.2",
"@sapphire/ts-config": "3.3.4",
"@types/ws": "^8.5.3",
"eslint": "8.13.0",
"prettier": "2.6.2",
"rimraf": "3.0.2",
"typescript": "4.6.3",
"unbuild": "0.7.4"
},
"prettier": "@sapphire/prettier-config"
"prettier": "@sapphire/prettier-config",
"dependencies": {
"@kirishima/core": "^0.7.0",
"@kirishima/ws": "^0.2.2",
"discord-api-types": "^0.31.1",
"ioredis": "^5.0.4",
"lavalink-api-types": "^0.1.9",
"tiny-typed-emitter": "^2.1.0"
}
}
131 changes: 131 additions & 0 deletions src/Structures/KirishimaPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {
isPartialTrack,
isTrack,
Kirishima,
KirishimaNode,
KirishimaPartialTrack,
KirishimaPlayerOptions,
KirishimaTrack,
Structure
} from '@kirishima/core';
import { ConnectionState, KirishimaPlayerToJSON, LoopType } from '../Types';
import { KirishimaQueueTracks } from './KirishimaQueueTracks';
import { KirishimaVoiceConnection } from './KirishimaVoiceConnection';

export class KirishimaPlayer extends Structure.get('KirishimaPlayer') {
public queue: KirishimaQueueTracks;
public loopType: LoopType = LoopType.None;
public connection = new KirishimaVoiceConnection(this);
public position = 0;

public constructor(public options: KirishimaPlayerOptions, kirishima: Kirishima, node: KirishimaNode) {
super(options, kirishima, node);
this.queue = new KirishimaQueueTracks(this, ...(options.tracks?.items ?? []));
this.queue.current = options.tracks?.current ?? null;
this.queue.previous = options.tracks?.previous ?? null;
}

public overrideCurrentPropertyFromOptions(options: KirishimaPlayerToJSON) {
if (options.loopType) {
this.loopType = options.loopType;
}

if (options.connected) {
this.connection.state = options.connected ? ConnectionState.Connected : ConnectionState.Disconnected;
}

if (options.paused) {
this.paused = options.paused;
}

if (options.playing) {
this.playing = options.playing;
}

if (options.connection) {
this.connection.overrideCurrentPropertyFromOptions(options.connection);
}

if (options.node && this.kirishima.resolveNode(options.node)) {
this.node = this.kirishima.resolveNode(options.node)!;
}

if (options.position) {
this.position = options.position;
}

return this;
}

public setLoop(type: LoopType) {
this.loopType = type;
return this;
}

public async connect() {
this.connection.state = ConnectionState.Connecting;
const player = await super.connect();
this.connection.state = ConnectionState.Connected;
return player;
}

public async disconnect() {
const player = await super.disconnect();
this.connection.state = ConnectionState.Disconnected;
return player;
}

public resolvePartialTrack(track: KirishimaPartialTrack) {
return this.kirishima.resolveTracks(`${track.info?.title} - ${track.info?.author ? track.info.author : ''}`);
}

public async playTrack(track?: KirishimaTrack | KirishimaPartialTrack | string) {
if (track && isTrack(track)) {
return super.playTrack(track);
}

if (this.queue.current && isTrack(this.queue.current)) {
return super.playTrack(this.queue.current);
}

if (track && isPartialTrack(track)) {
const resolved = await this.resolvePartialTrack(track);
if (resolved.tracks.length && isTrack(resolved.tracks[0])) {
return super.playTrack(resolved.tracks[0].track);
}
}

if (this.queue.current && isPartialTrack(this.queue.current)) {
const resolved = await this.resolvePartialTrack(this.queue.current);
if (resolved.tracks.length && isTrack(resolved.tracks[0])) {
return super.playTrack(resolved.tracks[0].track);
}
}

if (typeof track === 'string') {
return super.playTrack(track);
}

if (typeof track === 'object' && track.track) {
return super.playTrack(track.track);
}

throw new Error('No track to play, is the track invalid?');
}

public get connected() {
return Boolean(this.voiceServer && this.voiceState?.channel_id && this.connection.state === ConnectionState.Connected);
}

public toJSON() {
return {
node: this.node.options.identifier,
loopType: this.loopType,
connected: this.connected,
paused: this.paused,
playing: this.playing,
position: this.position,
connection: this.connection.toJSON()
};
}
}
Loading