mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-05-10 04:01:37 +02:00
Merge Player and Queue services
This commit is contained in:
parent
646f030781
commit
9c91ce1a13
21 changed files with 236 additions and 255 deletions
|
@ -8,8 +8,9 @@ import ytsr from 'ytsr';
|
|||
import YouTube from 'youtube.ts';
|
||||
import pLimit from 'p-limit';
|
||||
import uniqueRandomArray from 'unique-random-array';
|
||||
import {QueuedSong, QueuedPlaylist} from '../services/queue';
|
||||
import {QueuedSong, QueuedPlaylist} from '../services/player';
|
||||
import {TYPES} from '../types';
|
||||
import {parseTime} from '../utils/time';
|
||||
|
||||
@injectable()
|
||||
export default class {
|
||||
|
@ -186,7 +187,7 @@ export default class {
|
|||
return {
|
||||
title: video.title,
|
||||
artist: track.artists[0].name,
|
||||
length: track.duration_ms / 1000,
|
||||
length: parseTime(video.duration),
|
||||
url: video.link,
|
||||
playlist,
|
||||
isLive: video.live
|
||||
|
|
|
@ -2,17 +2,14 @@ import {inject, injectable} from 'inversify';
|
|||
import {Message} from 'discord.js';
|
||||
import {TYPES} from '../types';
|
||||
import PlayerManager from '../managers/player';
|
||||
import QueueManager from '../managers/queue';
|
||||
import {getMostPopularVoiceChannel} from '../utils/channels';
|
||||
|
||||
@injectable()
|
||||
export default class {
|
||||
private readonly playerManager: PlayerManager;
|
||||
private readonly queueManager: QueueManager;
|
||||
|
||||
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
||||
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
|
||||
this.playerManager = playerManager;
|
||||
this.queueManager = queueManager;
|
||||
}
|
||||
|
||||
async execute(msg: Message): Promise<boolean> {
|
||||
|
@ -24,7 +21,6 @@ export default class {
|
|||
}
|
||||
|
||||
if (msg.content.includes('packers')) {
|
||||
const queue = this.queueManager.get(msg.guild!.id);
|
||||
const player = this.playerManager.get(msg.guild!.id);
|
||||
|
||||
const [channel, n] = getMostPopularVoiceChannel(msg.guild!);
|
||||
|
@ -39,14 +35,15 @@ export default class {
|
|||
await player.connect(channel);
|
||||
}
|
||||
|
||||
const isPlaying = queue.getCurrent() !== null;
|
||||
const isPlaying = player.getCurrent() !== null;
|
||||
let oldPosition = 0;
|
||||
|
||||
queue.add({title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false});
|
||||
player.add({title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false}, {immediate: true});
|
||||
|
||||
if (isPlaying) {
|
||||
oldPosition = player.getPosition();
|
||||
queue.forward();
|
||||
|
||||
await player.forward();
|
||||
}
|
||||
|
||||
await player.seek(8);
|
||||
|
@ -54,10 +51,10 @@ export default class {
|
|||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
setTimeout(async () => {
|
||||
queue.removeCurrent();
|
||||
player.removeCurrent();
|
||||
|
||||
if (isPlaying) {
|
||||
queue.back();
|
||||
await player.back();
|
||||
await player.seek(oldPosition);
|
||||
} else {
|
||||
player.disconnect();
|
||||
|
|
|
@ -6,7 +6,21 @@ import hasha from 'hasha';
|
|||
import ytdl from 'ytdl-core';
|
||||
import {WriteStream} from 'fs-capacitor';
|
||||
import ffmpeg from 'fluent-ffmpeg';
|
||||
import Queue, {QueuedSong} from './queue';
|
||||
import shuffle from 'array-shuffle';
|
||||
|
||||
export interface QueuedPlaylist {
|
||||
title: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface QueuedSong {
|
||||
title: string;
|
||||
artist: string;
|
||||
url: string;
|
||||
length: number;
|
||||
playlist: QueuedPlaylist | null;
|
||||
isLive: boolean;
|
||||
}
|
||||
|
||||
export enum STATUS {
|
||||
PLAYING,
|
||||
|
@ -16,7 +30,8 @@ export enum STATUS {
|
|||
export default class {
|
||||
public status = STATUS.PAUSED;
|
||||
public voiceConnection: VoiceConnection | null = null;
|
||||
private readonly queue: Queue;
|
||||
private queue: QueuedSong[] = [];
|
||||
private queuePosition = 0;
|
||||
private readonly cacheDir: string;
|
||||
private dispatcher: StreamDispatcher | null = null;
|
||||
private nowPlaying: QueuedSong | null = null;
|
||||
|
@ -25,8 +40,7 @@ export default class {
|
|||
|
||||
private positionInSeconds = 0;
|
||||
|
||||
constructor(queue: Queue, cacheDir: string) {
|
||||
this.queue = queue;
|
||||
constructor(cacheDir: string) {
|
||||
this.cacheDir = cacheDir;
|
||||
}
|
||||
|
||||
|
@ -58,7 +72,7 @@ export default class {
|
|||
throw new Error('Not connected to a voice channel.');
|
||||
}
|
||||
|
||||
const currentSong = this.queue.getCurrent();
|
||||
const currentSong = this.getCurrent();
|
||||
|
||||
if (!currentSong) {
|
||||
throw new Error('No song currently playing');
|
||||
|
@ -90,14 +104,14 @@ export default class {
|
|||
throw new Error('Not connected to a voice channel.');
|
||||
}
|
||||
|
||||
const currentSong = this.queue.getCurrent();
|
||||
const currentSong = this.getCurrent();
|
||||
|
||||
if (!currentSong) {
|
||||
throw new Error('Queue empty.');
|
||||
}
|
||||
|
||||
// Resume from paused state
|
||||
if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && currentSong.url === this.nowPlaying?.url) {
|
||||
if (this.status === STATUS.PAUSED && currentSong.url === this.nowPlaying?.url) {
|
||||
if (this.dispatcher) {
|
||||
this.dispatcher.resume();
|
||||
this.status = STATUS.PLAYING;
|
||||
|
@ -109,20 +123,25 @@ export default class {
|
|||
return this.seek(this.getPosition());
|
||||
}
|
||||
|
||||
const stream = await this.getStream(currentSong.url);
|
||||
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
|
||||
try {
|
||||
const stream = await this.getStream(currentSong.url);
|
||||
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
|
||||
|
||||
this.attachListeners();
|
||||
this.attachListeners();
|
||||
|
||||
this.status = STATUS.PLAYING;
|
||||
this.nowPlaying = currentSong;
|
||||
this.status = STATUS.PLAYING;
|
||||
this.nowPlaying = currentSong;
|
||||
|
||||
if (currentSong.url === this.lastSongURL) {
|
||||
this.startTrackingPosition();
|
||||
} else {
|
||||
// Reset position counter
|
||||
this.startTrackingPosition(0);
|
||||
this.lastSongURL = currentSong.url;
|
||||
if (currentSong.url === this.lastSongURL) {
|
||||
this.startTrackingPosition();
|
||||
} else {
|
||||
// Reset position counter
|
||||
this.startTrackingPosition(0);
|
||||
this.lastSongURL = currentSong.url;
|
||||
}
|
||||
} catch (error) {
|
||||
this.removeCurrent();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,8 +159,103 @@ export default class {
|
|||
this.stopTrackingPosition();
|
||||
}
|
||||
|
||||
resetPosition(): void {
|
||||
this.positionInSeconds = 0;
|
||||
async forward(): Promise<void> {
|
||||
if (this.queuePosition < this.queueSize() + 1) {
|
||||
this.queuePosition++;
|
||||
|
||||
try {
|
||||
if (this.getCurrent() && this.status !== STATUS.PAUSED) {
|
||||
await this.play();
|
||||
} else {
|
||||
this.status = STATUS.PAUSED;
|
||||
this.disconnect();
|
||||
}
|
||||
} catch (error) {
|
||||
this.queuePosition--;
|
||||
throw error;
|
||||
}
|
||||
} else {
|
||||
throw new Error('No songs in queue to forward to.');
|
||||
}
|
||||
}
|
||||
|
||||
async back(): Promise<void> {
|
||||
if (this.queuePosition - 1 >= 0) {
|
||||
this.queuePosition--;
|
||||
this.positionInSeconds = 0;
|
||||
|
||||
if (this.status !== STATUS.PAUSED) {
|
||||
await this.play();
|
||||
}
|
||||
} else {
|
||||
throw new Error('No songs in queue to go back to.');
|
||||
}
|
||||
}
|
||||
|
||||
getCurrent(): QueuedSong | null {
|
||||
if (this.queue[this.queuePosition]) {
|
||||
return this.queue[this.queuePosition];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
getQueue(): QueuedSong[] {
|
||||
return this.queue.slice(this.queuePosition + 1);
|
||||
}
|
||||
|
||||
add(song: QueuedSong, {immediate = false} = {}): void {
|
||||
if (song.playlist) {
|
||||
// Add to end of queue
|
||||
this.queue.push(song);
|
||||
} else {
|
||||
// Not from playlist, add immediately
|
||||
let insertAt = this.queuePosition + 1;
|
||||
|
||||
if (!immediate) {
|
||||
// Loop until playlist song
|
||||
this.queue.some(song => {
|
||||
if (song.playlist) {
|
||||
return true;
|
||||
}
|
||||
|
||||
insertAt++;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
|
||||
}
|
||||
}
|
||||
|
||||
shuffle(): void {
|
||||
this.queue = [...this.queue.slice(0, this.queuePosition + 1), ...shuffle(this.queue.slice(this.queuePosition + 1))];
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
const newQueue = [];
|
||||
|
||||
// Don't clear curently playing song
|
||||
const current = this.getCurrent();
|
||||
|
||||
if (current) {
|
||||
newQueue.push(current);
|
||||
}
|
||||
|
||||
this.queuePosition = 0;
|
||||
this.queue = newQueue;
|
||||
}
|
||||
|
||||
removeCurrent(): void {
|
||||
this.queue = [...this.queue.slice(0, this.queuePosition), ...this.queue.slice(this.queuePosition + 1)];
|
||||
}
|
||||
|
||||
queueSize(): number {
|
||||
return this.getQueue().length;
|
||||
}
|
||||
|
||||
isQueueEmpty(): boolean {
|
||||
return this.queueSize() === 0;
|
||||
}
|
||||
|
||||
private getCachedPath(url: string): string {
|
||||
|
@ -271,30 +385,23 @@ export default class {
|
|||
return;
|
||||
}
|
||||
|
||||
this.voiceConnection.on('disconnect', () => {
|
||||
this.disconnect(false);
|
||||
});
|
||||
this.voiceConnection.on('disconnect', this.onVoiceConnectionDisconnect.bind(this));
|
||||
|
||||
if (!this.dispatcher) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.dispatcher.on('speaking', async isSpeaking => {
|
||||
// Automatically advance queued song at end
|
||||
if (!isSpeaking && this.status === STATUS.PLAYING) {
|
||||
if (this.queue.size() >= 0) {
|
||||
this.queue.forward();
|
||||
this.dispatcher.on('speaking', this.onVoiceConnectionSpeaking.bind(this));
|
||||
}
|
||||
|
||||
this.positionInSeconds = 0;
|
||||
private onVoiceConnectionDisconnect(): void {
|
||||
this.disconnect(false);
|
||||
}
|
||||
|
||||
if (this.queue.getCurrent()) {
|
||||
await this.play();
|
||||
} else {
|
||||
this.status = STATUS.PAUSED;
|
||||
this.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
private async onVoiceConnectionSpeaking(isSpeaking: boolean): Promise<void> {
|
||||
// Automatically advance queued song at end
|
||||
if (!isSpeaking && this.status === STATUS.PLAYING) {
|
||||
await this.forward();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,97 +0,0 @@
|
|||
import shuffle from 'array-shuffle';
|
||||
|
||||
export interface QueuedPlaylist {
|
||||
title: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface QueuedSong {
|
||||
title: string;
|
||||
artist: string;
|
||||
url: string;
|
||||
length: number;
|
||||
playlist: QueuedPlaylist | null;
|
||||
isLive: boolean;
|
||||
}
|
||||
|
||||
export default class {
|
||||
private queue: QueuedSong[] = [];
|
||||
private position = 0;
|
||||
|
||||
forward(): void {
|
||||
if (this.position < this.size() + 1) {
|
||||
this.position++;
|
||||
} else {
|
||||
throw new Error('No songs in queue to forward to.');
|
||||
}
|
||||
}
|
||||
|
||||
back(): void {
|
||||
if (this.position - 1 >= 0) {
|
||||
this.position--;
|
||||
} else {
|
||||
throw new Error('No songs in queue to go back to.');
|
||||
}
|
||||
}
|
||||
|
||||
getCurrent(): QueuedSong | null {
|
||||
if (this.queue[this.position]) {
|
||||
return this.queue[this.position];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
get(): QueuedSong[] {
|
||||
return this.queue.slice(this.position + 1);
|
||||
}
|
||||
|
||||
add(song: QueuedSong): void {
|
||||
if (song.playlist) {
|
||||
// Add to end of queue
|
||||
this.queue.push(song);
|
||||
} else {
|
||||
// Not from playlist, add immediately
|
||||
let insertAt = this.position;
|
||||
|
||||
// Loop until playlist song
|
||||
this.queue.some(song => {
|
||||
if (song.playlist) {
|
||||
return true;
|
||||
}
|
||||
|
||||
insertAt++;
|
||||
return false;
|
||||
});
|
||||
|
||||
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
|
||||
}
|
||||
}
|
||||
|
||||
shuffle(): void {
|
||||
this.queue = [...this.queue.slice(0, this.position), this.queue[this.position], this.queue[0], ...shuffle(this.queue.slice(this.position + 1))];
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
const newQueue = [];
|
||||
|
||||
// Don't clear curently playing song
|
||||
if (this.queue.length > 0) {
|
||||
newQueue.push(this.queue[this.position]);
|
||||
}
|
||||
|
||||
this.queue = newQueue;
|
||||
}
|
||||
|
||||
removeCurrent(): void {
|
||||
this.queue = [...this.queue.slice(0, this.position), ...this.queue.slice(this.position + 1)];
|
||||
}
|
||||
|
||||
size(): number {
|
||||
return this.get().length;
|
||||
}
|
||||
|
||||
isEmpty(): boolean {
|
||||
return this.get().length === 0;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue