Archived
removed old managers
This commit is contained in:
1 parent
4d2507e3e6
commit
38eeb6f788
3 files changed
-219
No files matched your search
@@ -1,146 +0,0 @@
|
||||
import { CommandInteraction, Message } from "discord.js";
|
||||
import { CommandNotFound, MissingParameterError } from "../errors.js";
|
||||
import { BooleanParameter, InputParameter, NullParameter, NumberParameter, ObjectParameter, Parameter, StringParameter } from "../structures/Parameter.js";
|
||||
import { CommandInteractionData } from "../commands/types/commands.js";
|
||||
import { ChatCommand } from "../commands/ChatCommand.js";
|
||||
import { CommandManager } from "./CommandManager.js";
|
||||
import { ParameterResolvable } from "../structures/types/Parameter.js";
|
||||
|
||||
export class ChatCommandManager extends CommandManager {
|
||||
protected readonly _list: ChatCommand[] = [];
|
||||
public readonly prefix?: string;
|
||||
public readonly parameterSeparator: string;
|
||||
|
||||
constructor(prefix?: string, parameterSeparator?: string) {
|
||||
super();
|
||||
this.prefix = prefix;
|
||||
this.parameterSeparator = parameterSeparator || ",";
|
||||
}
|
||||
|
||||
public get(q: string): ChatCommand | null {
|
||||
const cmd = (super.get(q) as ChatCommand) || null;
|
||||
if (!cmd) {
|
||||
this._list.map((c) => {
|
||||
if (c.aliases) {
|
||||
if (c.aliases.find((a) => a === q)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
});
|
||||
return null;
|
||||
} else {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
|
||||
public add(command: ChatCommand): void {
|
||||
try {
|
||||
if (ChatCommand.isCommand(command)) {
|
||||
if (command.aliases && command.aliases.length > 0) {
|
||||
command.aliases.map((a, i, ar) => {
|
||||
const existingPhraseCmd: ChatCommand | null =
|
||||
this._list.find((c) => {
|
||||
if (c.aliases && c.aliases.length > 0) {
|
||||
if (c.aliases.find((w) => w === a)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}) || null;
|
||||
if (existingPhraseCmd) {
|
||||
console.warn(
|
||||
`[⚠️ WARNING] The alias "${a}" is already registered in the "${existingPhraseCmd.name}" command. It won't be included in the "${command.name}" command.`
|
||||
);
|
||||
ar.splice(i, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
super.add(command);
|
||||
} else {
|
||||
throw new Error("Incorrect argument type");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[❌ ERROR] ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
public fetch(i: Message | CommandInteraction): CommandInteractionData | null {
|
||||
if (i instanceof Message) {
|
||||
if (!this.prefix || i.author.bot || !i.content.startsWith(this.prefix)) return null;
|
||||
else {
|
||||
const messageContentRaw = i.content.replace(this.prefix, "").split(" ");
|
||||
const command = this.get(messageContentRaw[0]);
|
||||
if (command) {
|
||||
messageContentRaw.splice(0, 1);
|
||||
const argsRaw = messageContentRaw
|
||||
.join(" ")
|
||||
.split(this.parameterSeparator)
|
||||
.map((arg) => arg.replace(" ", ""));
|
||||
if ((argsRaw[0] == "" || argsRaw[0] == " ") && argsRaw.length == 1) {
|
||||
argsRaw.splice(0, 1);
|
||||
}
|
||||
if (command.parameters.length > 0) {
|
||||
const args: InputParameter[] = this.processRawInput(argsRaw, command.parameters);
|
||||
return {
|
||||
command: command,
|
||||
parameters: args,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
command: command,
|
||||
parameters: [],
|
||||
};
|
||||
}
|
||||
} else {
|
||||
throw new CommandNotFound(messageContentRaw[0]);
|
||||
}
|
||||
}
|
||||
} else if (i instanceof CommandInteraction) {
|
||||
const command = this.get(i.commandName);
|
||||
if (command) {
|
||||
if (command.parameters.length > 0) {
|
||||
const argsRaw = i.options.data.map((a) => a.value || null);
|
||||
const args = this.processRawInput(argsRaw, command.parameters);
|
||||
return {
|
||||
command: command,
|
||||
parameters: args,
|
||||
};
|
||||
}
|
||||
return {
|
||||
command: command,
|
||||
parameters: [],
|
||||
};
|
||||
} else {
|
||||
throw new CommandNotFound(i.commandName);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private processRawInput(args: ParameterResolvable[], schema: Parameter[]): InputParameter[] {
|
||||
return schema.map((p, i) => {
|
||||
if (!p.optional && !args[i]) {
|
||||
throw new MissingParameterError(p);
|
||||
} else if (p.optional && !args[i]) {
|
||||
return new NullParameter(p);
|
||||
}
|
||||
switch (p.type) {
|
||||
case "mentionable":
|
||||
case "channel":
|
||||
case "role":
|
||||
case "user":
|
||||
return new ObjectParameter(p, args[i]);
|
||||
case "string":
|
||||
return new StringParameter(p, args[i]);
|
||||
case "boolean":
|
||||
return new BooleanParameter(p, args[i]);
|
||||
case "number":
|
||||
return new NumberParameter(p, args[i]);
|
||||
default:
|
||||
return new InputParameter(p, args[i]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { applicationState } from "../state.js";
|
||||
import { BaseCommand } from "../commands/BaseCommand.js";
|
||||
|
||||
export class CommandManager {
|
||||
protected readonly _list: BaseCommand[] = [];
|
||||
|
||||
public get(q: string): BaseCommand | null {
|
||||
return this._list.find((c) => c.name === q) || null;
|
||||
}
|
||||
|
||||
get list(): readonly BaseCommand[] {
|
||||
return Object.freeze([...this._list]);
|
||||
}
|
||||
|
||||
public add(command: BaseCommand): void {
|
||||
try {
|
||||
if (applicationState.running) {
|
||||
throw new Error("Cannot add a command while the application is running");
|
||||
}
|
||||
if (BaseCommand.isCommand(command)) {
|
||||
if (this.get(command.name)) {
|
||||
throw new Error(`A command with name "${command.name}" is already registered in this manager`);
|
||||
} else {
|
||||
this._list.push(command);
|
||||
}
|
||||
} else {
|
||||
throw new TypeError("Incorrect argument type");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[❌ ERROR] ${e}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { Interaction } from "discord.js";
|
||||
import { CommandInteractionData } from "../commands/types/commands.js";
|
||||
import { MessageCommand } from "../commands/MessageCommand.js";
|
||||
import { CommandManager } from "./CommandManager.js";
|
||||
import { CommandNotFound } from "../errors.js";
|
||||
import { Parameter, TargetID, TargetParameter } from "../structures/Parameter.js";
|
||||
|
||||
export class MessageCommandManager extends CommandManager {
|
||||
protected readonly _list: MessageCommand[] = [];
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public add(command: MessageCommand) {
|
||||
if (MessageCommand.isCommand(command)) {
|
||||
super.add(command);
|
||||
} else {
|
||||
console.error("[❌ ERROR] Incorrect argument type");
|
||||
}
|
||||
}
|
||||
|
||||
public fetch(i: Interaction): CommandInteractionData | null {
|
||||
if (!i.isContextMenu() || i.targetType !== "MESSAGE") return null;
|
||||
const command = this.get(i.commandName);
|
||||
if (!command) {
|
||||
throw new CommandNotFound(i.commandName);
|
||||
}
|
||||
const parameter = new Parameter({
|
||||
name: "target",
|
||||
optional: false,
|
||||
type: "target",
|
||||
});
|
||||
const inputParameter = new TargetParameter(parameter, new TargetID(i.targetId));
|
||||
return {
|
||||
command: command,
|
||||
parameters: [inputParameter],
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user