fix player

This commit is contained in:
Snowflake107 2021-04-06 21:23:17 +05:45
parent fc8d0d08ee
commit c1a8de52f3
10 changed files with 106 additions and 92 deletions

View file

@ -7,7 +7,7 @@
"lib/**/*" "lib/**/*"
], ],
"scripts": { "scripts": {
"test": "cd test && node index.js", "test": "yarn build && cd test && node index.js",
"build": "tsc", "build": "tsc",
"format": "prettier --write \"src/**/*.ts\"", "format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json" "lint": "tslint -p tsconfig.json"
@ -47,7 +47,7 @@
"@discordjs/opus": "^0.5.0", "@discordjs/opus": "^0.5.0",
"@types/node": "^14.14.37", "@types/node": "^14.14.37",
"@types/ws": "^7.4.1", "@types/ws": "^7.4.1",
"discord.js": "discordjs/discord.js", "discord.js": "^12.5.3",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"tslint": "^6.1.3", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0", "tslint-config-prettier": "^1.18.0",

View file

@ -1,11 +1,10 @@
import YouTube from 'youtube-sr';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { Client, Collection, Snowflake, Collector, Message } from 'discord.js'; import { Client, Collection, Snowflake, Collector, Message } from 'discord.js';
import { PlayerOptions } from './types/types'; import { PlayerOptions, QueueFilters } from './types/types';
import Util from './utils/Util'; import Util from './utils/Util';
import AudioFilters from './utils/AudioFilters'; import AudioFilters from './utils/AudioFilters';
import Queue from './Structures/Queue'; import { Queue } from './Structures/Queue';
import Track from './Structures/Track'; import { Track } from './Structures/Track';
import { PlayerErrorEventCodes, PlayerEvents } from './utils/Constants'; import { PlayerErrorEventCodes, PlayerEvents } from './utils/Constants';
import PlayerError from './utils/PlayerError'; import PlayerError from './utils/PlayerError';
import ytdl from 'discord-ytdl-core'; import ytdl from 'discord-ytdl-core';
@ -17,7 +16,7 @@ import { Client as SoundCloudClient } from 'soundcloud-scraper';
const SoundCloud = new SoundCloudClient(); const SoundCloud = new SoundCloudClient();
export default class Player extends EventEmitter { export class Player extends EventEmitter {
public client!: Client; public client!: Client;
public options: PlayerOptions; public options: PlayerOptions;
public filters: typeof AudioFilters; public filters: typeof AudioFilters;
@ -53,6 +52,9 @@ export default class Player extends EventEmitter {
* Player queues * Player queues
*/ */
this.queues = new Collection(); this.queues = new Collection();
this._resultsCollectors = new Collection();
this._cooldownsTimeout = new Collection();
} }
static get AudioFilters() { static get AudioFilters() {
@ -146,7 +148,7 @@ export default class Player extends EventEmitter {
}); });
} }
async play(message: Message, query: string | Track, firstResult?: boolean) { async play(message: Message, query: string | Track, firstResult?: boolean): Promise<void> {
if (!message) throw new PlayerError('Play function needs message'); if (!message) throw new PlayerError('Play function needs message');
if (!query) throw new PlayerError('Play function needs search query as a string or Player.Track object'); if (!query) throw new PlayerError('Play function needs search query as a string or Player.Track object');
@ -162,9 +164,9 @@ export default class Player extends EventEmitter {
else { else {
if (ytdl.validateURL(query)) { if (ytdl.validateURL(query)) {
const info = await ytdl.getBasicInfo(query).catch(() => {}); const info = await ytdl.getBasicInfo(query).catch(() => {});
if (!info) return this.emit(PlayerEvents.NO_RESULTS, message, query); if (!info) return void this.emit(PlayerEvents.NO_RESULTS, message, query);
if (info.videoDetails.isLiveContent && !this.options.enableLive) if (info.videoDetails.isLiveContent && !this.options.enableLive)
return this.emit( return void this.emit(
PlayerEvents.ERROR, PlayerEvents.ERROR,
PlayerErrorEventCodes.LIVE_VIDEO, PlayerErrorEventCodes.LIVE_VIDEO,
message, message,
@ -208,6 +210,28 @@ export default class Player extends EventEmitter {
return this.queues.find((g) => g.guildID === message.guild.id); return this.queues.find((g) => g.guildID === message.guild.id);
} }
setFilters(message: Message, newFilters: QueueFilters): Promise<void> {
return new Promise((resolve) => {
const queue = this.queues.find((g) => g.guildID === message.guild.id);
if (!queue)
this.emit(
PlayerEvents.ERROR,
PlayerErrorEventCodes.NOT_PLAYING,
message,
new PlayerError('Not playing')
);
Object.keys(newFilters).forEach((filterName) => {
// @ts-ignore
queue.filters[filterName] = newFilters[filterName];
});
this._playStream(queue, true).then(() => {
resolve();
});
});
}
private _addTrackToQueue(message: Message, track: Track) { private _addTrackToQueue(message: Message, track: Track) {
const queue = this.getQueue(message); const queue = this.getQueue(message);
if (!queue) if (!queue)
@ -246,7 +270,7 @@ export default class Player extends EventEmitter {
queue.tracks.push(track); queue.tracks.push(track);
this.emit(PlayerEvents.QUEUE_CREATE, message, queue); this.emit(PlayerEvents.QUEUE_CREATE, message, queue);
resolve(queue); resolve(queue);
// this._playTrack(queue, true) this._playTrack(queue, true);
}) })
.catch((err) => { .catch((err) => {
this.queues.delete(message.guild.id); this.queues.delete(message.guild.id);
@ -260,7 +284,7 @@ export default class Player extends EventEmitter {
}); });
} }
private async _playTrack(queue: Queue, firstPlay: boolean) { private async _playTrack(queue: Queue, firstPlay: boolean): Promise<void> {
if (queue.stopped) return; if (queue.stopped) return;
if (queue.tracks.length === 1 && !queue.loopMode && !queue.repeatMode && !firstPlay) { if (queue.tracks.length === 1 && !queue.loopMode && !queue.repeatMode && !firstPlay) {
@ -275,10 +299,10 @@ export default class Player extends EventEmitter {
this.queues.delete(queue.guildID); this.queues.delete(queue.guildID);
if (queue.stopped) { if (queue.stopped) {
return this.emit(PlayerEvents.MUSIC_STOP, queue.firstMessage); return void this.emit(PlayerEvents.MUSIC_STOP, queue.firstMessage);
} }
return this.emit(PlayerEvents.QUEUE_END, queue.firstMessage, queue); return void this.emit(PlayerEvents.QUEUE_END, queue.firstMessage, queue);
} }
if (!queue.repeatMode && !firstPlay) { if (!queue.repeatMode && !firstPlay) {
@ -316,7 +340,7 @@ export default class Player extends EventEmitter {
} }
}); });
let encoderArgs: string[]; let encoderArgs: string[] = [];
if (encoderArgsFilters.length < 1) { if (encoderArgsFilters.length < 1) {
encoderArgs = []; encoderArgs = [];
} else { } else {
@ -386,3 +410,5 @@ export default class Player extends EventEmitter {
}); });
} }
} }
export default Player;

View file

@ -1,11 +1,11 @@
import { Message, Snowflake, VoiceConnection } from 'discord.js'; import { Message, Snowflake, VoiceConnection } from 'discord.js';
import AudioFilters from '../utils/AudioFilters'; import AudioFilters from '../utils/AudioFilters';
import Player from '../Player'; import { Player } from '../Player';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import Track from './Track'; import { Track } from './Track';
import { QueueFilters } from '../types/types'; import { QueueFilters } from '../types/types';
export default class Queue extends EventEmitter { export class Queue extends EventEmitter {
public player!: Player; public player!: Player;
public guildID: Snowflake; public guildID: Snowflake;
public voiceConnection?: VoiceConnection; public voiceConnection?: VoiceConnection;
@ -127,3 +127,5 @@ export default class Queue extends EventEmitter {
return this.voiceConnection?.dispatcher?.streamTime + this.additionalStreamTime || 0; return this.voiceConnection?.dispatcher?.streamTime + this.additionalStreamTime || 0;
} }
} }
export default Queue;

View file

@ -1,8 +1,8 @@
import Player from '../Player'; import { Player } from '../Player';
import { User } from 'discord.js'; import { User } from 'discord.js';
import { TrackData } from '../types/types'; import { TrackData } from '../types/types';
export default class Track { export class Track {
public player!: Player; public player!: Player;
public title!: string; public title!: string;
public description!: string; public description!: string;
@ -67,3 +67,5 @@ export default class Track {
return `${this.title} by ${this.author}`; return `${this.title} by ${this.author}`;
} }
} }
export default Track;

View file

@ -1,9 +1,9 @@
export * as AudioFilters from './utils/AudioFilters'; export { AudioFilters } from './utils/AudioFilters';
export * as Constants from './utils/Constants'; export * as Constants from './utils/Constants';
export * as Player from './Player'; export { Player } from './Player';
export * as Util from './utils/Util'; export { Util } from './utils/Util';
export * as Track from './Structures/Track'; export { Track } from './Structures/Track';
export * as Queue from './Structures/Queue'; export { Queue } from './Structures/Queue';
export * from './types/types'; export * from './types/types';
export * as PlayerError from './utils/PlayerError'; export { PlayerError } from './utils/PlayerError';
export { version } from '../package.json'; export { version } from '../package.json';

View file

@ -13,35 +13,7 @@ export interface PlayerOptions {
useSafeSearch?: boolean; useSafeSearch?: boolean;
} }
export type FiltersName = export type FiltersName = keyof QueueFilters;
| 'bassboost'
| '8D'
| 'vaporwave'
| 'nightcore'
| 'phaser'
| 'tremolo'
| 'vibrato'
| 'reverse'
| 'treble'
| 'normalizer'
| 'surrounding'
| 'pulsator'
| 'subboost'
| 'karaoke'
| 'flanger'
| 'gate'
| 'haas'
| 'mcompand'
| 'mono'
| 'mstlr'
| 'mstrr'
| 'compressor'
| 'expander'
| 'softlimiter'
| 'chorus'
| 'chorus2d'
| 'chorus3d'
| 'fadein';
export type TrackSource = 'soundcloud' | 'youtube' | 'arbitrary'; export type TrackSource = 'soundcloud' | 'youtube' | 'arbitrary';
@ -60,34 +32,34 @@ export interface TrackData {
} }
export type QueueFilters = { export type QueueFilters = {
bassboost: boolean; bassboost?: boolean;
'8D': boolean; '8D'?: boolean;
vaporwave: boolean; vaporwave?: boolean;
nightcore: boolean; nightcore?: boolean;
phaser: boolean; phaser?: boolean;
tremolo: boolean; tremolo?: boolean;
vibrato: boolean; vibrato?: boolean;
reverse: boolean; reverse?: boolean;
treble: boolean; treble?: boolean;
normalizer: boolean; normalizer?: boolean;
surrounding: boolean; surrounding?: boolean;
pulsator: boolean; pulsator?: boolean;
subboost: boolean; subboost?: boolean;
karaoke: boolean; karaoke?: boolean;
flanger: boolean; flanger?: boolean;
gate: boolean; gate?: boolean;
haas: boolean; haas?: boolean;
mcompand: boolean; mcompand?: boolean;
mono: boolean; mono?: boolean;
mstlr: boolean; mstlr?: boolean;
mstrr: boolean; mstrr?: boolean;
compressor: boolean; compressor?: boolean;
expander: boolean; expander?: boolean;
softlimiter: boolean; softlimiter?: boolean;
chorus: boolean; chorus?: boolean;
chorus2d: boolean; chorus2d?: boolean;
chorus3d: boolean; chorus3d?: boolean;
fadein: boolean; fadein?: boolean;
}; };
export type QueryType = export type QueryType =

View file

@ -55,3 +55,4 @@ const FilterList = {
}; };
export default FilterList; export default FilterList;
export { FilterList as AudioFilters };

View file

@ -6,3 +6,5 @@ export default class PlayerError extends Error {
Error.captureStackTrace(this); Error.captureStackTrace(this);
} }
} }
export { PlayerError };

View file

@ -1,7 +1,7 @@
import { PlayerOptions, QueryType } from '../types/types'; import { PlayerOptions, QueryType } from '../types/types';
import { FFmpeg } from 'prism-media'; import { FFmpeg } from 'prism-media';
import YouTube from 'youtube-sr'; import YouTube from 'youtube-sr';
import Track from '../Structures/Track'; import { Track } from '../Structures/Track';
// @ts-ignore // @ts-ignore
import { validateURL as SoundcloudValidateURL } from 'soundcloud-scraper'; import { validateURL as SoundcloudValidateURL } from 'soundcloud-scraper';
@ -12,7 +12,7 @@ const vimeoRegex = /(http|https)?:\/\/(www\.|player\.)?vimeo\.com\/(?:channels\/
const facebookRegex = /(https?:\/\/)(www\.|m\.)?(facebook|fb).com\/.*\/videos\/.*/; const facebookRegex = /(https?:\/\/)(www\.|m\.)?(facebook|fb).com\/.*\/videos\/.*/;
const reverbnationRegex = /https:\/\/(www.)?reverbnation.com\/(.+)\/song\/(.+)/; const reverbnationRegex = /https:\/\/(www.)?reverbnation.com\/(.+)\/song\/(.+)/;
export default class Util { export class Util {
constructor() { constructor() {
throw new Error(`The ${this.constructor.name} class is static and cannot be instantiated!`); throw new Error(`The ${this.constructor.name} class is static and cannot be instantiated!`);
} }
@ -127,3 +127,5 @@ export default class Util {
}); });
} }
} }
export default Util;

View file

@ -300,17 +300,19 @@ discord-ytdl-core@^5.0.2:
dependencies: dependencies:
prism-media "^1.2.7" prism-media "^1.2.7"
discord.js@discordjs/discord.js: discord.js@^12.5.3:
version "12.5.0" version "12.5.3"
resolved "https://codeload.github.com/discordjs/discord.js/tar.gz/e848d25c86fcd4a11a7879f7dcd55d0bc93faa6d" resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-12.5.3.tgz#56820d473c24320871df9ea0bbc6b462f21cf85c"
integrity sha512-D3nkOa/pCkNyn6jLZnAiJApw2N9XrIsXUAdThf01i7yrEuqUmDGc7/CexVWwEcgbQR97XQ+mcnqJpmJ/92B4Aw==
dependencies: dependencies:
"@discordjs/collection" "^0.1.6" "@discordjs/collection" "^0.1.6"
"@discordjs/form-data" "^3.0.1" "@discordjs/form-data" "^3.0.1"
abort-controller "^3.0.0" abort-controller "^3.0.0"
node-fetch "^2.6.1" node-fetch "^2.6.1"
prism-media "^1.2.2" prism-media "^1.2.9"
setimmediate "^1.0.5"
tweetnacl "^1.0.3" tweetnacl "^1.0.3"
ws "^7.3.1" ws "^7.4.4"
dom-serializer@^1.0.1, dom-serializer@~1.2.0: dom-serializer@^1.0.1, dom-serializer@~1.2.0:
version "1.2.0" version "1.2.0"
@ -667,7 +669,7 @@ prettier@^2.2.1:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5" resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q== integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
prism-media@^1.2.2, prism-media@^1.2.7: prism-media@^1.2.7, prism-media@^1.2.9:
version "1.2.9" version "1.2.9"
resolved "https://registry.yarnpkg.com/prism-media/-/prism-media-1.2.9.tgz#8d4f97b36efdfc82483eb8d3db64020767866f36" resolved "https://registry.yarnpkg.com/prism-media/-/prism-media-1.2.9.tgz#8d4f97b36efdfc82483eb8d3db64020767866f36"
integrity sha512-UHCYuqHipbTR1ZsXr5eg4JUmHER8Ss4YEb9Azn+9zzJ7/jlTtD1h0lc4g6tNx3eMlB8Mp6bfll0LPMAV4R6r3Q== integrity sha512-UHCYuqHipbTR1ZsXr5eg4JUmHER8Ss4YEb9Azn+9zzJ7/jlTtD1h0lc4g6tNx3eMlB8Mp6bfll0LPMAV4R6r3Q==
@ -737,6 +739,11 @@ set-blocking@~2.0.0:
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
integrity sha1-BF+XgtARrppoA93TgrJDkrPYkPc= integrity sha1-BF+XgtARrppoA93TgrJDkrPYkPc=
setimmediate@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285"
integrity sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=
signal-exit@^3.0.0: signal-exit@^3.0.0:
version "3.0.3" version "3.0.3"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c"
@ -898,7 +905,7 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
ws@^7.3.1: ws@^7.4.4:
version "7.4.4" version "7.4.4"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.4.tgz#383bc9742cb202292c9077ceab6f6047b17f2d59" resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.4.tgz#383bc9742cb202292c9077ceab6f6047b17f2d59"
integrity sha512-Qm8k8ojNQIMx7S+Zp8u/uHOx7Qazv3Yv4q68MiWWWOJhiwG5W3x7iqmRtJo8xxrciZUY4vRxUTJCKuRnF28ZZw== integrity sha512-Qm8k8ojNQIMx7S+Zp8u/uHOx7Qazv3Yv4q68MiWWWOJhiwG5W3x7iqmRtJo8xxrciZUY4vRxUTJCKuRnF28ZZw==