mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-05-10 04:01:37 +02:00
Use IoC, impliment queue
This commit is contained in:
parent
8eb4c8a6c0
commit
17ba78f7b7
17 changed files with 1081 additions and 131 deletions
88
src/services/player.ts
Normal file
88
src/services/player.ts
Normal file
|
@ -0,0 +1,88 @@
|
|||
import {inject, injectable} from 'inversify';
|
||||
import {VoiceConnection, VoiceChannel} from 'discord.js';
|
||||
import {TYPES} from '../types';
|
||||
import Queue from './queue';
|
||||
import getYouTubeStream from '../utils/get-youtube-stream';
|
||||
|
||||
export enum Status {
|
||||
Playing,
|
||||
Paused,
|
||||
Disconnected
|
||||
}
|
||||
|
||||
export interface GuildPlayer {
|
||||
status: Status;
|
||||
voiceConnection: VoiceConnection | null;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
export default class {
|
||||
private readonly guildPlayers = new Map<string, GuildPlayer>();
|
||||
private readonly queue: Queue;
|
||||
|
||||
constructor(@inject(TYPES.Services.Queue) queue: Queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
async connect(guildId: string, channel: VoiceChannel): Promise<void> {
|
||||
this.initGuild(guildId);
|
||||
|
||||
const guildPlayer = this.guildPlayers.get(guildId);
|
||||
|
||||
const conn = await channel.join();
|
||||
|
||||
guildPlayer!.voiceConnection = conn;
|
||||
|
||||
this.guildPlayers.set(guildId, guildPlayer!);
|
||||
}
|
||||
|
||||
disconnect(guildId: string): void {
|
||||
this.initGuild(guildId);
|
||||
|
||||
const guildPlayer = this.guildPlayers.get(guildId);
|
||||
|
||||
if (guildPlayer?.voiceConnection) {
|
||||
guildPlayer.voiceConnection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
async play(guildId: string): Promise<void> {
|
||||
const guildPlayer = this.get(guildId);
|
||||
if (guildPlayer.voiceConnection === null) {
|
||||
throw new Error('Not connected to a voice channel.');
|
||||
}
|
||||
|
||||
if (guildPlayer.status === Status.Playing) {
|
||||
// Already playing, return
|
||||
return;
|
||||
}
|
||||
|
||||
const songs = this.queue.get(guildId);
|
||||
|
||||
if (songs.length === 0) {
|
||||
throw new Error('Queue empty.');
|
||||
}
|
||||
|
||||
const song = songs[0];
|
||||
|
||||
const stream = await getYouTubeStream(song.url);
|
||||
|
||||
this.get(guildId).voiceConnection!.play(stream, {type: 'webm/opus'});
|
||||
|
||||
guildPlayer.status = Status.Playing;
|
||||
|
||||
this.guildPlayers.set(guildId, guildPlayer);
|
||||
}
|
||||
|
||||
get(guildId: string): GuildPlayer {
|
||||
this.initGuild(guildId);
|
||||
|
||||
return this.guildPlayers.get(guildId) as GuildPlayer;
|
||||
}
|
||||
|
||||
private initGuild(guildId: string): void {
|
||||
if (!this.guildPlayers.get(guildId)) {
|
||||
this.guildPlayers.set(guildId, {status: Status.Disconnected, voiceConnection: null});
|
||||
}
|
||||
}
|
||||
}
|
89
src/services/queue.ts
Normal file
89
src/services/queue.ts
Normal file
|
@ -0,0 +1,89 @@
|
|||
import {injectable} from 'inversify';
|
||||
|
||||
export interface QueuedPlaylist {
|
||||
title: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface QueuedSong {
|
||||
title: string;
|
||||
url: string;
|
||||
length: number;
|
||||
playlist: QueuedPlaylist | null;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
export default class {
|
||||
private readonly guildQueues = new Map<string, QueuedSong[]>();
|
||||
private readonly queuePositions = new Map<string, number>();
|
||||
|
||||
forward(guildId: string): void {
|
||||
const currentPosition = this.queuePositions.get(guildId);
|
||||
|
||||
if (currentPosition && currentPosition + 1 <= this.size(guildId)) {
|
||||
this.queuePositions.set(guildId, currentPosition + 1);
|
||||
} else {
|
||||
throw new Error('No songs in queue to forward to.');
|
||||
}
|
||||
}
|
||||
|
||||
back(guildId: string): void {
|
||||
const currentPosition = this.queuePositions.get(guildId);
|
||||
|
||||
if (currentPosition && currentPosition - 1 >= 0) {
|
||||
this.queuePositions.set(guildId, currentPosition - 1);
|
||||
} else {
|
||||
throw new Error('No songs in queue to go back to.');
|
||||
}
|
||||
}
|
||||
|
||||
get(guildId: string): QueuedSong[] {
|
||||
const currentPosition = this.queuePositions.get(guildId);
|
||||
|
||||
if (currentPosition === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const guildQueue = this.guildQueues.get(guildId);
|
||||
|
||||
if (!guildQueue) {
|
||||
throw new Error('Bad state. Queue for guild exists but position does not.');
|
||||
}
|
||||
|
||||
return guildQueue.slice(currentPosition);
|
||||
}
|
||||
|
||||
add(guildId: string, song: QueuedSong): void {
|
||||
if (!this.guildQueues.get(guildId)) {
|
||||
this.guildQueues.set(guildId, []);
|
||||
this.queuePositions.set(guildId, 0);
|
||||
}
|
||||
|
||||
if (song.playlist) {
|
||||
// Add to end of queue
|
||||
this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!, song]);
|
||||
} else if (this.guildQueues.get(guildId)!.length === 0) {
|
||||
// Queue is currently empty
|
||||
this.guildQueues.set(guildId, [song]);
|
||||
} else {
|
||||
// Not from playlist, add immediately
|
||||
let insertAt = 0;
|
||||
|
||||
// Loop until playlist song
|
||||
this.guildQueues.get(guildId)!.some(song => {
|
||||
if (song.playlist) {
|
||||
return true;
|
||||
}
|
||||
|
||||
insertAt++;
|
||||
return false;
|
||||
});
|
||||
|
||||
this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!.slice(0, insertAt), song, ...this.guildQueues.get(guildId)!.slice(insertAt)]);
|
||||
}
|
||||
}
|
||||
|
||||
size(guildId: string): number {
|
||||
return this.get(guildId).length;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue