Compare commits
No commits in common. "bdd73dd6a94c4434a6252efcc5e540422dae1cf4" and "6686aa1fc1a2e9fb0c993918696857bc7e07e4dd" have entirely different histories.
bdd73dd6a9
...
6686aa1fc1
@ -106,7 +106,7 @@
|
|||||||
"no-extend-native": "warn",
|
"no-extend-native": "warn",
|
||||||
"no-extra-bind": "warn",
|
"no-extra-bind": "warn",
|
||||||
"no-extra-label": "warn",
|
"no-extra-label": "warn",
|
||||||
// "no-extra-parens": "warn",
|
"no-extra-parens": "warn",
|
||||||
"no-floating-decimal": "warn",
|
"no-floating-decimal": "warn",
|
||||||
"no-implicit-coercion": "warn",
|
"no-implicit-coercion": "warn",
|
||||||
"no-implicit-globals": "warn",
|
"no-implicit-globals": "warn",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "commandparser",
|
"name": "commandparser",
|
||||||
"version": "1.0.24",
|
"version": "1.0.19",
|
||||||
"description": "Parser meant to parse commands and their options for discord bots",
|
"description": "Parser meant to parse commands and their options for discord bots",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"author": "Navy.gif",
|
"author": "Navy.gif",
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// eslint-disable-next-line max-classes-per-file
|
// eslint-disable-next-line max-classes-per-file
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import Command from './classes/Command';
|
|
||||||
import CommandOption from './classes/CommandOption';
|
import CommandOption from './classes/CommandOption';
|
||||||
|
|
||||||
|
import ICommand from "./interfaces/Command";
|
||||||
import { OptionType } from "./interfaces/CommandOption";
|
import { OptionType } from "./interfaces/CommandOption";
|
||||||
import IResolver from './interfaces/Resolver';
|
import IResolver from './interfaces/Resolver';
|
||||||
import ExtendedMap from "./util/Map";
|
import ExtendedMap from "./util/Map";
|
||||||
@ -14,13 +14,13 @@ type ArgsResult = {
|
|||||||
|
|
||||||
type ParseResult = {
|
type ParseResult = {
|
||||||
args: ArgsResult,
|
args: ArgsResult,
|
||||||
command: Command,
|
command: ICommand,
|
||||||
subcommand: string | null,
|
subcommand: string | null,
|
||||||
subcommandGroup: string | null
|
subcommandGroup: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParserOptions = {
|
type ParserOptions = {
|
||||||
commands: Iterable<Command>,
|
commands: Iterable<ICommand>,
|
||||||
prefix?: string,
|
prefix?: string,
|
||||||
debug?: boolean,
|
debug?: boolean,
|
||||||
resolver: IResolver<unknown, unknown, unknown, unknown, unknown>
|
resolver: IResolver<unknown, unknown, unknown, unknown, unknown>
|
||||||
@ -32,7 +32,7 @@ const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
|
|||||||
|
|
||||||
class Parser extends EventEmitter {
|
class Parser extends EventEmitter {
|
||||||
|
|
||||||
private commands: ExtendedMap<string, Command>;
|
private commands: ExtendedMap<string, ICommand>;
|
||||||
|
|
||||||
private _debug = false;
|
private _debug = false;
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ class Parser extends EventEmitter {
|
|||||||
super();
|
super();
|
||||||
|
|
||||||
this._debug = debug;
|
this._debug = debug;
|
||||||
this.commands = new ExtendedMap<string, Command>();
|
this.commands = new ExtendedMap<string, ICommand>();
|
||||||
for (const command of commands) this.commands.set(command.name, command);
|
for (const command of commands) this.commands.set(command.name, command);
|
||||||
this.resolver = resolver;
|
this.resolver = resolver;
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ class Parser extends EventEmitter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private matchCommand(name: string): Command | null {
|
private matchCommand(name: string): ICommand | null {
|
||||||
let command = null;
|
let command = null;
|
||||||
if (this.commands.has(name)) command = this.commands.get(name);
|
if (this.commands.has(name)) command = this.commands.get(name);
|
||||||
else command = this.commands.find((c) => c.aliases.includes(name));
|
else command = this.commands.find((c) => c.aliases.includes(name));
|
||||||
@ -73,7 +73,7 @@ class Parser extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
async parseMessage(message: string, prefix = this.prefix,
|
async parseMessage(message: string, prefix = this.prefix,
|
||||||
guild?: unknown, commandFilter?: (command: Command) => boolean): Promise<ParseResult | null> {
|
guild?: unknown, commandFilter?: (command: ICommand) => boolean): Promise<ParseResult | null> {
|
||||||
|
|
||||||
if (!message.startsWith(prefix) || !message.length) return null;
|
if (!message.startsWith(prefix) || !message.length) return null;
|
||||||
let params: string[] = message.replace(prefix, '').split(' ').filter((str) => str.length);
|
let params: string[] = message.replace(prefix, '').split(' ').filter((str) => str.length);
|
||||||
@ -82,16 +82,9 @@ class Parser extends EventEmitter {
|
|||||||
|
|
||||||
const command = this.matchCommand(commandName);
|
const command = this.matchCommand(commandName);
|
||||||
if (!command) return null;
|
if (!command) return null;
|
||||||
|
if (commandFilter && typeof commandFilter === 'function' && await commandFilter(command)) return null;
|
||||||
|
|
||||||
this.debug(`Matched command ${command.name}`);
|
this.debug(`Matched command ${command.name}`);
|
||||||
|
|
||||||
if (commandFilter && typeof commandFilter === 'function') {
|
|
||||||
const result = await commandFilter(command);
|
|
||||||
if (result) {
|
|
||||||
this.debug(`Ignoring command ${command.name}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { subcommands, subcommandGroups } = command;
|
const { subcommands, subcommandGroups } = command;
|
||||||
const args: ArgsResult = {};
|
const args: ArgsResult = {};
|
||||||
const parseResult: ParseResult = { args, command, subcommand: null, subcommandGroup: null };
|
const parseResult: ParseResult = { args, command, subcommand: null, subcommandGroup: null };
|
||||||
|
@ -44,7 +44,7 @@ class CommandOption implements ICommandOption {
|
|||||||
|
|
||||||
private resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>|undefined = undefined;
|
private resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>|undefined = undefined;
|
||||||
|
|
||||||
constructor(def: CommandOptionDefinition|CommandOption|ICommandOption) {
|
constructor(def: CommandOptionDefinition|ICommandOption) {
|
||||||
|
|
||||||
this.name = def.name;
|
this.name = def.name;
|
||||||
this.aliases = def.aliases || [];
|
this.aliases = def.aliases || [];
|
||||||
@ -101,46 +101,6 @@ class CommandOption implements ICommandOption {
|
|||||||
return { value: member, removed: this.rawValue };
|
return { value: member, removed: this.rawValue };
|
||||||
}
|
}
|
||||||
|
|
||||||
protected STRING() {
|
|
||||||
if (!this.rawValue) return { error: true };
|
|
||||||
if (this.aliased) return { value: this.rawValue.join(' '), removed: [] };
|
|
||||||
if (this.choices.length) {
|
|
||||||
const found = this.choices.find((c) => {
|
|
||||||
const choice = c as string;
|
|
||||||
return choice.toLowerCase() === this.rawValue?.join(' ').toLowerCase();
|
|
||||||
});
|
|
||||||
if (found) return { value: found, removed: this.rawValue };
|
|
||||||
return { error: true };
|
|
||||||
}
|
|
||||||
return { value: this.rawValue.join(' '), removed: this.rawValue };
|
|
||||||
}
|
|
||||||
|
|
||||||
protected INTEGER() {
|
|
||||||
if (!this.rawValue) return { error: true };
|
|
||||||
const integer = parseInt(this.rawValue[0]);
|
|
||||||
if(isNaN(integer)) return { error: true };
|
|
||||||
if (this.minimum !== undefined && integer < this.minimum) return { error: true };
|
|
||||||
if (this.maximum !== undefined && integer > this.maximum) return { error: true };
|
|
||||||
return { value: integer, removed: [this.rawValue[0]] };
|
|
||||||
}
|
|
||||||
|
|
||||||
protected FLOAT() {
|
|
||||||
if (!this.rawValue) return { error: true };
|
|
||||||
const float = parseFloat(this.rawValue[0]);
|
|
||||||
if(isNaN(float)) return { error: true };
|
|
||||||
if (this.minimum !== undefined && float < this.minimum) return { error: true };
|
|
||||||
if (this.maximum !== undefined && float > this.maximum) return { error: true };
|
|
||||||
return { value: (float), removed: [this.rawValue[0]] };
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BOOLEAN() {
|
|
||||||
if (!this.rawValue) return { error: true };
|
|
||||||
const boolean = this.resolver?.resolveBoolean(this.rawValue[0]) || null;
|
|
||||||
if (boolean === null && this.valueOptional) return { value: this.defaultValue, removed: [] };
|
|
||||||
else if(boolean === null) return { error: true };
|
|
||||||
return { value: boolean, removed: [this.rawValue[0]] };
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { CommandOption };
|
export { CommandOption };
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
const { Parser, Command, CommandOption, OptionType } = require('../build/index');
|
|
||||||
|
|
||||||
const opt = new CommandOption({
|
|
||||||
name: 'text',
|
|
||||||
type: OptionType.STRING
|
|
||||||
});
|
|
||||||
class TestCommand extends Command {
|
|
||||||
constructor() {
|
|
||||||
super({
|
|
||||||
name: 'test',
|
|
||||||
options: [opt]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const parser = new Parser({
|
|
||||||
commands: [new TestCommand()],
|
|
||||||
resolver: null
|
|
||||||
});
|
|
||||||
|
|
||||||
(async () => {
|
|
||||||
const parsed = await parser.parseMessage('test message.author')
|
|
||||||
console.log(parsed)
|
|
||||||
})();
|
|
Loading…
Reference in New Issue
Block a user