This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (64 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// @ts-check
const { Collection } = require("@discordjs/collection")
/** @template {Array<any>} Params */
class CommandManager {
constructor() {
/**
* A cache of all Commands assigned to the manager keyed by the first alias in the Command
*
* @type {Collection<string, Command<Params>>}
*/
this.cache = new Collection()
/**
* An auto managed Map keyed by category names with an Array of command's first aliases
*
* @type {Map<string, Array<string>>}
*/
this.categories = new Map()
}
/**
* A method to assign Commands to the manager
*
* @param {Array<Command<Params>>} properties
*/
assign(properties) {
properties.forEach(i => {
if (this.cache.get(i.name)) this.cache.delete(i.name)
this.cache.set(i.name, i)
this.categories.forEach(c => {
if (c.includes(i.name)) c.splice(c.indexOf(i.name), 1)
})
const cat = this.categories.get(i.category)
if (!cat) this.categories.set(i.category, [i.name])
else if (!cat.includes(i.name)) cat.push(i.name)
})
}
/**
* A method to remove Commands from the manager
*
* @param {Array<string>} commands
*/
remove(commands) {
for (const command of commands) {
if (this.cache.get(command)) {
this.cache.delete(command)
this.categories.forEach(c => {
if (c.includes(command)) c.splice(c.indexOf(command), 1)
})
}
}
}
}
CommandManager.default = CommandManager
module.exports = CommandManager;
/**
* @template {Array<any>} Params
* @typedef {Object} Command
* @property {string} name
* @property {Array<import("discord-typings").ApplicationCommandOption>} [options]
* @property {string} description
* @property {string} category
* @property {Array<string>} [examples]
* @property {number} [order]
* @property {(...args: Params) => any} process
*/