mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-04-18 20:43:55 +02:00
Use manager instances for guild services
This commit is contained in:
parent
0cebca7917
commit
3408c7a0c2
13 changed files with 178 additions and 162 deletions
|
@ -54,8 +54,7 @@
|
||||||
"rules": {
|
"rules": {
|
||||||
"new-cap": "off",
|
"new-cap": "off",
|
||||||
"@typescript-eslint/no-unused-vars": "off",
|
"@typescript-eslint/no-unused-vars": "off",
|
||||||
"@typescript-eslint/no-unused-vars-experimental": "error",
|
"@typescript-eslint/no-unused-vars-experimental": "error"
|
||||||
"@typescript-eslint/no-inferrable-types": "off"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"husky": {
|
"husky": {
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
import {Message} from 'discord.js';
|
import {Message} from 'discord.js';
|
||||||
import {TYPES} from '../types';
|
import {TYPES} from '../types';
|
||||||
import {inject, injectable} from 'inversify';
|
import {inject, injectable} from 'inversify';
|
||||||
import Queue from '../services/queue';
|
import QueueManager from '../managers/queue';
|
||||||
import Command from '.';
|
import Command from '.';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export default class implements Command {
|
export default class implements Command {
|
||||||
public name = 'clear';
|
public name = 'clear';
|
||||||
public description = 'clears all songs in queue (except currently playing)';
|
public description = 'clears all songs in queue (except currently playing)';
|
||||||
private readonly queue: Queue;
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Services.Queue) queue: Queue) {
|
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
||||||
this.queue = queue;
|
this.queueManager = queueManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execute(msg: Message, _: string []): Promise<void> {
|
public async execute(msg: Message, _: string []): Promise<void> {
|
||||||
this.queue.clear(msg.guild!.id);
|
this.queueManager.get(msg.guild!.id).clear();
|
||||||
|
|
||||||
await msg.channel.send('cleared');
|
await msg.channel.send('cleared');
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,9 @@ import got from 'got';
|
||||||
import {parse, toSeconds} from 'iso8601-duration';
|
import {parse, toSeconds} from 'iso8601-duration';
|
||||||
import {TYPES} from '../types';
|
import {TYPES} from '../types';
|
||||||
import {inject, injectable} from 'inversify';
|
import {inject, injectable} from 'inversify';
|
||||||
import Queue, {QueuedSong, QueuedPlaylist} from '../services/queue';
|
import {QueuedSong, QueuedPlaylist} from '../services/queue';
|
||||||
import Player from '../services/player';
|
import QueueManager from '../managers/queue';
|
||||||
|
import PlayerManager from '../managers/player';
|
||||||
import {getMostPopularVoiceChannel} from '../utils/channels';
|
import {getMostPopularVoiceChannel} from '../utils/channels';
|
||||||
import LoadingMessage from '../utils/loading-message';
|
import LoadingMessage from '../utils/loading-message';
|
||||||
import Command from '.';
|
import Command from '.';
|
||||||
|
@ -19,15 +20,15 @@ import Command from '.';
|
||||||
export default class implements Command {
|
export default class implements Command {
|
||||||
public name = 'play';
|
public name = 'play';
|
||||||
public description = 'plays a song';
|
public description = 'plays a song';
|
||||||
private readonly queue: Queue;
|
private readonly queueManager: QueueManager;
|
||||||
private readonly player: Player;
|
private readonly playerManager: PlayerManager;
|
||||||
private readonly youtube: YouTube;
|
private readonly youtube: YouTube;
|
||||||
private readonly youtubeKey: string;
|
private readonly youtubeKey: string;
|
||||||
private readonly spotify: Spotify;
|
private readonly spotify: Spotify;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Services.Queue) queue: Queue, @inject(TYPES.Services.Player) player: Player, @inject(TYPES.Lib.YouTube) youtube: YouTube, @inject(TYPES.Config.YOUTUBE_API_KEY) youtubeKey: string, @inject(TYPES.Lib.Spotify) spotify: Spotify) {
|
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager, @inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Lib.YouTube) youtube: YouTube, @inject(TYPES.Config.YOUTUBE_API_KEY) youtubeKey: string, @inject(TYPES.Lib.Spotify) spotify: Spotify) {
|
||||||
this.queue = queue;
|
this.queueManager = queueManager;
|
||||||
this.player = player;
|
this.playerManager = playerManager;
|
||||||
this.youtube = youtube;
|
this.youtube = youtube;
|
||||||
this.youtubeKey = youtubeKey;
|
this.youtubeKey = youtubeKey;
|
||||||
this.spotify = spotify;
|
this.spotify = spotify;
|
||||||
|
@ -208,7 +209,7 @@ export default class implements Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
newSongs.forEach(song => this.queue.add(msg.guild!.id, song));
|
newSongs.forEach(song => this.queueManager.get(msg.guild!.id).add(song));
|
||||||
|
|
||||||
// TODO: better response
|
// TODO: better response
|
||||||
await res.stop('song(s) queued');
|
await res.stop('song(s) queued');
|
||||||
|
@ -216,8 +217,8 @@ export default class implements Command {
|
||||||
const channel = getMostPopularVoiceChannel(msg.guild!);
|
const channel = getMostPopularVoiceChannel(msg.guild!);
|
||||||
|
|
||||||
// TODO: don't connect if already connected.
|
// TODO: don't connect if already connected.
|
||||||
await this.player.connect(msg.guild!.id, channel);
|
await this.playerManager.get(msg.guild!.id).connect(channel);
|
||||||
|
|
||||||
await this.player.play(msg.guild!.id);
|
await this.playerManager.get(msg.guild!.id).play();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
import {Message} from 'discord.js';
|
import {Message} from 'discord.js';
|
||||||
import {TYPES} from '../types';
|
import {TYPES} from '../types';
|
||||||
import {inject, injectable} from 'inversify';
|
import {inject, injectable} from 'inversify';
|
||||||
import Queue from '../services/queue';
|
import QueueManager from '../managers/queue';
|
||||||
import Command from '.';
|
import Command from '.';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export default class implements Command {
|
export default class implements Command {
|
||||||
public name = 'queue';
|
public name = 'queue';
|
||||||
public description = 'shows current queue';
|
public description = 'shows current queue';
|
||||||
private readonly queue: Queue;
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Services.Queue) queue: Queue) {
|
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
||||||
this.queue = queue;
|
this.queueManager = queueManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execute(msg: Message, _: string []): Promise<void> {
|
public async execute(msg: Message, _: string []): Promise<void> {
|
||||||
const queue = this.queue.get(msg.guild!.id);
|
const queue = this.queueManager.get(msg.guild!.id).get();
|
||||||
|
|
||||||
await msg.channel.send('`' + JSON.stringify(queue.slice(0, 10)) + '`');
|
await msg.channel.send('`' + JSON.stringify(queue.slice(0, 10)) + '`');
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import {Message, TextChannel} from 'discord.js';
|
import {Message, TextChannel} from 'discord.js';
|
||||||
import {TYPES} from '../types';
|
import {TYPES} from '../types';
|
||||||
import {inject, injectable} from 'inversify';
|
import {inject, injectable} from 'inversify';
|
||||||
import Player from '../services/player';
|
import PlayerManager from '../managers/player';
|
||||||
import LoadingMessage from '../utils/loading-message';
|
import LoadingMessage from '../utils/loading-message';
|
||||||
import Command from '.';
|
import Command from '.';
|
||||||
|
|
||||||
|
@ -9,10 +9,10 @@ import Command from '.';
|
||||||
export default class implements Command {
|
export default class implements Command {
|
||||||
public name = 'seek';
|
public name = 'seek';
|
||||||
public description = 'seeks position in currently playing song';
|
public description = 'seeks position in currently playing song';
|
||||||
private readonly player: Player;
|
private readonly playerManager: PlayerManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Services.Player) player: Player) {
|
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
|
||||||
this.player = player;
|
this.playerManager = playerManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execute(msg: Message, args: string []): Promise<void> {
|
public async execute(msg: Message, args: string []): Promise<void> {
|
||||||
|
@ -31,7 +31,7 @@ export default class implements Command {
|
||||||
await loading.start();
|
await loading.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.player.seek(msg.guild!.id, seekTime);
|
await this.playerManager.get(msg.guild!.id).seek(seekTime);
|
||||||
|
|
||||||
await loading.stop('seeked');
|
await loading.stop('seeked');
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
import {Message} from 'discord.js';
|
import {Message} from 'discord.js';
|
||||||
import {TYPES} from '../types';
|
import {TYPES} from '../types';
|
||||||
import {inject, injectable} from 'inversify';
|
import {inject, injectable} from 'inversify';
|
||||||
import Queue from '../services/queue';
|
import QueueManager from '../managers/queue';
|
||||||
import Command from '.';
|
import Command from '.';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export default class implements Command {
|
export default class implements Command {
|
||||||
public name = 'shuffle';
|
public name = 'shuffle';
|
||||||
public description = 'shuffle current queue';
|
public description = 'shuffle current queue';
|
||||||
private readonly queue: Queue;
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Services.Queue) queue: Queue) {
|
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
||||||
this.queue = queue;
|
this.queueManager = queueManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async execute(msg: Message, _: string []): Promise<void> {
|
public async execute(msg: Message, _: string []): Promise<void> {
|
||||||
const queue = this.queue.get(msg.guild!.id);
|
const queue = this.queueManager.get(msg.guild!.id).get();
|
||||||
|
|
||||||
if (queue.length <= 2) {
|
if (queue.length <= 2) {
|
||||||
await msg.channel.send('error: not enough songs to shuffle');
|
await msg.channel.send('error: not enough songs to shuffle');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.queue.shuffle(msg.guild!.id);
|
this.queueManager.get(msg.guild!.id).shuffle();
|
||||||
|
|
||||||
await msg.channel.send('`' + JSON.stringify(this.queue.get(msg.guild!.id).slice(0, 10)) + '`');
|
await msg.channel.send('`' + JSON.stringify(this.queueManager.get(msg.guild!.id).get().slice(0, 10)) + '`');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,9 +15,9 @@ import {
|
||||||
CACHE_DIR
|
CACHE_DIR
|
||||||
} from './utils/config';
|
} from './utils/config';
|
||||||
|
|
||||||
// Services
|
// Managers
|
||||||
import Queue from './services/queue';
|
import PlayerManager from './managers/player';
|
||||||
import Player from './services/player';
|
import QueueManager from './managers/queue';
|
||||||
|
|
||||||
// Comands
|
// Comands
|
||||||
import Command from './commands';
|
import Command from './commands';
|
||||||
|
@ -34,9 +34,9 @@ let container = new Container();
|
||||||
container.bind<Bot>(TYPES.Bot).to(Bot).inSingletonScope();
|
container.bind<Bot>(TYPES.Bot).to(Bot).inSingletonScope();
|
||||||
container.bind<Client>(TYPES.Client).toConstantValue(new Client());
|
container.bind<Client>(TYPES.Client).toConstantValue(new Client());
|
||||||
|
|
||||||
// Services
|
// Managers
|
||||||
container.bind<Player>(TYPES.Services.Player).to(Player).inSingletonScope();
|
container.bind<PlayerManager>(TYPES.Managers.Player).to(PlayerManager).inSingletonScope();
|
||||||
container.bind<Queue>(TYPES.Services.Queue).to(Queue).inSingletonScope();
|
container.bind<QueueManager>(TYPES.Managers.Queue).to(QueueManager).inSingletonScope();
|
||||||
|
|
||||||
// Commands
|
// Commands
|
||||||
container.bind<Command>(TYPES.Command).to(Clear).inSingletonScope();
|
container.bind<Command>(TYPES.Command).to(Clear).inSingletonScope();
|
||||||
|
|
29
src/managers/player.ts
Normal file
29
src/managers/player.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import {inject, injectable} from 'inversify';
|
||||||
|
import {TYPES} from '../types';
|
||||||
|
import Player from '../services/player';
|
||||||
|
import QueueManager from './queue';
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export default class {
|
||||||
|
private readonly guildPlayers: Map<string, Player>;
|
||||||
|
private readonly cacheDir: string;
|
||||||
|
private readonly queueManager: QueueManager;
|
||||||
|
|
||||||
|
constructor(@inject(TYPES.Config.CACHE_DIR) cacheDir: string, @inject(TYPES.Managers.Queue) queueManager: QueueManager) {
|
||||||
|
this.guildPlayers = new Map();
|
||||||
|
this.cacheDir = cacheDir;
|
||||||
|
this.queueManager = queueManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
get(guildId: string): Player {
|
||||||
|
let player = this.guildPlayers.get(guildId);
|
||||||
|
|
||||||
|
if (!player) {
|
||||||
|
player = new Player(this.queueManager.get(guildId), this.cacheDir);
|
||||||
|
|
||||||
|
this.guildPlayers.set(guildId, player);
|
||||||
|
}
|
||||||
|
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
}
|
23
src/managers/queue.ts
Normal file
23
src/managers/queue.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import {injectable} from 'inversify';
|
||||||
|
import Queue from '../services/queue';
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export default class {
|
||||||
|
private readonly guildQueues: Map<string, Queue>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.guildQueues = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
get(guildId: string): Queue {
|
||||||
|
let queue = this.guildQueues.get(guildId);
|
||||||
|
|
||||||
|
if (!queue) {
|
||||||
|
queue = new Queue();
|
||||||
|
|
||||||
|
this.guildQueues.set(guildId, queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
import {inject, injectable} from 'inversify';
|
import {VoiceConnection, VoiceChannel, StreamDispatcher} from 'discord.js';
|
||||||
import {VoiceConnection, VoiceChannel} from 'discord.js';
|
|
||||||
import {promises as fs, createWriteStream} from 'fs';
|
import {promises as fs, createWriteStream} from 'fs';
|
||||||
import {Readable, PassThrough} from 'stream';
|
import {Readable, PassThrough} from 'stream';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
@ -7,60 +6,44 @@ import hasha from 'hasha';
|
||||||
import ytdl from 'ytdl-core';
|
import ytdl from 'ytdl-core';
|
||||||
import {WriteStream} from 'fs-capacitor';
|
import {WriteStream} from 'fs-capacitor';
|
||||||
import ffmpeg from 'fluent-ffmpeg';
|
import ffmpeg from 'fluent-ffmpeg';
|
||||||
import {TYPES} from '../types';
|
|
||||||
import Queue, {QueuedSong} from './queue';
|
import Queue, {QueuedSong} from './queue';
|
||||||
|
|
||||||
export enum Status {
|
export enum STATUS {
|
||||||
Playing,
|
PLAYING,
|
||||||
Paused,
|
PAUSED,
|
||||||
Disconnected
|
DISCONNECTED
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GuildPlayer {
|
|
||||||
status: Status;
|
|
||||||
voiceConnection: VoiceConnection | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@injectable()
|
|
||||||
export default class {
|
export default class {
|
||||||
private readonly guildPlayers = new Map<string, GuildPlayer>();
|
public status = STATUS.DISCONNECTED;
|
||||||
private readonly queue: Queue;
|
private readonly queue: Queue;
|
||||||
private readonly cacheDir: string;
|
private readonly cacheDir: string;
|
||||||
|
private voiceConnection: VoiceConnection | null = null;
|
||||||
|
private dispatcher: StreamDispatcher | null = null;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Services.Queue) queue: Queue, @inject(TYPES.Config.CACHE_DIR) cacheDir: string) {
|
constructor(queue: Queue, cacheDir: string) {
|
||||||
this.queue = queue;
|
this.queue = queue;
|
||||||
this.cacheDir = cacheDir;
|
this.cacheDir = cacheDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect(guildId: string, channel: VoiceChannel): Promise<void> {
|
async connect(channel: VoiceChannel): Promise<void> {
|
||||||
this.initGuild(guildId);
|
|
||||||
|
|
||||||
const guildPlayer = this.guildPlayers.get(guildId);
|
|
||||||
|
|
||||||
const conn = await channel.join();
|
const conn = await channel.join();
|
||||||
|
|
||||||
guildPlayer!.voiceConnection = conn;
|
this.voiceConnection = conn;
|
||||||
|
|
||||||
this.guildPlayers.set(guildId, guildPlayer!);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect(guildId: string): void {
|
disconnect(): void {
|
||||||
this.initGuild(guildId);
|
if (this.voiceConnection) {
|
||||||
|
this.voiceConnection.disconnect();
|
||||||
const guildPlayer = this.guildPlayers.get(guildId);
|
|
||||||
|
|
||||||
if (guildPlayer?.voiceConnection) {
|
|
||||||
guildPlayer.voiceConnection.disconnect();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async seek(guildId: string, positionSeconds: number): Promise<void> {
|
async seek(positionSeconds: number): Promise<void> {
|
||||||
const guildPlayer = this.get(guildId);
|
if (this.voiceConnection === null) {
|
||||||
if (guildPlayer.voiceConnection === null) {
|
|
||||||
throw new Error('Not connected to a voice channel.');
|
throw new Error('Not connected to a voice channel.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentSong = this.getCurrentSong(guildId);
|
const currentSong = this.getCurrentSong();
|
||||||
|
|
||||||
if (!currentSong) {
|
if (!currentSong) {
|
||||||
throw new Error('No song currently playing');
|
throw new Error('No song currently playing');
|
||||||
|
@ -68,46 +51,52 @@ export default class {
|
||||||
|
|
||||||
await this.waitForCache(currentSong.url);
|
await this.waitForCache(currentSong.url);
|
||||||
|
|
||||||
guildPlayer.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds});
|
this.attachListeners(this.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async play(guildId: string): Promise<void> {
|
async play(): Promise<void> {
|
||||||
const guildPlayer = this.get(guildId);
|
if (this.voiceConnection === null) {
|
||||||
if (guildPlayer.voiceConnection === null) {
|
|
||||||
throw new Error('Not connected to a voice channel.');
|
throw new Error('Not connected to a voice channel.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (guildPlayer.status === Status.Playing) {
|
// Resume from paused state
|
||||||
// Already playing, return
|
if (this.status === STATUS.PAUSED && this.dispatcher) {
|
||||||
|
this.dispatcher.resume();
|
||||||
|
this.status = STATUS.PLAYING;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentSong = this.getCurrentSong(guildId);
|
const currentSong = this.getCurrentSong();
|
||||||
|
|
||||||
if (!currentSong) {
|
if (!currentSong) {
|
||||||
throw new Error('Queue empty.');
|
throw new Error('Queue empty.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let dispatcher: StreamDispatcher;
|
||||||
|
|
||||||
if (await this.isCached(currentSong.url)) {
|
if (await this.isCached(currentSong.url)) {
|
||||||
this.get(guildId).voiceConnection!.play(this.getCachedPath(currentSong.url));
|
dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url));
|
||||||
} else {
|
} else {
|
||||||
const stream = await this.getStream(currentSong.url);
|
const stream = await this.getStream(currentSong.url);
|
||||||
this.get(guildId).voiceConnection!.play(stream, {type: 'webm/opus'});
|
dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
|
||||||
}
|
}
|
||||||
|
|
||||||
guildPlayer.status = Status.Playing;
|
this.attachListeners(dispatcher);
|
||||||
|
|
||||||
this.guildPlayers.set(guildId, guildPlayer);
|
this.status = STATUS.PLAYING;
|
||||||
|
this.dispatcher = dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
get(guildId: string): GuildPlayer {
|
pause(): void {
|
||||||
this.initGuild(guildId);
|
if (!this.dispatcher || this.status !== STATUS.PLAYING) {
|
||||||
|
throw new Error('Not currently playing.');
|
||||||
|
}
|
||||||
|
|
||||||
return this.guildPlayers.get(guildId) as GuildPlayer;
|
this.dispatcher.pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
private getCurrentSong(guildId: string): QueuedSong|null {
|
private getCurrentSong(): QueuedSong|null {
|
||||||
const songs = this.queue.get(guildId);
|
const songs = this.queue.get();
|
||||||
|
|
||||||
if (songs.length === 0) {
|
if (songs.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -116,12 +105,6 @@ export default class {
|
||||||
return songs[0];
|
return songs[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
private initGuild(guildId: string): void {
|
|
||||||
if (!this.guildPlayers.get(guildId)) {
|
|
||||||
this.guildPlayers.set(guildId, {status: Status.Disconnected, voiceConnection: null});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private getCachedPath(url: string): string {
|
private getCachedPath(url: string): string {
|
||||||
const hash = hasha(url);
|
const hash = hasha(url);
|
||||||
return path.join(this.cacheDir, `${hash}.webm`);
|
return path.join(this.cacheDir, `${hash}.webm`);
|
||||||
|
@ -238,4 +221,23 @@ export default class {
|
||||||
|
|
||||||
return capacitor.createReadStream();
|
return capacitor.createReadStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private attachListeners(stream: StreamDispatcher): void {
|
||||||
|
stream.on('speaking', async isSpeaking => {
|
||||||
|
// Automatically advance queued song at end
|
||||||
|
if (!isSpeaking && this.status === STATUS.PLAYING) {
|
||||||
|
if (this.queue.get().length > 0) {
|
||||||
|
this.queue.forward();
|
||||||
|
await this.play();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
stream.on('close', () => {
|
||||||
|
// Remove dispatcher from guild player
|
||||||
|
this.dispatcher = null;
|
||||||
|
|
||||||
|
// TODO: set voiceConnection null as well?
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import {injectable} from 'inversify';
|
|
||||||
import shuffle from 'array-shuffle';
|
import shuffle from 'array-shuffle';
|
||||||
|
|
||||||
export interface QueuedPlaylist {
|
export interface QueuedPlaylist {
|
||||||
|
@ -14,62 +13,40 @@ export interface QueuedSong {
|
||||||
playlist: QueuedPlaylist | null;
|
playlist: QueuedPlaylist | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@injectable()
|
|
||||||
export default class {
|
export default class {
|
||||||
private readonly guildQueues = new Map<string, QueuedSong[]>();
|
private queue: QueuedSong[] = [];
|
||||||
private readonly queuePositions = new Map<string, number>();
|
private position = 0;
|
||||||
|
|
||||||
forward(guildId: string): void {
|
forward(): void {
|
||||||
const currentPosition = this.queuePositions.get(guildId);
|
if (this.position + 1 <= this.size()) {
|
||||||
|
this.position++;
|
||||||
if (currentPosition && currentPosition + 1 <= this.size(guildId)) {
|
|
||||||
this.queuePositions.set(guildId, currentPosition + 1);
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error('No songs in queue to forward to.');
|
throw new Error('No songs in queue to forward to.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
back(guildId: string): void {
|
back(): void {
|
||||||
const currentPosition = this.queuePositions.get(guildId);
|
if (this.position - 1 >= 0) {
|
||||||
|
this.position--;
|
||||||
if (currentPosition && currentPosition - 1 >= 0) {
|
|
||||||
this.queuePositions.set(guildId, currentPosition - 1);
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error('No songs in queue to go back to.');
|
throw new Error('No songs in queue to go back to.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get(guildId: string): QueuedSong[] {
|
get(): QueuedSong[] {
|
||||||
const currentPosition = this.queuePositions.get(guildId);
|
return this.queue.slice(this.position);
|
||||||
|
|
||||||
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 {
|
add(song: QueuedSong): void {
|
||||||
this.initQueue(guildId);
|
|
||||||
|
|
||||||
if (song.playlist) {
|
if (song.playlist) {
|
||||||
// Add to end of queue
|
// Add to end of queue
|
||||||
this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!, song]);
|
this.queue.push(song);
|
||||||
} else if (this.guildQueues.get(guildId)!.length === 0) {
|
|
||||||
// Queue is currently empty
|
|
||||||
this.guildQueues.set(guildId, [song]);
|
|
||||||
} else {
|
} else {
|
||||||
// Not from playlist, add immediately
|
// Not from playlist, add immediately
|
||||||
let insertAt = 0;
|
let insertAt = 0;
|
||||||
|
|
||||||
// Loop until playlist song
|
// Loop until playlist song
|
||||||
this.guildQueues.get(guildId)!.some(song => {
|
this.queue.some(song => {
|
||||||
if (song.playlist) {
|
if (song.playlist) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -78,41 +55,26 @@ export default class {
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!.slice(0, insertAt), song, ...this.guildQueues.get(guildId)!.slice(insertAt)]);
|
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shuffle(guildId: string): void {
|
shuffle(): void {
|
||||||
const queue = this.guildQueues.get(guildId);
|
this.queue = [this.queue[0], ...shuffle(this.queue.slice(1))];
|
||||||
|
|
||||||
if (!queue) {
|
|
||||||
throw new Error('Queue doesn\'t exist yet.');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.guildQueues.set(guildId, [queue[0], ...shuffle(queue.slice(1))]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clear(guildId: string): void {
|
clear(): void {
|
||||||
this.initQueue(guildId);
|
|
||||||
const queue = this.guildQueues.get(guildId);
|
|
||||||
|
|
||||||
const newQueue = [];
|
const newQueue = [];
|
||||||
|
|
||||||
if (queue!.length > 0) {
|
// Don't clear curently playing song
|
||||||
newQueue.push(queue![0]);
|
if (this.queue.length > 0) {
|
||||||
|
newQueue.push(this.queue[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.guildQueues.set(guildId, newQueue);
|
this.queue = newQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
size(guildId: string): number {
|
size(): number {
|
||||||
return this.get(guildId).length;
|
return this.queue.length;
|
||||||
}
|
|
||||||
|
|
||||||
private initQueue(guildId: string): void {
|
|
||||||
if (!this.guildQueues.get(guildId)) {
|
|
||||||
this.guildQueues.set(guildId, []);
|
|
||||||
this.queuePositions.set(guildId, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,12 @@ export const TYPES = {
|
||||||
CACHE_DIR: Symbol('CACHE_DIR')
|
CACHE_DIR: Symbol('CACHE_DIR')
|
||||||
},
|
},
|
||||||
Command: Symbol('Command'),
|
Command: Symbol('Command'),
|
||||||
Services: {
|
|
||||||
Player: Symbol('Player'),
|
|
||||||
Queue: Symbol('Queue')
|
|
||||||
},
|
|
||||||
Lib: {
|
Lib: {
|
||||||
YouTube: Symbol('YouTube'),
|
YouTube: Symbol('YouTube'),
|
||||||
Spotify: Symbol('Spotify')
|
Spotify: Symbol('Spotify')
|
||||||
|
},
|
||||||
|
Managers: {
|
||||||
|
Player: Symbol('PlayerManager'),
|
||||||
|
Queue: Symbol('QueueManager')
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@ export default class {
|
||||||
private readonly channel: TextChannel;
|
private readonly channel: TextChannel;
|
||||||
private readonly text: string;
|
private readonly text: string;
|
||||||
private msg!: Message;
|
private msg!: Message;
|
||||||
private isStopped: boolean = false;
|
private isStopped = false;
|
||||||
|
|
||||||
constructor(channel: TextChannel, text: string) {
|
constructor(channel: TextChannel, text: string) {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
|
|
Loading…
Add table
Reference in a new issue