getList method with type filtering

This commit is contained in:
mmacierzanka committed 2021-09-05 16:18:45 +02:00
1 parent 332a1dec97
commit c4b7dc3c44
4 files changed
+29 -11

No files matched your search

+19 -8
View File
@@ -6,12 +6,13 @@ import { TextCommand } from "../structures/TextCommand.js";
import { CommandMessageStructure, PhraseOccurrenceData } from "../types/TextCommand.js"; import { CommandMessageStructure, PhraseOccurrenceData } from "../types/TextCommand.js";
import { applicationState } from "../state.js"; import { applicationState } from "../state.js";
import { MessageCommand } from "../structures/MessageCommand.js"; import { MessageCommand } from "../structures/MessageCommand.js";
import { CommandType } from "../types/BaseCommand.js";
/** /**
* @class Command manager * @class Command manager
*/ */
export class CommandManager { export class CommandManager {
private readonly _textCommandsList: TextCommand[] = []; private readonly _chatCommandsList: TextCommand[] = [];
private readonly _messagesCommandsList: BaseCommand[] = []; private readonly _messagesCommandsList: BaseCommand[] = [];
@@ -46,7 +47,7 @@ export class CommandManager {
*/ */
public get(phrase: string): TextCommand | null { public get(phrase: string): TextCommand | null {
let command: TextCommand | null = null; let command: TextCommand | null = null;
this._textCommandsList.map((c) => { this._chatCommandsList.map((c) => {
if (c.name == phrase) { if (c.name == phrase) {
command = c; command = c;
} }
@@ -61,11 +62,21 @@ export class CommandManager {
} }
/** /**
* List of commands registered in the manager *
* @type {Array} {@link Command} * @param {CommandType} filter - type of commands included in the list
* @returns {BaseCommand[]} List of commands registered in the manager
*/ */
get list() { public getList(filter?: CommandType): readonly BaseCommand[] {
return Object.freeze([...this._textCommandsList]); switch (filter) {
case "CHAT":
return Object.freeze([...this._chatCommandsList]);
case "MESSAGE":
return Object.freeze([...this._messagesCommandsList]);
case "USER":
return Object.freeze([...this._userCommandsList]);
default:
return Object.freeze([...this._chatCommandsList, ...this._messagesCommandsList, ...this._userCommandsList]);
}
} }
/** /**
@@ -99,7 +110,7 @@ export class CommandManager {
throw new Error('Parameter with defined choices must have a "string" type'); throw new Error('Parameter with defined choices must have a "string" type');
} }
}); });
this._textCommandsList.push(command); this._chatCommandsList.push(command);
} else { } else {
throw new TypeError("Invalid argument type"); throw new TypeError("Invalid argument type");
} }
@@ -219,7 +230,7 @@ export class CommandManager {
private findTextPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null { private findTextPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null {
let returnValue: PhraseOccurrenceData | null = null; let returnValue: PhraseOccurrenceData | null = null;
this._textCommandsList.map((c) => { this._chatCommandsList.map((c) => {
if (phrase == c.name) { if (phrase == c.name) {
returnValue = { returnValue = {
command: c, command: c,
+2
View File
@@ -106,6 +106,8 @@ export class BaseCommand {
} }
} }
public toObject() {}
private createAccessor(args: InputParameter[]) { private createAccessor(args: InputParameter[]) {
return function (query: string, returnType?: "value" | "object") { return function (query: string, returnType?: "value" | "object") {
return returnType === "object" ? args.find((p) => p.name === query) || null : args.find((p) => p.name === query)?.value || null; return returnType === "object" ? args.find((p) => p.name === query) || null : args.find((p) => p.name === query)?.value || null;
+6 -2
View File
@@ -258,11 +258,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.toObject()), this.commands
.getList()
.filter((c) => !Array.isArray(c.guilds) && c instanceof TextCommand && c.slash)
.map((c) => c.toObject()),
{ 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) => {
+2 -1
View File
@@ -64,8 +64,9 @@ export class HelpMessage extends TextCommand {
helpMsg.setTitle(params.title); helpMsg.setTitle(params.title);
helpMsg.setDescription(params.bottomText); helpMsg.setDescription(params.bottomText);
cmdManager.list.map((c) => { cmdManager.getList("CHAT").map((c) => {
if ( if (
c instanceof TextCommand &&
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)
) { ) {