Archived
getList method with type filtering
This commit is contained in:
1 parent
332a1dec97
commit
c4b7dc3c44
4 files changed
+29
-11
No files matched your search
@@ -6,12 +6,13 @@ import { TextCommand } from "../structures/TextCommand.js";
|
||||
import { CommandMessageStructure, PhraseOccurrenceData } from "../types/TextCommand.js";
|
||||
import { applicationState } from "../state.js";
|
||||
import { MessageCommand } from "../structures/MessageCommand.js";
|
||||
import { CommandType } from "../types/BaseCommand.js";
|
||||
|
||||
/**
|
||||
* @class Command manager
|
||||
*/
|
||||
export class CommandManager {
|
||||
private readonly _textCommandsList: TextCommand[] = [];
|
||||
private readonly _chatCommandsList: TextCommand[] = [];
|
||||
|
||||
private readonly _messagesCommandsList: BaseCommand[] = [];
|
||||
|
||||
@@ -46,7 +47,7 @@ export class CommandManager {
|
||||
*/
|
||||
public get(phrase: string): TextCommand | null {
|
||||
let command: TextCommand | null = null;
|
||||
this._textCommandsList.map((c) => {
|
||||
this._chatCommandsList.map((c) => {
|
||||
if (c.name == phrase) {
|
||||
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() {
|
||||
return Object.freeze([...this._textCommandsList]);
|
||||
public getList(filter?: CommandType): readonly BaseCommand[] {
|
||||
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');
|
||||
}
|
||||
});
|
||||
this._textCommandsList.push(command);
|
||||
this._chatCommandsList.push(command);
|
||||
} else {
|
||||
throw new TypeError("Invalid argument type");
|
||||
}
|
||||
@@ -219,7 +230,7 @@ export class CommandManager {
|
||||
|
||||
private findTextPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null {
|
||||
let returnValue: PhraseOccurrenceData | null = null;
|
||||
this._textCommandsList.map((c) => {
|
||||
this._chatCommandsList.map((c) => {
|
||||
if (phrase == c.name) {
|
||||
returnValue = {
|
||||
command: c,
|
||||
|
||||
@@ -106,6 +106,8 @@ export class BaseCommand {
|
||||
}
|
||||
}
|
||||
|
||||
public toObject() {}
|
||||
|
||||
private createAccessor(args: InputParameter[]) {
|
||||
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;
|
||||
|
||||
@@ -258,11 +258,15 @@ export class Bot extends EventEmitter {
|
||||
process.stdout.write("Registering commands... ");
|
||||
await axios.put(
|
||||
`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}` } }
|
||||
);
|
||||
const guilds: any = {};
|
||||
await this.commands.list
|
||||
await this.commands
|
||||
.getList()
|
||||
.filter((c) => Array.isArray(c.guilds) && c.guilds.length > 0)
|
||||
.map((c) => {
|
||||
c.guilds?.map(async (g) => {
|
||||
|
||||
@@ -64,8 +64,9 @@ export class HelpMessage extends TextCommand {
|
||||
helpMsg.setTitle(params.title);
|
||||
helpMsg.setDescription(params.bottomText);
|
||||
|
||||
cmdManager.list.map((c) => {
|
||||
cmdManager.getList("CHAT").map((c) => {
|
||||
if (
|
||||
c instanceof TextCommand &&
|
||||
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)
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user