renamed TextCommand to ChatCommand

This commit is contained in:
mmacierzanka committed 2021-09-07 17:16:59 +02:00
1 parent b7bdbf3977
commit 47ec47338c
7 files changed
+33 -33

No files matched your search

+4 -4
View File
@@ -1,9 +1,9 @@
import { Bot } from "./src/structures/Bot.js";
import { TextCommand } from "./src/structures/TextCommand.js";
import { ChatCommand } from "./src/structures/ChatCommand.js";
import { MessageCommand } from "./src/structures/MessageCommand.js";
import { UserCommand } from "./src/structures/UserCommand.js";
import { InitOptions } from "./src/types/Bot.js";
import { TextCommandInit, CommandMessageStructure } from "./src/types/TextCommand.js";
import { ChatCommandInit, CommandMessageStructure } from "./src/types/ChatCommand.js";
import { HelpMessageParams } from "./src/types/HelpMessage.js";
import { ParameterResolvable, ParameterType, ParameterSchema } from "./src/types/Parameter.js";
import { SystemMessageAppearance, SystemMessageData } from "./src/types/SystemMessage.js";
@@ -13,11 +13,11 @@ import { PermissionsError, ParameterTypeError, MissingParameterError, OperationS
export default Bot;
export {
Bot,
TextCommand,
ChatCommand,
MessageCommand,
UserCommand,
InitOptions,
TextCommandInit,
ChatCommandInit,
CommandMessageStructure,
HelpMessageParams,
ParameterResolvable,
+7 -7
View File
@@ -3,8 +3,8 @@ import { CommandInteraction, Message } from "discord.js";
import { BaseCommand } from "../structures/BaseCommand.js";
import { MissingParameterError } from "../errors.js";
import { BooleanParameter, InputParameter, NumberParameter, ObjectParameter, StringParameter } from "../structures/Parameter.js";
import { TextCommand } from "../structures/TextCommand.js";
import { CommandMessageStructure } from "../types/TextCommand.js";
import { ChatCommand } from "../structures/ChatCommand.js";
import { CommandMessageStructure } from "../types/ChatCommand.js";
import { applicationState } from "../state.js";
import { MessageCommand } from "../structures/MessageCommand.js";
import { CommandType, PhraseOccurrenceData } from "../types/BaseCommand.js";
@@ -15,7 +15,7 @@ import { CommandResolvable, CommandStructure } from "../types/commands.js";
* @class Command manager
*/
export class CommandManager {
private readonly _chatCommandsList: TextCommand[] = [];
private readonly _chatCommandsList: ChatCommand[] = [];
private readonly _messagesCommandsList: MessageCommand[] = [];
private readonly _userCommandsList: UserCommand[] = [];
@@ -44,7 +44,7 @@ export class CommandManager {
/**
* Retrieves the command by name, alias or keyword
* @param {string} phrase - command name, alias or keyword
* @returns {TextCommand | MessageCommand | UserCommand | null} Retrieved extension of {@link BaseCommand} object from the manager or *null*
* @returns {ChatCommand | MessageCommand | UserCommand | null} Retrieved extension of {@link BaseCommand} object from the manager or *null*
*/
public get<T extends CommandType>(phrase: string, type: T): CommandStructure<T> | null {
let command: CommandStructure<T> | null = null;
@@ -103,7 +103,7 @@ export class CommandManager {
if (applicationState.running) {
throw new Error("Cannot add a command while the application is running");
}
if (command instanceof TextCommand) {
if (command instanceof ChatCommand) {
const nameOccurrence: PhraseOccurrenceData | null = this.findPhraseOccurrence(command.type, command.name);
if (nameOccurrence) {
throw new Error(`The name "${command.name}" has already been registered as ${nameOccurrence.type} in the "${nameOccurrence.command.name}" command.`);
@@ -175,7 +175,7 @@ export class CommandManager {
paramsList.splice(0, 1);
}
const parameters: InputParameter[] = [];
command instanceof TextCommand &&
command instanceof ChatCommand &&
command.parameters.map((p, i) => {
if (!p.optional && !paramsList[i]) {
throw new MissingParameterError(p);
@@ -224,7 +224,7 @@ export class CommandManager {
if (i.options?.data) {
const paramsList = i.options.data;
const parameters: InputParameter[] = [];
cmd instanceof TextCommand &&
cmd instanceof ChatCommand &&
cmd.parameters.map((p) => {
const inputParam = paramsList.find((d) => d.name == p.name);
if (!p.optional && !inputParam) {
+3 -3
View File
@@ -1,12 +1,12 @@
import { Client, CommandInteraction, GuildMember, Intents, Message } from "discord.js";
import { EventEmitter } from "events";
import * as http from "http";
import { TextCommand } from "./TextCommand.js";
import { ChatCommand } from "./ChatCommand.js";
import { CommandManager } from "../managers/CommandManager.old.js";
import { OperationSuccess, PermissionsError } from "../errors.js";
import { HelpMessage } from "./Help.js";
import { SystemMessageManager } from "../managers/SystemMessage.js";
import { CommandMessageStructure } from "../types/TextCommand.js";
import { CommandMessageStructure } from "../types/ChatCommand.js";
import { InitOptions } from "../types/Bot.js";
import { HelpMessageParams } from "../types/HelpMessage.js";
import { applicationState } from "../state.js";
@@ -138,7 +138,7 @@ export class Bot extends EventEmitter {
console.log("✔");
}
if (this.messages.help.enabled === true) {
const helpMsg: TextCommand = new HelpMessage(this.commands, this.messages.help, this.name);
const helpMsg: ChatCommand = new HelpMessage(this.commands, this.messages.help, this.name);
this.commands.add(helpMsg);
}
applicationState.running = true;
@@ -1,13 +1,13 @@
import { Message, CommandInteraction } from "discord.js";
import { BaseCommand } from "./BaseCommand.js";
import { TextCommandInit } from "../types/TextCommand.js";
import { ChatCommandInit } from "../types/ChatCommand.js";
import { DefaultParameter, InputParameter, Parameter } from "./Parameter.js";
import { TextCommandObject, TextCommandOptionChoiceObject, TextCommandOptionObject } from "../types/api.js";
/**
* @class Class that represents a command instance
*/
export class TextCommand extends BaseCommand {
export class ChatCommand extends BaseCommand {
/**
* List of parameters that can passed to this command
* @type {Array} {@link Parameter}
@@ -49,13 +49,13 @@ export class TextCommand extends BaseCommand {
/**
* Command constructor
* @constructor
* @param {TextCommandInit} o - {@link CommandBuilder}
* @param {ChatCommandInit} o - {@link CommandBuilder}
*/
constructor(o: TextCommandInit) {
if (!TextCommand.nameRegExp.test(o.name)) {
constructor(o: ChatCommandInit) {
if (!ChatCommand.nameRegExp.test(o.name)) {
throw new Error("Incorrect command name. Text and slash commands must match this regular expression: ^[w-]{1,32}$");
}
if (o.description && !TextCommand.descriptionRegExp.test(o.description)) {
if (o.description && !ChatCommand.descriptionRegExp.test(o.description)) {
throw new Error("Command descriptions must be 1-100 characters long");
}
super("CHAT", {
@@ -73,7 +73,7 @@ export class TextCommand extends BaseCommand {
} else {
this.parameters = o.parameters.map((ps) => new Parameter(ps));
}
this.aliases = TextCommand.processPhrase(o.aliases);
this.aliases = ChatCommand.processPhrase(o.aliases);
this.description = o.description || "No description";
this.usage = o.usage || this.generateUsageFromArguments();
this.visible = o.visible !== undefined ? o.visible : true;
@@ -94,7 +94,7 @@ export class TextCommand extends BaseCommand {
}
/**
* Converts {@link TextCommand} instance to object that is recognized by the Discord API
* Converts {@link ChatCommand} instance to object that is recognized by the Discord API
* @returns {Object} object
*/
public toObject(): TextCommandObject {
@@ -189,17 +189,17 @@ export class TextCommand extends BaseCommand {
return usageTemplate;
}
public static isCommand(o: any): o is TextCommand {
public static isCommand(o: any): o is ChatCommand {
return (
"description" in o &&
typeof o.description === "string" &&
TextCommand.descriptionRegExp.test(o.description) &&
ChatCommand.descriptionRegExp.test(o.description) &&
"parameters" in o &&
Array.isArray(o.parameters) &&
"visible" in o &&
typeof o.visible === "boolean" &&
BaseCommand.isCommand(o) &&
TextCommand.nameRegExp.test(o.name)
ChatCommand.nameRegExp.test(o.name)
);
}
}
+4 -4
View File
@@ -1,9 +1,9 @@
import { MessageEmbed } from "discord.js";
import { TextCommand } from "./TextCommand.js";
import { ChatCommand } from "./ChatCommand.js";
import { CommandManager } from "../managers/CommandManager.old.js";
import { HelpMessageParams } from "../types/HelpMessage.js";
export class HelpMessage extends TextCommand {
export class HelpMessage extends ChatCommand {
constructor(cmdManager: CommandManager, params: HelpMessageParams, botName?: string) {
super({
name: "help",
@@ -29,7 +29,7 @@ export class HelpMessage extends TextCommand {
helpMsg.setFooter(botName || "");
if (helpMsg != null) {
if (p("command_name")) {
const cmd: TextCommand | null = cmdManager.get(p("command_name")?.toString() || "", "CHAT");
const cmd: ChatCommand | null = cmdManager.get(p("command_name")?.toString() || "", "CHAT");
if (cmd) {
if (Array.isArray(cmd.guilds) && cmd.guilds.length > 0 && !cmd.guilds.find((g) => i?.guild?.id === g)) {
throw new ReferenceError(`Command "${cmd.name}" is not available`);
@@ -66,7 +66,7 @@ export class HelpMessage extends TextCommand {
cmdManager.getList("CHAT").map((c) => {
if (
c instanceof TextCommand &&
c instanceof ChatCommand &&
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)
) {
@@ -9,7 +9,7 @@ import { BaseCommand } from "../structures/BaseCommand.js";
* @interface
* Options for building a {@link Command} object
*/
export interface TextCommandInit {
export interface ChatCommandInit {
/**
* Command name
* @type {string}
+3 -3
View File
@@ -1,8 +1,8 @@
import { BaseCommand } from "../structures/BaseCommand.js";
import { TextCommand } from "../structures/TextCommand.js";
import { ChatCommand } from "../structures/ChatCommand.js";
import { MessageCommand } from "../structures/MessageCommand.js";
import { UserCommand } from "../structures/UserCommand.js";
export type CommandStructure<T> = T extends "MESSAGE" ? MessageCommand : T extends "USER" ? UserCommand : T extends "CHAT" ? TextCommand : BaseCommand;
export type CommandStructure<T> = T extends "MESSAGE" ? MessageCommand : T extends "USER" ? UserCommand : T extends "CHAT" ? ChatCommand : BaseCommand;
export type CommandResolvable = BaseCommand | TextCommand | MessageCommand | UserCommand;
export type CommandResolvable = BaseCommand | ChatCommand | MessageCommand | UserCommand;