Archived
3.0.0-beta2 - renamed "argument" to "parameter" + added more exports
This commit is contained in:
1 parent
aea31d26ca
commit
8d114eb726
6 files changed
+50
-45
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { Bot } from "./src/Bot.js";
|
||||
import { Command } from "./src/Command.js";
|
||||
import { ParameterResolvable, ParameterType } from "./src/Parameter.js";
|
||||
import { MessageEmbed } from "discord.js";
|
||||
|
||||
export default Bot;
|
||||
export { Bot, Command, MessageEmbed };
|
||||
export { Bot, Command, MessageEmbed, ParameterResolvable, ParameterType };
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "commandbot",
|
||||
"version": "3.0.0-beta1",
|
||||
"version": "3.0.0-beta2",
|
||||
"description": "A framework that helps you create your own Discord bot easier.",
|
||||
"main": "dist/cjs/index.js",
|
||||
"module": "dist/mjs/index.js",
|
||||
|
||||
+2
-2
@@ -42,7 +42,7 @@ export class Bot extends EventEmitter {
|
||||
* @param {ConstructorOptions} options - instance properties
|
||||
* @param {string} options.name - name of your bot
|
||||
* @param {string} options.prefix - prefix used to call commands
|
||||
* @param {string} [options.argumentSeparator=','] - used to get arguments from message
|
||||
* @param {string} [options.argumentSeparator=','] - used to get parameters from message
|
||||
* @param {ClientOptions} [options.clientOptions] - client options from Discord.js
|
||||
* @param {string} [options.token] - bot token from Discord Developer Portal
|
||||
*/
|
||||
@@ -110,7 +110,7 @@ export class Bot extends EventEmitter {
|
||||
if (cmdMsg) {
|
||||
this.emit("COMMAND", m, cmdMsg);
|
||||
try {
|
||||
await cmdMsg.command.start(m, cmdMsg.arguments);
|
||||
await cmdMsg.command.start(m, cmdMsg.parameters);
|
||||
} catch (e) {
|
||||
if (e instanceof PermissionsError) {
|
||||
this.messages.system.send(
|
||||
|
||||
+26
-22
@@ -5,19 +5,23 @@ import {
|
||||
Message,
|
||||
MessageEmbed,
|
||||
} from "discord.js";
|
||||
import { MissingArgumentError, PermissionsError } from "./Error.js";
|
||||
import { Argument, ArgumentResolvable, ProcessArgument } from "./Arguments.js";
|
||||
import { MissingParameterError, PermissionsError } from "./Error.js";
|
||||
import {
|
||||
Parameter,
|
||||
ParameterResolvable,
|
||||
ProcessArgument,
|
||||
} from "./Parameter.js";
|
||||
|
||||
//TYPE DEFINITIONS
|
||||
type GetMode = "ALL" | "PREFIX" | "NO_PREFIX";
|
||||
type PermissionCheckTypes = "ALL" | "ANY";
|
||||
export interface CommandMessageStructure {
|
||||
command: Command;
|
||||
arguments: string[];
|
||||
parameters: string[];
|
||||
}
|
||||
interface CommandBuilder {
|
||||
name: string;
|
||||
arguments?: Argument[];
|
||||
parameters?: Parameter[];
|
||||
aliases?: string[] | string;
|
||||
keywords?: string[] | string;
|
||||
description?: string;
|
||||
@@ -27,7 +31,7 @@ interface CommandBuilder {
|
||||
visible?: boolean;
|
||||
function: (
|
||||
message?: Message,
|
||||
cmdArguments?: ArgumentResolvable[]
|
||||
cmdParams?: ParameterResolvable[]
|
||||
) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>;
|
||||
}
|
||||
interface PhraseOccurrenceData {
|
||||
@@ -38,7 +42,7 @@ interface PhraseOccurrenceData {
|
||||
//CLASSES
|
||||
export class Command {
|
||||
name: string;
|
||||
arguments: Argument[];
|
||||
parameters: Parameter[];
|
||||
aliases: string[];
|
||||
keywords: string[];
|
||||
description: string;
|
||||
@@ -48,7 +52,7 @@ export class Command {
|
||||
visible: boolean;
|
||||
private function: (
|
||||
message?: Message,
|
||||
cmdArguments?: ArgumentResolvable[]
|
||||
cmdParams?: ParameterResolvable[]
|
||||
) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>;
|
||||
|
||||
/**
|
||||
@@ -67,7 +71,7 @@ export class Command {
|
||||
*/
|
||||
constructor(options: CommandBuilder) {
|
||||
this.name = options.name.split(" ").join("_");
|
||||
this.arguments = options.arguments || [];
|
||||
this.parameters = options.parameters || [];
|
||||
this.aliases = Command.processPhrase(options.aliases);
|
||||
this.keywords = Command.processPhrase(options.keywords);
|
||||
this.description = options.description || "No description";
|
||||
@@ -84,10 +88,10 @@ export class Command {
|
||||
/**
|
||||
* Starts the command
|
||||
* @param {Message} [message] - a *Message* object used to check caller's permissions. It will get passed to the execution function (specified in *function* property of command's constructor)
|
||||
* @param {string[]} [cmdArguments] - list of processed arguments passed in a Discord message
|
||||
* @param {string[]} [cmdParams] - list of processed parameters passed in a Discord message
|
||||
* @returns *Promise<void>*
|
||||
*/
|
||||
async start(message?: Message, cmdArguments?: string[]): Promise<void> {
|
||||
async start(message?: Message, cmdParams?: string[]): Promise<void> {
|
||||
const memberPermissions: Readonly<Permissions> =
|
||||
message?.member?.permissions || new Permissions(0);
|
||||
if (
|
||||
@@ -95,11 +99,11 @@ export class Command {
|
||||
? memberPermissions.has(this.permissions, true)
|
||||
: memberPermissions.any(this.permissions, true)
|
||||
) {
|
||||
let inputArguments: ArgumentResolvable[] = cmdArguments || [];
|
||||
if (this.arguments.length > 0) {
|
||||
this.arguments.map((a, i) => {
|
||||
let inputArguments: ParameterResolvable[] = cmdParams || [];
|
||||
if (this.parameters.length > 0) {
|
||||
this.parameters.map((a, i) => {
|
||||
if (!inputArguments[i] && !a.optional) {
|
||||
throw new MissingArgumentError(a);
|
||||
throw new MissingParameterError(a);
|
||||
} else if (inputArguments[i]) {
|
||||
inputArguments[i] = ProcessArgument(
|
||||
inputArguments[i] as string,
|
||||
@@ -108,7 +112,7 @@ export class Command {
|
||||
}
|
||||
});
|
||||
}
|
||||
const fnResult = await this.function(message, cmdArguments);
|
||||
const fnResult = await this.function(message, cmdParams);
|
||||
if (typeof fnResult == "string") {
|
||||
await message?.reply(fnResult);
|
||||
} else if (fnResult instanceof MessageEmbed) {
|
||||
@@ -141,7 +145,7 @@ export class Command {
|
||||
|
||||
private generateUsageFromArguments(): string {
|
||||
let usageTemplate: string = "";
|
||||
this.arguments.map((e) => {
|
||||
this.parameters.map((e) => {
|
||||
usageTemplate += `[${e.name} (${e.type}${
|
||||
e.optional ? ", optional" : ""
|
||||
})] `;
|
||||
@@ -254,7 +258,7 @@ export class CommandManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches command and arguments from the given *Message* object
|
||||
* Fetches command and parameters from the given *Message* object
|
||||
* @param {Message} message - *Message* object
|
||||
* @returns *CommandMessagesStructure* | *null*
|
||||
*/
|
||||
@@ -271,20 +275,20 @@ export class CommandManager {
|
||||
const command = this.get(name, prefix ? "PREFIX" : "NO_PREFIX");
|
||||
if (command) {
|
||||
const argumentsText = content.replace(name, "");
|
||||
const argumentsList = argumentsText
|
||||
const paramsList = argumentsText
|
||||
.split(this.argumentSeparator)
|
||||
.map((a) => {
|
||||
return a.replace(" ", "");
|
||||
});
|
||||
if (
|
||||
(argumentsList[0] == "" || argumentsList[0] == " ") &&
|
||||
argumentsList.length == 1
|
||||
(paramsList[0] == "" || paramsList[0] == " ") &&
|
||||
paramsList.length == 1
|
||||
) {
|
||||
argumentsList.splice(0, 1);
|
||||
paramsList.splice(0, 1);
|
||||
}
|
||||
return {
|
||||
command: command,
|
||||
arguments: argumentsList,
|
||||
parameters: paramsList,
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
|
||||
+9
-9
@@ -1,6 +1,6 @@
|
||||
import { Command } from "./Command.js";
|
||||
import { GuildMember } from "discord.js";
|
||||
import { Argument, ArgumentType } from "./Arguments.js";
|
||||
import { Parameter, ParameterType } from "./Parameter.js";
|
||||
|
||||
export class PermissionsError extends Error {
|
||||
private command: Command;
|
||||
@@ -15,26 +15,26 @@ export class PermissionsError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class ArgumentTypeError extends TypeError {
|
||||
export class ParameterTypeError extends TypeError {
|
||||
private stringContent: string;
|
||||
private type: ArgumentType;
|
||||
constructor(s: string, type: ArgumentType) {
|
||||
private type: ParameterType;
|
||||
constructor(s: string, type: ParameterType) {
|
||||
super();
|
||||
this.stringContent = s;
|
||||
this.type = type;
|
||||
}
|
||||
toString() {
|
||||
return `Argument "${this.stringContent}" cannot be converted to ${this.type}`;
|
||||
return `Parameter "${this.stringContent}" cannot be converted to ${this.type}`;
|
||||
}
|
||||
}
|
||||
|
||||
export class MissingArgumentError extends ReferenceError {
|
||||
private argument: Argument;
|
||||
constructor(a: Argument) {
|
||||
export class MissingParameterError extends ReferenceError {
|
||||
private argument: Parameter;
|
||||
constructor(a: Parameter) {
|
||||
super();
|
||||
this.argument = a;
|
||||
}
|
||||
toString() {
|
||||
return `Your request is missing a "${this.argument.name}" argument which is not optional`;
|
||||
return `Your request is missing a "${this.argument.name}" parameter which is not optional`;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,30 @@
|
||||
import { ArgumentTypeError } from "./Error.js";
|
||||
import { ParameterTypeError } from "./Error.js";
|
||||
|
||||
export type ArgumentType = "STRING" | "BOOLEAN" | "INTEGER" | "FLOAT";
|
||||
export type ArgumentResolvable = string | boolean | number;
|
||||
export interface Argument {
|
||||
export type ParameterType = "STRING" | "BOOLEAN" | "INTEGER" | "FLOAT";
|
||||
export type ParameterResolvable = string | boolean | number;
|
||||
export interface Parameter {
|
||||
name: string;
|
||||
optional: boolean;
|
||||
type: ArgumentType;
|
||||
type: ParameterType;
|
||||
}
|
||||
export const ProcessArgument = (
|
||||
a: string,
|
||||
type: ArgumentType
|
||||
): ArgumentResolvable => {
|
||||
type: ParameterType
|
||||
): ParameterResolvable => {
|
||||
switch (type) {
|
||||
case "STRING":
|
||||
return a;
|
||||
case "INTEGER":
|
||||
const numI = parseInt(a);
|
||||
if (isNaN(numI)) {
|
||||
throw new ArgumentTypeError(a, type);
|
||||
throw new ParameterTypeError(a, type);
|
||||
} else {
|
||||
return numI;
|
||||
}
|
||||
case "FLOAT":
|
||||
const numF = parseFloat(a);
|
||||
if (isNaN(numF)) {
|
||||
throw new ArgumentTypeError(a, type);
|
||||
throw new ParameterTypeError(a, type);
|
||||
} else {
|
||||
return numF;
|
||||
}
|
||||
@@ -34,7 +34,7 @@ export const ProcessArgument = (
|
||||
} else if (a.toLowerCase() == "false") {
|
||||
return false;
|
||||
} else {
|
||||
throw new ArgumentTypeError(a, type);
|
||||
throw new ParameterTypeError(a, type);
|
||||
}
|
||||
default:
|
||||
throw new TypeError("Invalied type specified");
|
||||
Reference in new issue
Block a user