Skip to content

Commit

Permalink
Fixed leaveOnEmpty issue, changelog in releases
Browse files Browse the repository at this point in the history
  • Loading branch information
Olebeh committed Nov 13, 2022
2 parents ed4a7ff + f2e774a commit 97be1ed
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 45 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "./dist/index.js",
"types": "./typings/index.d.ts",
"scripts": {
"all": "npm run types && npm run compile && npm update",
"all": "npm run types && npm run compile && npm run update",
"update": "npm version patch && npm publish",
"types": "tsc --allowJs true --declaration true --emitDeclarationOnly true --outDir ./typings --removeComments false",
"compile": "tsc",
Expand Down
24 changes: 14 additions & 10 deletions src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class Player extends TypedEmitter<PlayerEvents> {
})

this.on(`queueEnd`, async queue => {
if (!queue.options.alwaysOn) return

const max = queue.options.leaveOnIdleTimeout
let cooldown = this._idleCooldowns[queue.id]
let time = 0
Expand All @@ -82,14 +84,12 @@ export class Player extends TypedEmitter<PlayerEvents> {

if (!queue) return

try {
queue.destroy(true)
} catch {}
if (!queue.destroyed) queue.destroy(true)

this.emit(`botDisconnect`, queue)
}

if (!oldState.channelId) {
if (newState.channelId) {
const queue = this.getQueue(newState.guild.id)

if (!queue || !queue.connection) return
Expand All @@ -112,19 +112,17 @@ export class Player extends TypedEmitter<PlayerEvents> {

const queue = this.getQueue(oldState.guild.id)

if (!queue) return
if (!queue || !queue.options.alwaysOn) return

const max = queue.options.leaveOnEmptyTimeout
let cooldown = this._emptyCooldowns[oldState.guild.id]
let time = 0

if (!max) return

time = 0

cooldown = setInterval(() => {
members = oldState.channel?.members.filter(m => !m.user.bot).size

console.log(max)
if (members) {
clearInterval(cooldown)
time = 0
Expand Down Expand Up @@ -154,7 +152,7 @@ export class Player extends TypedEmitter<PlayerEvents> {

/**
* Creates a new queue in the specified guild or, if it already exists, gets existed one
* @param guild Guild or some information about it that could be use for resolvation
* @param guild Guild or it's id
* @param options Some options for creating a queue
* @returns Created or found queue
*/
Expand All @@ -166,7 +164,13 @@ export class Player extends TypedEmitter<PlayerEvents> {
if (existingQueue && existingQueue.exists()) return existingQueue
else if (existingQueue && !existingQueue.exists()) this.deleteQueue(_guild)

const queue = new Queue<true>(_guild, this, options)
const queue = new Queue<true>(_guild, this, options = {
leaveOnEmptyTimeout: 30000,
leaveOnIdleTimeout: 30000,
alwaysOn: false,
maxVolume: 200,
...options
})
this.queues.push(queue)
return queue
}
Expand Down
7 changes: 4 additions & 3 deletions src/declarations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export interface TimeData {

export interface CreateQueueOptions {
channel?: Discord.GuildTextBasedChannel
leaveOnEmptyTimeout?: number,
leaveOnIdleTimeout?: number,
maxVolume?: number,
leaveOnEmptyTimeout?: number
leaveOnIdleTimeout?: number
maxVolume?: number
alwaysOn?: boolean
}

export interface PlaylistOptions {
Expand Down
37 changes: 23 additions & 14 deletions src/modules/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import { Util } from '../utils/Utils'

type If<T, A, B = undefined> = T extends true ? A : T extends false ? B : A | B

/**
* Queue is an object that is used to manipulate with guild tracks and is unique for each guild
*/
export class Queue<State extends boolean = boolean> {
/**
* Voice channel bot's currently in (if there's one)
* Voice channel bot is currently in (if there's one)
*/
channel?: Discord.GuildTextBasedChannel
/**
* Queue options like leaveOnEmptyTimeout and leaveOnIdleTimeout
*/
options: Omit<CreateQueueOptions, `channel`> = {}
options: Required<Omit<CreateQueueOptions, `maxVolume` | `channel`>>
/**
* An array of all tracks in the queue
*/
Expand All @@ -32,15 +35,15 @@ export class Queue<State extends boolean = boolean> {
*/
repeatMode: QueueRepeatMode = QueueRepeatMode.Off
/**
* Current queue's player state (paused or not)
* Current queue player state (paused or not)
*/
paused = false
/**
* Current queue's volume
* Current queue volume
*/
volume = 100
/**
* Max queue's volume possible to set
* Max queue volume possible to set
*/
maxVolume: number
/**
Expand Down Expand Up @@ -74,14 +77,20 @@ export class Queue<State extends boolean = boolean> {
private _queued: Track[] = []
private _current?: Track

constructor(guild: Discord.Guild, player: Player, options?: CreateQueueOptions) {
/**
* @warning Don't use constructor to create new Queue instance, use Player#createQueue() instead which is much more saver and will prevent duplicated Queues
*/
constructor(guild: Discord.Guild, player: Player, options: Required<Omit<CreateQueueOptions, 'channel'>> & { channel?: Discord.GuildTextBasedChannel }) {
this.guild = guild
this.id = guild.id
this.channel = options?.channel
this.channel = options.channel
this.player = player
this.options.leaveOnEmptyTimeout = options?.leaveOnEmptyTimeout ?? 30000
this.options.leaveOnIdleTimeout = options?.leaveOnIdleTimeout ?? 30000
this.maxVolume = options?.maxVolume ?? 200
this.options = {
leaveOnEmptyTimeout: options.leaveOnEmptyTimeout,
leaveOnIdleTimeout: options.leaveOnIdleTimeout,
alwaysOn: options.alwaysOn
}
this.maxVolume = options.maxVolume
this.#destroyed = false
}

Expand Down Expand Up @@ -164,7 +173,7 @@ export class Queue<State extends boolean = boolean> {

if (this.connection.listeners(`start`).length < 1) this.connection.on(`start`, async resource => {
this._current = resource.metadata
this.player.emit(`trackStart`, this, resource.metadata)
if (!this._streamTime) this.player.emit(`trackStart`, this, resource.metadata)
})

if (this.connection.listeners(`finish`).length < 1) this.connection.on(`finish`, async resourse => {
Expand Down Expand Up @@ -450,7 +459,7 @@ export class Queue<State extends boolean = boolean> {
/**
* Starts the playback of the track
* @param trackToPlay If specified, this track will start playing. Else the first track from the queue will start playing
* @param options Force - if true, the playback of the track will forcefully start, even if there's a track currently playing. Else won't play. Seeks track to specified time (in ms)
* @param options Force - if true, the playback of the track will forcefully start, even if there's a track currently playing. Else won't play; seek - seeks track to specified time (in ms)
* @returns
*/
async play(trackToPlay?: Track, options?: { force?: boolean, seek?: number }) {
Expand All @@ -461,10 +470,10 @@ export class Queue<State extends boolean = boolean> {
if (!this.connection.channel || !this.connection.channel.id) return
if (this.connection.audioPlayer) this.connection.audioPlayer.stop()

this._streamTime = 0
const track = !trackToPlay ? this.tracks.shift() : trackToPlay

if (!track) return

this._queued.push(track)

const { title, author, source, url } = track
Expand Down Expand Up @@ -518,7 +527,7 @@ export class Queue<State extends boolean = boolean> {
}

/**
* Destroyed current queue without leaving the voice channel. Create just for convenience
* Equals to queue.destroy(false)
* @returns
*/
stop() {
Expand Down
5 changes: 4 additions & 1 deletion src/modules/Track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export class Track {
* The Date when live started, if the track is live stream
*/
liveAt?: Date
/**
* If this track was requested from a playlist, it will appear here
*/
playlist: PlaylistOptions | null
id: string
readonly player: Player
Expand Down Expand Up @@ -50,6 +53,6 @@ export class Track {
toString(includeAuthor?: boolean) {
const { title, url, author, requestedBy, duration } = this

return `[${title}](${url}) ${includeAuthor ? `| ${author} ` : ``}| ${requestedBy ?? this.player.client.user} | \`${duration}\``
return `[${title}](${url}, "${author} - ${title}, ${duration}") ${includeAuthor ? `| ${author} ` : ``}| ${requestedBy ?? this.player.client.user} | \`${duration}\``
}
}
2 changes: 1 addition & 1 deletion typings/Player.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export declare class Player extends TypedEmitter<PlayerEvents> {
_playerHandler(): void;
/**
* Creates a new queue in the specified guild or, if it already exists, gets existed one
* @param guild Guild or some information about it that could be use for resolvation
* @param guild Guild or it's id
* @param options Some options for creating a queue
* @returns Created or found queue
*/
Expand Down
24 changes: 16 additions & 8 deletions typings/modules/Queue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import { CreateQueueOptions, PlayerProgressBarOptions, PlayerTimestamp, QueueRep
import { Track } from './Track';
import { StreamDispatcher } from '../utils/StreamDispatcher';
declare type If<T, A, B = undefined> = T extends true ? A : T extends false ? B : A | B;
/**
* Queue is an object that is used to manipulate with guild tracks and is unique for each guild
*/
export declare class Queue<State extends boolean = boolean> {
#private;
/**
* Voice channel bot's currently in (if there's one)
* Voice channel bot is currently in (if there's one)
*/
channel?: Discord.GuildTextBasedChannel;
/**
* Queue options like leaveOnEmptyTimeout and leaveOnIdleTimeout
*/
options: Omit<CreateQueueOptions, `channel`>;
options: Required<Omit<CreateQueueOptions, `maxVolume` | `channel`>>;
/**
* An array of all tracks in the queue
*/
Expand All @@ -27,15 +30,15 @@ export declare class Queue<State extends boolean = boolean> {
*/
repeatMode: QueueRepeatMode;
/**
* Current queue's player state (paused or not)
* Current queue player state (paused or not)
*/
paused: boolean;
/**
* Current queue's volume
* Current queue volume
*/
volume: number;
/**
* Max queue's volume possible to set
* Max queue volume possible to set
*/
maxVolume: number;
/**
Expand Down Expand Up @@ -64,7 +67,12 @@ export declare class Queue<State extends boolean = boolean> {
*/
private _queued;
private _current?;
constructor(guild: Discord.Guild, player: Player, options?: CreateQueueOptions);
/**
* @warning Don't use constructor to create new Queue instance, use Player#createQueue() instead which is much more saver and will prevent duplicated Queues
*/
constructor(guild: Discord.Guild, player: Player, options: Required<Omit<CreateQueueOptions, 'channel'>> & {
channel?: Discord.GuildTextBasedChannel;
});
/**
* Adds specified track or tracks to the end of the queue
* @param tracks Track or tracks that should be added
Expand Down Expand Up @@ -189,7 +197,7 @@ export declare class Queue<State extends boolean = boolean> {
/**
* Starts the playback of the track
* @param trackToPlay If specified, this track will start playing. Else the first track from the queue will start playing
* @param options Force - if true, the playback of the track will forcefully start, even if there's a track currently playing. Else won't play. Seeks track to specified time (in ms)
* @param options Force - if true, the playback of the track will forcefully start, even if there's a track currently playing. Else won't play; seek - seeks track to specified time (in ms)
* @returns
*/
play(trackToPlay?: Track, options?: {
Expand All @@ -207,7 +215,7 @@ export declare class Queue<State extends boolean = boolean> {
*/
skip(): If<State, boolean, undefined>;
/**
* Destroyed current queue without leaving the voice channel. Create just for convenience
* Equals to queue.destroy(false)
* @returns
*/
stop(): void;
Expand Down
3 changes: 3 additions & 0 deletions typings/modules/Track.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export declare class Track {
* The Date when live started, if the track is live stream
*/
liveAt?: Date;
/**
* If this track was requested from a playlist, it will appear here
*/
playlist: PlaylistOptions | null;
id: string;
readonly player: Player;
Expand Down

0 comments on commit 97be1ed

Please sign in to comment.