Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
achiragaming committed Nov 19, 2024
1 parent 1431c1c commit 3152841
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "damonjs",
"version": "1.8.2",
"version": "1.8.3",
"preview": false,
"description": "A modified Shoukaku wrapper with enhanced queue support.",
"main": "dist/Index.js",
Expand Down
7 changes: 6 additions & 1 deletion src/DamonJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export class DamonJs extends EventEmitter {
max: number;
time: number;
};
public trackEndSpam: {
max: number;
time: number;
};
/** Stuck config until skip stops */
public stuck: {
max: number;
Expand All @@ -67,9 +71,10 @@ export class DamonJs extends EventEmitter {
super();

this.shoukaku = shoukaku;

this.exceptions = this.DamonJsOptions.exceptions ? this.DamonJsOptions.exceptions : { max: 3, time: 30 * 1000 };
this.stuck = this.DamonJsOptions.stuck ? this.DamonJsOptions.stuck : { max: 3, time: 30 * 1000 };
this.trackEndSpam = this.DamonJsOptions.trackEndSpam ?this.DamonJsOptions.trackEndSpam : { max: 3, time: 30 * 1000 };
this.resolveError = this.DamonJsOptions.resolveError
? this.DamonJsOptions.resolveError
: { max: 3, time: 30 * 1000 };
Expand Down
2 changes: 1 addition & 1 deletion src/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export * from './DamonJs';
export { DamonJsTrack, DamonJsQueue, DamonJsPlayer, Plugins };
export * from './Modules/Interfaces';

export const version = '1.8.2';
export const version = '1.8.3';
22 changes: 18 additions & 4 deletions src/Managers/DamonJsPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class DamonJsPlayer {
public readonly data: Map<string, any>;
public search: (query: string, options: DamonJsSearchOptions) => Promise<DamonJsSearchResult>;
public readonly errors: {
trackendSpamData: { trackEnds: number[]; lastTrackEndTime: number };
exceptionData: { exceptions: number[]; lastExceptionTime: number };
stuckData: { stucks: number[]; lastStuckTime: number };
resolveErrorData: { resolveErrors: number[]; lastResolveErrorTime: number };
Expand All @@ -73,6 +74,7 @@ export class DamonJsPlayer {
this.data = new Map(options.data);
this.textId = this.options.textId;
this.errors = {
trackendSpamData: { trackEnds: [], lastTrackEndTime: 0 },
exceptionData: { exceptions: [], lastExceptionTime: 0 },
stuckData: { lastStuckTime: 0, stucks: [] },
resolveErrorData: { resolveErrors: [], lastResolveErrorTime: 0 },
Expand Down Expand Up @@ -142,13 +144,25 @@ export class DamonJsPlayer {
}
private async handleTrackEnd(data: TrackEndEvent) {
return await this.withLock('trackEnd', async () => {
if (!this.queue.current) return this.emit(Events.Debug, this, `No track to start ${this.guildId}`);
if (this.state === PlayerState.DESTROYING || this.state === PlayerState.DESTROYED) {
return this.emit(Events.Debug, this, `Player ${this.guildId} destroyed from end event`);
}
if (!this.queue.current) return this.emit(Events.Debug, this, `No track to start ${this.guildId}`);
this.isTrackPlaying = false;
this.emit(Events.PlayerEnd, this, this.queue.current);
await this.skip().catch(() => null);
const now = Date.now();

const maxTime = this.damonjs.trackEndSpam.time;
const trackEnds = this.errors.trackendSpamData.trackEnds.filter((time: number) => now - time < maxTime);

if (trackEnds.length < this.damonjs.trackEndSpam.max) {
this.errors.trackendSpamData.lastTrackEndTime = now;
this.isTrackPlaying = false;

this.emit(Events.PlayerEnd, this, this.queue.current, data);
trackEnds.push(now);
this.errors.trackendSpamData.trackEnds = trackEnds;
} else {
this.emit(Events.Debug, this, `Player ${this.guildId} hit the max trackends limit`);
}
});
}
private async handlePlayerEmpty() {
Expand Down
4 changes: 2 additions & 2 deletions src/Managers/Supports/DamonJsTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export class DamonJsTrack {
/** International Standard Recording Code */
public isrc: string | undefined;
public resolvedBySource: boolean = false;

public readonly data: Map<string, any>;
constructor(raw: RawTrack, requester: unknown) {
this.damonjs = undefined;

this.data = new Map();
this.encoded = raw.encoded;
this.sourceName = raw.info.sourceName;
this.title = raw.info.title;
Expand Down
9 changes: 7 additions & 2 deletions src/Modules/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { DamonJs } from '../DamonJs';
import { DamonJsPlayer } from '../Index';
import { DamonJsTrack } from '../Managers/Supports/DamonJsTrack';
import { PlayerUpdate, TrackExceptionEvent, TrackStuckEvent, Utils, WebSocketClosedEvent } from 'shoukaku';
import { PlayerUpdate, TrackEndEvent, TrackExceptionEvent, TrackStuckEvent, Utils, WebSocketClosedEvent } from 'shoukaku';
import { Snowflake } from 'discord.js';
export interface DamonJsOptions {
/** Default search engine if no engine was provided. Default to youtube */
defaultSearchEngine: SearchEngines;
/** TrackEnd spam config until skip stops */
trackEndSpam?: {
max: number;
time: number;
};
/** Exception config until skip stops */
exceptions?: {
max: number;
Expand Down Expand Up @@ -111,7 +116,7 @@ export interface DamonJsEvents {
playerDestroy: [player: DamonJsPlayer];
playerCreate: [player: DamonJsPlayer];
playerStart: [player: DamonJsPlayer, track: DamonJsTrack];
playerEnd: [player: DamonJsPlayer, track: DamonJsTrack];
playerEnd: [player: DamonJsPlayer, track: DamonJsTrack,data:TrackEndEvent];
playerEmpty: [player: DamonJsPlayer, track?: DamonJsTrack];
playerClosed: [player: DamonJsPlayer, data: WebSocketClosedEvent];
playerUpdate: [player: DamonJsPlayer, track: DamonJsTrack, data: PlayerUpdate];
Expand Down

0 comments on commit 3152841

Please sign in to comment.