Archived
Command -> TextCommand
This commit is contained in:
1 parent
996c3733a0
commit
108bfb38d3
8 files changed
+37
-37
No files matched your search
@@ -1,8 +1,8 @@
|
|||||||
import { MessageEmbed } from "discord.js";
|
import { MessageEmbed } from "discord.js";
|
||||||
import { Bot } from "./src/structures/Bot.js";
|
import { Bot } from "./src/structures/Bot.js";
|
||||||
import { Command } from "./src/structures/Command.js";
|
import { TextCommand } from "./src/structures/TextCommand.js";
|
||||||
import { InitOptions } from "./src/types/Bot.js";
|
import { InitOptions } from "./src/types/Bot.js";
|
||||||
import { CommandBuilder, CommandMessageStructure } from "./src/types/Command.js";
|
import { TextCommandInit, CommandMessageStructure } from "./src/types/TextCommand.js";
|
||||||
import { HelpMessageParams } from "./src/types/HelpMessage.js";
|
import { HelpMessageParams } from "./src/types/HelpMessage.js";
|
||||||
import { ParameterResolvable, ParameterType, ParameterSchema } from "./src/types/Parameter.js";
|
import { ParameterResolvable, ParameterType, ParameterSchema } from "./src/types/Parameter.js";
|
||||||
import { SystemMessageAppearance, SystemMessageData } from "./src/types/SystemMessage.js";
|
import { SystemMessageAppearance, SystemMessageData } from "./src/types/SystemMessage.js";
|
||||||
@@ -12,10 +12,10 @@ import { PermissionsError, ParameterTypeError, MissingParameterError, OperationS
|
|||||||
export default Bot;
|
export default Bot;
|
||||||
export {
|
export {
|
||||||
Bot,
|
Bot,
|
||||||
Command,
|
TextCommand,
|
||||||
MessageEmbed,
|
MessageEmbed,
|
||||||
InitOptions,
|
InitOptions,
|
||||||
CommandBuilder,
|
TextCommandInit,
|
||||||
CommandMessageStructure,
|
CommandMessageStructure,
|
||||||
HelpMessageParams,
|
HelpMessageParams,
|
||||||
ParameterResolvable,
|
ParameterResolvable,
|
||||||
|
|||||||
+5
-5
@@ -1,4 +1,4 @@
|
|||||||
import { Command } from "./structures/Command.js";
|
import { TextCommand } from "./structures/TextCommand.js";
|
||||||
import { GuildMember } from "discord.js";
|
import { GuildMember } from "discord.js";
|
||||||
import { Parameter } from "./structures/Parameter.js";
|
import { Parameter } from "./structures/Parameter.js";
|
||||||
import { ParameterType } from "./types/Parameter.js";
|
import { ParameterType } from "./types/Parameter.js";
|
||||||
@@ -9,9 +9,9 @@ import { ParameterType } from "./types/Parameter.js";
|
|||||||
* @exports
|
* @exports
|
||||||
*/
|
*/
|
||||||
export class PermissionsError extends Error {
|
export class PermissionsError extends Error {
|
||||||
private readonly command: Command;
|
private readonly command: TextCommand;
|
||||||
private readonly user: GuildMember | null;
|
private readonly user: GuildMember | null;
|
||||||
constructor(command: Command, user?: GuildMember | null) {
|
constructor(command: TextCommand, user?: GuildMember | null) {
|
||||||
super();
|
super();
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.user = user || null;
|
this.user = user || null;
|
||||||
@@ -60,8 +60,8 @@ export class MissingParameterError extends ReferenceError {
|
|||||||
* @exports
|
* @exports
|
||||||
*/
|
*/
|
||||||
export class OperationSuccess {
|
export class OperationSuccess {
|
||||||
public readonly command: Command;
|
public readonly command: TextCommand;
|
||||||
constructor(c: Command) {
|
constructor(c: TextCommand) {
|
||||||
this.command = c;
|
this.command = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
import { CommandInteraction, Message } from "discord.js";
|
import { CommandInteraction, Message } from "discord.js";
|
||||||
import { MissingParameterError } from "../errors.js";
|
import { MissingParameterError } from "../errors.js";
|
||||||
import { BooleanParameter, InputParameter, NumberParameter, ObjectParameter, StringParameter } from "../structures/Parameter.js";
|
import { BooleanParameter, InputParameter, NumberParameter, ObjectParameter, StringParameter } from "../structures/Parameter.js";
|
||||||
import { Command } from "../structures/Command.js";
|
import { TextCommand } from "../structures/TextCommand.js";
|
||||||
import { CommandMessageStructure, PhraseOccurrenceData } from "../types/Command.js";
|
import { CommandMessageStructure, PhraseOccurrenceData } from "../types/TextCommand.js";
|
||||||
import { applicationState } from "../state.js";
|
import { applicationState } from "../state.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Command manager
|
* @class Command manager
|
||||||
*/
|
*/
|
||||||
export class CommandManager {
|
export class CommandManager {
|
||||||
private readonly _list: Command[] = [];
|
private readonly _list: TextCommand[] = [];
|
||||||
/**
|
/**
|
||||||
* 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}
|
||||||
@@ -34,10 +34,10 @@ export class CommandManager {
|
|||||||
/**
|
/**
|
||||||
* Retrieves the command by name, alias or keyword
|
* Retrieves the command by name, alias or keyword
|
||||||
* @param {string} phrase - command name, alias or keyword
|
* @param {string} phrase - command name, alias or keyword
|
||||||
* @returns {Command | null} Retrieved {@link Command} object from the manager or *null*
|
* @returns {TextCommand | null} Retrieved {@link Command} object from the manager or *null*
|
||||||
*/
|
*/
|
||||||
public get(phrase: string): Command | null {
|
public get(phrase: string): TextCommand | null {
|
||||||
let command: Command | null = null;
|
let command: TextCommand | null = null;
|
||||||
this._list.map((c) => {
|
this._list.map((c) => {
|
||||||
if (c.name == phrase) {
|
if (c.name == phrase) {
|
||||||
command = c;
|
command = c;
|
||||||
@@ -62,15 +62,15 @@ export class CommandManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 {TextCommand} command - {@link Command} instance object
|
||||||
* @returns {boolean} Whether this command has been added successfully
|
* @returns {boolean} Whether this command has been added successfully
|
||||||
*/
|
*/
|
||||||
public add(command: Command): boolean {
|
public add(command: TextCommand): boolean {
|
||||||
try {
|
try {
|
||||||
if (applicationState.running) {
|
if (applicationState.running) {
|
||||||
throw new Error("Cannot add command while the application is running");
|
throw new Error("Cannot add command while the application is running");
|
||||||
}
|
}
|
||||||
if (!(command instanceof Command)) {
|
if (!(command instanceof TextCommand)) {
|
||||||
throw new TypeError("Inavlid argument type");
|
throw new TypeError("Inavlid argument type");
|
||||||
}
|
}
|
||||||
const nameOccurrence: PhraseOccurrenceData | null = this.findPhraseOccurrence(command.name);
|
const nameOccurrence: PhraseOccurrenceData | null = this.findPhraseOccurrence(command.name);
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ import axios from "axios";
|
|||||||
import { Client, CommandInteraction, GuildMember, Intents, Message } from "discord.js";
|
import { Client, CommandInteraction, GuildMember, Intents, Message } from "discord.js";
|
||||||
import { EventEmitter } from "events";
|
import { EventEmitter } from "events";
|
||||||
import * as http from "http";
|
import * as http from "http";
|
||||||
import { Command } from "./Command.js";
|
import { TextCommand } from "./TextCommand.js";
|
||||||
import { CommandManager } from "../managers/CommandManager.js";
|
import { CommandManager } from "../managers/CommandManager.js";
|
||||||
import { OperationSuccess, PermissionsError } from "../errors.js";
|
import { OperationSuccess, PermissionsError } from "../errors.js";
|
||||||
import { HelpMessage } from "./Help.js";
|
import { HelpMessage } from "./Help.js";
|
||||||
import { SystemMessageManager } from "../managers/SystemMessage.js";
|
import { SystemMessageManager } from "../managers/SystemMessage.js";
|
||||||
import { CommandMessageStructure } from "../types/Command.js";
|
import { CommandMessageStructure } from "../types/TextCommand.js";
|
||||||
import { InitOptions } from "../types/Bot.js";
|
import { InitOptions } from "../types/Bot.js";
|
||||||
import { HelpMessageParams } from "../types/HelpMessage.js";
|
import { HelpMessageParams } from "../types/HelpMessage.js";
|
||||||
import { applicationState } from "../state.js";
|
import { applicationState } from "../state.js";
|
||||||
@@ -132,7 +132,7 @@ export class Bot extends EventEmitter {
|
|||||||
console.log("✔");
|
console.log("✔");
|
||||||
}
|
}
|
||||||
if (this.messages.help.enabled === true) {
|
if (this.messages.help.enabled === true) {
|
||||||
const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.name);
|
const helpMsg: TextCommand = new HelpMessage(this.commands, this.messages.help, this.name);
|
||||||
this.commands.add(helpMsg);
|
this.commands.add(helpMsg);
|
||||||
}
|
}
|
||||||
applicationState.running = true;
|
applicationState.running = true;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { MessageEmbed } from "discord.js";
|
import { MessageEmbed } from "discord.js";
|
||||||
import { Command } from "./Command.js";
|
import { TextCommand } from "./TextCommand.js";
|
||||||
import { CommandManager } from "../managers/CommandManager.js";
|
import { CommandManager } from "../managers/CommandManager.js";
|
||||||
import { HelpMessageParams } from "../types/HelpMessage.js";
|
import { HelpMessageParams } from "../types/HelpMessage.js";
|
||||||
|
|
||||||
export class HelpMessage extends Command {
|
export class HelpMessage extends TextCommand {
|
||||||
constructor(cmdManager: CommandManager, params: HelpMessageParams, botName?: string) {
|
constructor(cmdManager: CommandManager, params: HelpMessageParams, botName?: string) {
|
||||||
super({
|
super({
|
||||||
name: "help",
|
name: "help",
|
||||||
@@ -26,7 +26,7 @@ export class HelpMessage extends Command {
|
|||||||
helpMsg.setFooter(botName || "");
|
helpMsg.setFooter(botName || "");
|
||||||
if (helpMsg != null) {
|
if (helpMsg != null) {
|
||||||
if (p("command_name")) {
|
if (p("command_name")) {
|
||||||
const cmd: Command | null = cmdManager.get(p("command_name")?.toString() || "");
|
const cmd: TextCommand | null = cmdManager.get(p("command_name")?.toString() || "");
|
||||||
if (cmd) {
|
if (cmd) {
|
||||||
if (Array.isArray(cmd.guilds) && cmd.guilds.length > 0 && !cmd.guilds.find((g) => i?.guild?.id === g)) {
|
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`);
|
throw new ReferenceError(`Command "${cmd.name}" is not available`);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Permissions, Message, MessageEmbed, CommandInteraction, GuildMember, ReplyMessageOptions, PermissionResolvable } from "discord.js";
|
import { Permissions, Message, MessageEmbed, CommandInteraction, GuildMember, ReplyMessageOptions, PermissionResolvable } from "discord.js";
|
||||||
import { CommandBuilder } from "../types/Command.js";
|
import { TextCommandInit } from "../types/TextCommand.js";
|
||||||
import { ParameterResolvable } from "../types/Parameter.js";
|
import { ParameterResolvable } from "../types/Parameter.js";
|
||||||
import { PermissionCheckTypes } from "../types/permissions.js";
|
import { PermissionCheckTypes } from "../types/permissions.js";
|
||||||
import { OperationSuccess, PermissionsError } from "../errors.js";
|
import { OperationSuccess, PermissionsError } from "../errors.js";
|
||||||
@@ -9,7 +9,7 @@ import { DefaultParameter, InputParameter, Parameter } from "./Parameter.js";
|
|||||||
* @class Class that represents a command instance
|
* @class Class that represents a command instance
|
||||||
* @exports
|
* @exports
|
||||||
*/
|
*/
|
||||||
export class Command {
|
export class TextCommand {
|
||||||
/**
|
/**
|
||||||
* Command name
|
* Command name
|
||||||
* @type {string}
|
* @type {string}
|
||||||
@@ -81,9 +81,9 @@ export class Command {
|
|||||||
/**
|
/**
|
||||||
* Command constructor
|
* Command constructor
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {CommandBuilder} options - {@link CommandBuilder}
|
* @param {TextCommandInit} options - {@link CommandBuilder}
|
||||||
*/
|
*/
|
||||||
constructor(options: CommandBuilder) {
|
constructor(options: TextCommandInit) {
|
||||||
this.name = options.name.split(" ").join("_");
|
this.name = options.name.split(" ").join("_");
|
||||||
if (options.parameters == "no_input" || !options.parameters) {
|
if (options.parameters == "no_input" || !options.parameters) {
|
||||||
this.parameters = [];
|
this.parameters = [];
|
||||||
@@ -92,7 +92,7 @@ export class Command {
|
|||||||
} else {
|
} else {
|
||||||
this.parameters = options.parameters.map((ps) => new Parameter(ps));
|
this.parameters = options.parameters.map((ps) => new Parameter(ps));
|
||||||
}
|
}
|
||||||
this.aliases = Command.processPhrase(options.aliases);
|
this.aliases = TextCommand.processPhrase(options.aliases);
|
||||||
this.description = options.description || "No description";
|
this.description = options.description || "No description";
|
||||||
this.usage = options.usage || this.generateUsageFromArguments();
|
this.usage = options.usage || this.generateUsageFromArguments();
|
||||||
this.permissionCheck = options.permissionCheck == "ALL" || options.permissionCheck == "ANY" ? options.permissionCheck : "ANY";
|
this.permissionCheck = options.permissionCheck == "ALL" || options.permissionCheck == "ANY" ? options.permissionCheck : "ANY";
|
||||||
@@ -170,7 +170,7 @@ export class Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts {@link Command} instance to object that is recognized by the Discord API
|
* Converts {@link TextCommand} instance to object that is recognized by the Discord API
|
||||||
* @returns {Object} object
|
* @returns {Object} object
|
||||||
*/
|
*/
|
||||||
public toObject() {
|
public toObject() {
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ColorResolvable, GuildMember, User } from "discord.js";
|
import { ColorResolvable, GuildMember, User } from "discord.js";
|
||||||
import { Command } from "../structures/Command.js";
|
import { TextCommand } from "../structures/TextCommand.js";
|
||||||
import { PermissionsError } from "../errors.js";
|
import { PermissionsError } from "../errors.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,9 +61,9 @@ export interface SystemMessageAppearance {
|
|||||||
export interface SystemMessageData {
|
export interface SystemMessageData {
|
||||||
/**
|
/**
|
||||||
* A {@link Command} instance
|
* A {@link Command} instance
|
||||||
* @type {Command}
|
* @type {TextCommand}
|
||||||
*/
|
*/
|
||||||
command?: Command;
|
command?: TextCommand;
|
||||||
/**
|
/**
|
||||||
* Phrase received from a Discord channel
|
* Phrase received from a Discord channel
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { CommandInteraction, ReplyMessageOptions, PermissionResolvable, Message, MessageEmbed } from "discord.js";
|
import { CommandInteraction, ReplyMessageOptions, PermissionResolvable, Message, MessageEmbed } from "discord.js";
|
||||||
import { ParameterSchema, ParameterResolvable } from "./Parameter.js";
|
import { ParameterSchema, ParameterResolvable } from "./Parameter.js";
|
||||||
import { InputParameter } from "../structures/Parameter.js";
|
import { InputParameter } from "../structures/Parameter.js";
|
||||||
import { Command } from "../structures/Command.js";
|
import { TextCommand } from "../structures/TextCommand.js";
|
||||||
import { PermissionCheckTypes } from "./permissions.js";
|
import { PermissionCheckTypes } from "./permissions.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @interface
|
* @interface
|
||||||
* Options for building a {@link Command} object
|
* Options for building a {@link Command} object
|
||||||
*/
|
*/
|
||||||
export interface CommandBuilder {
|
export interface TextCommandInit {
|
||||||
/**
|
/**
|
||||||
* Command name
|
* Command name
|
||||||
* @type {string}
|
* @type {string}
|
||||||
@@ -78,13 +78,13 @@ export interface CommandBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface CommandMessageStructure {
|
export interface CommandMessageStructure {
|
||||||
command: Command;
|
command: TextCommand;
|
||||||
parameters: InputParameter[];
|
parameters: InputParameter[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GetMode = "ALL" | "PREFIX" | "NO_PREFIX";
|
export type GetMode = "ALL" | "PREFIX" | "NO_PREFIX";
|
||||||
|
|
||||||
export interface PhraseOccurrenceData {
|
export interface PhraseOccurrenceData {
|
||||||
command: Command;
|
command: TextCommand;
|
||||||
type: "NAME" | "ALIAS";
|
type: "NAME" | "ALIAS";
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user