Compare commits

...

3 Commits

Author SHA1 Message Date
Thibaut Broggi 1affded967
Make the parameter for !register command optional 2021-02-24 13:32:17 +01:00
Thibaut Broggi 44a8fe413d
Simplify Track constructor 2021-02-24 09:40:14 +01:00
Thibaut Broggi 8137afdc7a
Add documentation for the GuildAction class 2021-02-24 09:19:44 +01:00
6 changed files with 35 additions and 16 deletions

View File

@ -1,4 +1,21 @@
/**
* @class
* @desc A class that describe an action, used each time an event matches a pattern described in the configuration file
* @property {Object} message The complete message. See discord.js documentation for mor informations
* @property {string} target
* @property {string} action
* @property {string[]} params The parameters for the action
*/
class GuildAction {
/**
* @constructor
* @desc Create a new GuildAction, setting its attributes to the given values
* @param {Object} parameters
* @param {Object} parameters.message
* @param {string} parameters.target
* @param {string} parameters.action
* @param {string[]} parameters.params
*/
constructor({ target, action, message, params }) {
this.target = target;
this.action = action;

View File

@ -12,12 +12,6 @@ const { GuildAction } = require('./guild_action')
* @property {callback} [on_expire] A function to call if the ttl is reached before being triggered
*/
/**
* @typedef {Object} ActionParams
* @property {Object} message The complete message. See discord.js documentation for more informations
* @property {string[]} params The parameters for the action
*/
/**
* @class
*/

View File

@ -15,6 +15,7 @@ function shuffle_array(arr) {
class Playlist {
#current_index
#tracks
#name
/**
* @constructor
@ -22,9 +23,10 @@ class Playlist {
* @param {Track[]} [parameters.tracks=[]] The tracks that compose the playlist
* @param {number} [parameters.current_index=0] The id of the currently played track
*/
constructor({ tracks = [], current_index = 0 } = {}) {
constructor({ tracks = [], current_index = 0, name = null } = {}) {
this.#current_index = current_index
this.#tracks = tracks.map(t => t instanceof Track ? t : new Track(t))
this.#name = name
}
/**
@ -34,7 +36,8 @@ class Playlist {
clone() {
return new Playlist({
current_index: this.#current_index,
tracks: this.#tracks.map(t => t.clone())
tracks: this.#tracks.map(t => t.clone()),
name: this.#name
})
}
@ -151,6 +154,14 @@ class Playlist {
toString() {
return this.#tracks.join(', ')
}
get_name() {
return this.#name
}
set_name(name = null) {
this.#name = name
}
}
module.exports = { Playlist }

View File

@ -13,7 +13,7 @@ class PlaylistsManager {
return (async () => {
const playlists = data || await PlaylistsManager.readFile(filepath)
Object.entries(playlists).forEach(([title, tracks]) => {
playlists[title] = new Playlist({ tracks })
playlists[title] = new Playlist({ tracks, name: title })
})
this.#playlists = playlists
this.#filepath = filepath
@ -37,9 +37,9 @@ class PlaylistsManager {
}
async register({ message, params }) {
const playlist_title = params[0]
const player = this.#gmanager.get_manager(message, 'player')
let playlist_title = params[0] || player.get_playlist().get_name()
if (playlist_title) {
const player = this.#gmanager.get_manager(message, 'player')
this.#playlists[playlist_title] = player.get_playlist().clone()
await PlaylistsManager.writeFile(this.#filepath, this.#playlists)
message.channel.send(`:white_check_mark: Playlist \`${playlist_title}\` saved`)

View File

@ -16,7 +16,7 @@ class ResourceManager {
/**
* Do a research and send the search results back to the Discord channel it's been asked for
* @param {ActionParams}
* @param {GuildAction}
* @return {Track[]} An array of objects representing the search results.
*/
async search({ message, params }) {

View File

@ -23,10 +23,7 @@ class Track {
*/
constructor({url, title, full_title, length, source, last_played = 0, play_count = 0} = {}) {
if (typeof length === 'string') {
this.#length = length
.split(':')
.map(v => parseInt(v))
.reduce((acc, v) => acc * 60 + v)
this.#length = from_string_to_seconds(length)
} else {
this.#length = length
}