Compare commits
7 Commits
94d1291ffb
...
471c749ce4
Author | SHA1 | Date | |
---|---|---|---|
471c749ce4 | |||
3c0b112ac2 | |||
25fcb88885 | |||
4a15edefa1 | |||
92f1f2ee1c | |||
6a74608815 | |||
66beb8dd1d |
5
index.ts
5
index.ts
@ -1,5 +1,6 @@
|
|||||||
export { Parser, ParseError, ArgsResult } from './src/Parser.js';
|
export { Parser, ArgsResult } from './src/Parser.js';
|
||||||
|
|
||||||
|
export { ParserError } from './src/util/Errors/ParserError.js';
|
||||||
export { Command } from './src/classes/Command.js';
|
export { Command } from './src/classes/Command.js';
|
||||||
export { CommandOption } from './src/classes/CommandOption.js';
|
export { CommandOption } from './src/classes/CommandOption.js';
|
||||||
export { SubcommandGroupOption } from './src/classes/SubcommandGroupOption.js';
|
export { SubcommandGroupOption } from './src/classes/SubcommandGroupOption.js';
|
||||||
@ -7,4 +8,4 @@ export { SubcommandOption } from './src/classes/SubcommandOption.js';
|
|||||||
|
|
||||||
export { IResolver } from './src/interfaces/Resolver.js';
|
export { IResolver } from './src/interfaces/Resolver.js';
|
||||||
export { OptionType, CommandOptionDefinition } from './src/interfaces/CommandOption.js';
|
export { OptionType, CommandOptionDefinition } from './src/interfaces/CommandOption.js';
|
||||||
export { CommandDefinition, ICommand } from './src/interfaces/Command.js';
|
export { CommandDefinition, ICommand, CommandOpts } from './src/interfaces/Command.js';
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@navy.gif/commandparser",
|
"name": "@navy.gif/commandparser",
|
||||||
"version": "1.5.3",
|
"version": "1.5.7",
|
||||||
"description": "Parser meant to parse commands and their options for discord bots",
|
"description": "Parser meant to parse commands and their options for discord bots",
|
||||||
"author": "Navy.gif",
|
"author": "Navy.gif",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -7,6 +7,7 @@ import IResolver from './interfaces/Resolver.js';
|
|||||||
import ExtendedMap from "./util/Map.js";
|
import ExtendedMap from "./util/Map.js";
|
||||||
import Util from "./util/Util.js";
|
import Util from "./util/Util.js";
|
||||||
import { ICommand } from '../index.js';
|
import { ICommand } from '../index.js';
|
||||||
|
import { ParserError } from './util/Errors/ParserError.js';
|
||||||
|
|
||||||
type ArgsResult = {
|
type ArgsResult = {
|
||||||
[key: string]: CommandOption
|
[key: string]: CommandOption
|
||||||
@ -33,8 +34,6 @@ type ParseOptions = {
|
|||||||
guild?: unknown
|
guild?: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParseError extends Error {}
|
|
||||||
|
|
||||||
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
|
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
|
||||||
|
|
||||||
class Parser extends EventEmitter {
|
class Parser extends EventEmitter {
|
||||||
@ -103,7 +102,7 @@ class Parser extends EventEmitter {
|
|||||||
|
|
||||||
if (commandFilter && typeof commandFilter === 'function') {
|
if (commandFilter && typeof commandFilter === 'function') {
|
||||||
const result = await commandFilter(command);
|
const result = await commandFilter(command);
|
||||||
if (result) {
|
if (!result) {
|
||||||
this.debug(`Ignoring command ${command.name}`);
|
this.debug(`Ignoring command ${command.name}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -119,7 +118,7 @@ class Parser extends EventEmitter {
|
|||||||
if (subcommandGroups.length || subcommands.length) {
|
if (subcommandGroups.length || subcommands.length) {
|
||||||
this.debug(`Expecting subcommand`);
|
this.debug(`Expecting subcommand`);
|
||||||
if (!params.length)
|
if (!params.length)
|
||||||
throw new ParseError('Expected a subcommand(group), got nothing');
|
throw new ParserError('Expected a subcommand(group), got nothing');
|
||||||
const [ first, second, ...rest ] = params;
|
const [ first, second, ...rest ] = params;
|
||||||
group = command.subcommandGroup(first);
|
group = command.subcommandGroup(first);
|
||||||
|
|
||||||
@ -138,7 +137,7 @@ class Parser extends EventEmitter {
|
|||||||
parseResult.subcommandGroup = group?.name || null;
|
parseResult.subcommandGroup = group?.name || null;
|
||||||
|
|
||||||
if (!subcommand)
|
if (!subcommand)
|
||||||
throw new ParseError(`Expecting subcommand, one of ${subcommands.map((s) => s.name).join(', ')}`); // return { showUsage: true, verbose: true };
|
throw new ParserError(`Expecting subcommand, one of ${subcommands.map((s) => s.name).join(', ')}`); // return { showUsage: true, verbose: true };
|
||||||
this.debug(`Got ${subcommand.name}`);
|
this.debug(`Got ${subcommand.name}`);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -189,7 +188,7 @@ class Parser extends EventEmitter {
|
|||||||
return f.name === _flag || aliased;
|
return f.name === _flag || aliased;
|
||||||
});
|
});
|
||||||
if (!flag)
|
if (!flag)
|
||||||
throw new ParseError(`Unrecognised flag: ${_flag}`);
|
throw new ParserError(`Unrecognised flag: ${_flag}`);
|
||||||
this.debug(`Matched flag: ${flag.name} with ${_flag}`);
|
this.debug(`Matched flag: ${flag.name} with ${_flag}`);
|
||||||
|
|
||||||
params.splice(index, 1, '');
|
params.splice(index, 1, '');
|
||||||
@ -211,9 +210,9 @@ class Parser extends EventEmitter {
|
|||||||
const result = await flag.parse(guild);
|
const result = await flag.parse(guild);
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
if (flag.choices.length) {
|
if (flag.choices.length) {
|
||||||
throw new ParseError(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`);
|
throw new ParserError(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`);
|
||||||
}
|
}
|
||||||
throw new ParseError(`Failed to parse value for ${flag.name}, expected value type: ${OptionType[flag.type]}`); // return { option: flag, ...result.removed };
|
throw new ParserError(`Failed to parse value for ${flag.name}, expected value type: ${OptionType[flag.type]}`); // return { option: flag, ...result.removed };
|
||||||
}
|
}
|
||||||
this.debug(`Cleaning up params after ${flag.name}`);
|
this.debug(`Cleaning up params after ${flag.name}`);
|
||||||
for (const r of result.removed)
|
for (const r of result.removed)
|
||||||
@ -314,7 +313,7 @@ class Parser extends EventEmitter {
|
|||||||
for (const req of options.filter((opt) => opt.required)) {
|
for (const req of options.filter((opt) => opt.required)) {
|
||||||
if (!args[req.name]) {
|
if (!args[req.name]) {
|
||||||
if (req.defaultValue === null || !this.allowDefaultOnRequired)
|
if (req.defaultValue === null || !this.allowDefaultOnRequired)
|
||||||
throw new ParseError(`${req.name} is a required option`);
|
throw new ParserError(`${req.name} is a required option`);
|
||||||
|
|
||||||
const opt = req.clone();
|
const opt = req.clone();
|
||||||
opt.value = req.defaultValue;
|
opt.value = req.defaultValue;
|
||||||
@ -323,7 +322,7 @@ class Parser extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strings.length)
|
if (strings.length)
|
||||||
throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`);
|
throw new ParserError(`Unrecognised option(s): "${strings.join('", "')}"`);
|
||||||
|
|
||||||
return args;
|
return args;
|
||||||
|
|
||||||
@ -336,4 +335,4 @@ class Parser extends EventEmitter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Parser, ParseError, ArgsResult };
|
export { Parser, ArgsResult };
|
@ -4,7 +4,11 @@ import SubcommandOption from '../classes/SubcommandOption.js';
|
|||||||
import { ArgsResult } from '../Parser.js';
|
import { ArgsResult } from '../Parser.js';
|
||||||
import { CommandOptionDefinition } from './CommandOption.js';
|
import { CommandOptionDefinition } from './CommandOption.js';
|
||||||
|
|
||||||
|
export type CommandOpts = {
|
||||||
|
args?: ArgsResult,
|
||||||
|
subcommand?: string | null,
|
||||||
|
subcommandGroup?: string | null
|
||||||
|
}
|
||||||
interface ICommand {
|
interface ICommand {
|
||||||
|
|
||||||
name: string,
|
name: string,
|
||||||
@ -20,7 +24,7 @@ interface ICommand {
|
|||||||
|
|
||||||
subcommandGroup(name: string): SubcommandGroupOption | null
|
subcommandGroup(name: string): SubcommandGroupOption | null
|
||||||
|
|
||||||
execute(message: unknown, args?: ArgsResult): Promise<unknown>
|
execute(message: unknown, opts?: CommandOpts): Promise<unknown>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ interface IResolver {
|
|||||||
* @return {Member} {Member}
|
* @return {Member} {Member}
|
||||||
* @memberof IResolver
|
* @memberof IResolver
|
||||||
*/
|
*/
|
||||||
resolveMember<Guild, Member>(resolveable: string, strict: boolean, guild: Guild): Promise<Member | null>
|
resolveMember<Member, Guild>(resolveable: string, strict: boolean, guild: Guild): Promise<Member | null>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should resolve to a role abstraction
|
* Should resolve to a role abstraction
|
||||||
@ -30,7 +30,7 @@ interface IResolver {
|
|||||||
* @return {Channel} {Channel}
|
* @return {Channel} {Channel}
|
||||||
* @memberof IResolver
|
* @memberof IResolver
|
||||||
*/
|
*/
|
||||||
resolveChannel<Guild, Channel>(resolveable: string, strict: boolean, guild: Guild): Promise<Channel | null>
|
resolveChannel<Channel, Guild>(resolveable: string, strict: boolean, guild: Guild): Promise<Channel | null>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should resolve to a role abstraction
|
* Should resolve to a role abstraction
|
||||||
@ -41,7 +41,7 @@ interface IResolver {
|
|||||||
* @return {Role} {Role}
|
* @return {Role} {Role}
|
||||||
* @memberof IResolver
|
* @memberof IResolver
|
||||||
*/
|
*/
|
||||||
resolveRole<Guild, Role>(resolveable: string, strict: boolean, guild: Guild): Promise<Role | null>
|
resolveRole<Role, Guild>(resolveable: string, strict: boolean, guild: Guild): Promise<Role | null>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should resolve to true when a truthy resolveable is passed, i.e. yes, true, on etc
|
* Should resolve to true when a truthy resolveable is passed, i.e. yes, true, on etc
|
||||||
|
6
src/util/Errors/ParserError.ts
Normal file
6
src/util/Errors/ParserError.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export class ParserError extends Error {
|
||||||
|
constructor (message: string, options?: ErrorOptions) {
|
||||||
|
super(message, options);
|
||||||
|
this.name = 'ParserError';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user