Restricted access to CommandManager's list

added getList() method instead of list property, added object freezing when bot is starting up
This commit is contained in:
mmacierzanka committed 2021-08-31 20:23:14 +02:00
1 parent 6bf657c3d2
commit e0c5ea3820
3 files changed
+25 -5

No files matched your search

+7 -3
View File
@@ -132,7 +132,7 @@ export class Bot extends EventEmitter {
const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.name); const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.name);
this.commands.add(helpMsg); this.commands.add(helpMsg);
} }
Object.freeze(this.commands.list); this.commands.freezeList();
process.stdout.write("Connecting to Discord... "); process.stdout.write("Connecting to Discord... ");
this.client.login(this.token); this.client.login(this.token);
this.client.on("ready", async () => { this.client.on("ready", async () => {
@@ -248,11 +248,15 @@ export class Bot extends EventEmitter {
process.stdout.write("Registering commands... "); process.stdout.write("Registering commands... ");
await axios.put( await axios.put(
`https://discord.com/api/v8/applications/${this.applicationId}/commands`, `https://discord.com/api/v8/applications/${this.applicationId}/commands`,
this.commands.list.filter((c) => !Array.isArray(c.guilds) && c.slash).map((c) => c.toCommandObject()), this.commands
.getList()
.filter((c) => !Array.isArray(c.guilds) && c.slash)
.map((c) => c.toCommandObject()),
{ headers: { Authorization: `Bot ${this.token}` } } { headers: { Authorization: `Bot ${this.token}` } }
); );
const guilds: any = {}; const guilds: any = {};
await this.commands.list await this.commands
.getList()
.filter((c) => Array.isArray(c.guilds) && c.guilds.length > 0) .filter((c) => Array.isArray(c.guilds) && c.guilds.length > 0)
.map((c) => { .map((c) => {
c.guilds?.map(async (g) => { c.guilds?.map(async (g) => {
+17 -1
View File
@@ -12,7 +12,7 @@ export class CommandManager {
* List of commands registered in the manager * List of commands registered in the manager
* @type {Array} {@link Command} * @type {Array} {@link Command}
*/ */
public readonly list: Command[]; private readonly list: Command[];
/** /**
* Prefix used as a way to trigger the bot using messages * Prefix used as a way to trigger the bot using messages
* @type {string} * @type {string}
@@ -56,6 +56,15 @@ export class CommandManager {
return command; return command;
} }
/**
* Get list (array) of all {@link Command}s registered in a manager
* @returns {Command[]} Array of {@link Command}s registered in a manager
*/
getList(): Command[] {
const list = [...this.list];
return list;
}
/** /**
* Adds the given {@link Command} to the instance manager with initial processing * Adds the given {@link Command} to the instance manager with initial processing
* @param {Command} command - {@link Command} instance object * @param {Command} command - {@link Command} instance object
@@ -203,6 +212,13 @@ export class CommandManager {
return null; return null;
} }
freezeList(): void {
if (!Object.isFrozen(this.list)) {
Object.freeze(this.list);
}
return;
}
private findPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null { private findPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null {
let returnValue: PhraseOccurrenceData | null = null; let returnValue: PhraseOccurrenceData | null = null;
this.list.map((c) => { this.list.map((c) => {
+1 -1
View File
@@ -61,7 +61,7 @@ export class HelpMessage extends Command {
helpMsg.setTitle(params.title); helpMsg.setTitle(params.title);
helpMsg.setDescription(params.bottomText); helpMsg.setDescription(params.bottomText);
cmdManager.list.map((c) => { cmdManager.getList().map((c) => {
if ( if (
c.visible && c.visible &&
((Array.isArray(c.guilds) && c.guilds.length > 0 && c.guilds.find((g) => i?.guild?.id === g)) || !Array.isArray(c.guilds) || c.guilds.length === 0) ((Array.isArray(c.guilds) && c.guilds.length > 0 && c.guilds.find((g) => i?.guild?.id === g)) || !Array.isArray(c.guilds) || c.guilds.length === 0)