3.0.0-beta2 - renamed "argument" to "parameter" + added more exports

This commit is contained in:
mmacierzanka committed 2021-08-04 00:18:48 +02:00
1 parent aea31d26ca
commit 8d114eb726
6 files changed
+50 -45

No files matched your search

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