fleshing out structures
This commit is contained in:
parent
f93ba28867
commit
c6aa17fff4
@ -52,7 +52,7 @@ class Parser extends EventEmitter {
|
|||||||
return command || null;
|
return command || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async parseMessage(message: string, prefix = this.prefix) {
|
async parseMessage(message: string, prefix = this.prefix, guild?: unknown) {
|
||||||
|
|
||||||
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);
|
||||||
@ -143,7 +143,7 @@ class Parser extends EventEmitter {
|
|||||||
// Clean up params for option parsing
|
// Clean up params for option parsing
|
||||||
for (const flag of Object.values(args)) {
|
for (const flag of Object.values(args)) {
|
||||||
this.debug(`Running parser for ${flag.name}`);
|
this.debug(`Running parser for ${flag.name}`);
|
||||||
const result = await flag.parse();
|
const result = await flag.parse(guild);
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
if (flag.choices.length) {
|
if (flag.choices.length) {
|
||||||
throw new Error(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`);
|
throw new Error(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`);
|
||||||
@ -173,14 +173,14 @@ class Parser extends EventEmitter {
|
|||||||
error = false;
|
error = false;
|
||||||
if (cloned.plural) { // E.g. if the type is CHANNEL**S**, parse out any potential channels from the message
|
if (cloned.plural) { // E.g. if the type is CHANNEL**S**, parse out any potential channels from the message
|
||||||
cloned.rawValue = params;
|
cloned.rawValue = params;
|
||||||
({ removed } = await cloned.parse());
|
({ removed } = await cloned.parse(guild));
|
||||||
} else for (let index = 0; index < params.length;) { // Attempt to parse out a value from each param
|
} else for (let index = 0; index < params.length;) { // Attempt to parse out a value from each param
|
||||||
if (params[index] === null) {
|
if (params[index] === null) {
|
||||||
index++;
|
index++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cloned.rawValue = [params[index]];
|
cloned.rawValue = [params[index]];
|
||||||
({ removed, error } = await cloned.parse());
|
({ removed, error } = await cloned.parse(guild));
|
||||||
if (!error) break;
|
if (!error) break;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
@ -220,7 +220,7 @@ class Parser extends EventEmitter {
|
|||||||
const val = strings.shift();
|
const val = strings.shift();
|
||||||
if (!val) break;
|
if (!val) break;
|
||||||
const cloned = strOpt.clone([val]);
|
const cloned = strOpt.clone([val]);
|
||||||
await cloned.parse();
|
await cloned.parse(guild);
|
||||||
args[cloned.name] = cloned;
|
args[cloned.name] = cloned;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,11 @@ class Command implements ICommand {
|
|||||||
|
|
||||||
this.aliases = def.aliases || [];
|
this.aliases = def.aliases || [];
|
||||||
|
|
||||||
this.options = def.options || [];
|
this.options = [];
|
||||||
|
for (const opt of def.options || []) {
|
||||||
|
if (opt instanceof CommandOption) this.options.push(opt);
|
||||||
|
else this.options.push(new CommandOption(opt));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +40,8 @@ class CommandOption implements ICommandOption {
|
|||||||
|
|
||||||
aliased = false;
|
aliased = false;
|
||||||
|
|
||||||
|
strict: boolean;
|
||||||
|
|
||||||
private resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>|undefined = undefined;
|
private resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>|undefined = undefined;
|
||||||
|
|
||||||
constructor(def: CommandOptionDefinition|ICommandOption) {
|
constructor(def: CommandOptionDefinition|ICommandOption) {
|
||||||
@ -50,6 +52,7 @@ class CommandOption implements ICommandOption {
|
|||||||
this.options = def.options || [];
|
this.options = def.options || [];
|
||||||
this.choices = def.choices || [];
|
this.choices = def.choices || [];
|
||||||
|
|
||||||
|
this.strict = def.strict || false;
|
||||||
this.type = def.type || OptionType.STRING;
|
this.type = def.type || OptionType.STRING;
|
||||||
this.flag = def.flag || false;
|
this.flag = def.flag || false;
|
||||||
|
|
||||||
@ -71,9 +74,13 @@ class CommandOption implements ICommandOption {
|
|||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
|
|
||||||
async parse(): Promise<ParseResult> {
|
async parse(guild?: unknown): Promise<ParseResult> {
|
||||||
if(!this[OptionType[this.type]]) throw new Error(`Missing parsing function for ${this.type}`);
|
if (!this[OptionType[this.type]]) throw new Error(`Missing parsing function for ${this.type}`);
|
||||||
const result = await this[OptionType[this.type]];
|
if (typeof this[OptionType[this.type]] !== 'function') throw new Error(`Expected function type for memberr ${OptionType[this.type]}`);
|
||||||
|
this.guild = guild;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
|
const func = this[OptionType[this.type]] as Function;
|
||||||
|
const result = await func();
|
||||||
return result as ParseResult;
|
return result as ParseResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +91,7 @@ class CommandOption implements ICommandOption {
|
|||||||
protected async MEMBER() {
|
protected async MEMBER() {
|
||||||
if (!this.resolver) throw new Error('Missing resolver');
|
if (!this.resolver) throw new Error('Missing resolver');
|
||||||
if(!this.rawValue) throw new Error('Missing raw value');
|
if(!this.rawValue) throw new Error('Missing raw value');
|
||||||
const member = await this.resolver?.resolveMember(this.rawValue[0]);
|
const member = await this.resolver?.resolveMember(this.rawValue[0], this.strict, this.guild);
|
||||||
if (!member) return { error: true };
|
if (!member) return { error: true };
|
||||||
return { value: member, removed: this.rawValue };
|
return { value: member, removed: this.rawValue };
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import CommandOption from '../classes/CommandOption';
|
import CommandOption from '../classes/CommandOption';
|
||||||
import SubcommandGroupOption from '../classes/SubcommandGroupOption';
|
import SubcommandGroupOption from '../classes/SubcommandGroupOption';
|
||||||
import SubcommandOption from '../classes/SubcommandOption';
|
import SubcommandOption from '../classes/SubcommandOption';
|
||||||
|
import { CommandOptionDefinition } from './CommandOption';
|
||||||
|
|
||||||
|
|
||||||
interface ICommand {
|
interface ICommand {
|
||||||
@ -22,7 +23,7 @@ interface ICommand {
|
|||||||
type CommandDefinition = {
|
type CommandDefinition = {
|
||||||
name: string;
|
name: string;
|
||||||
aliases?: string[];
|
aliases?: string[];
|
||||||
options?: CommandOption[];
|
options?: (CommandOption|CommandOptionDefinition)[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export { ICommand, CommandDefinition };
|
export { ICommand, CommandDefinition };
|
||||||
|
@ -74,6 +74,7 @@ type CommandOptionDefinition = {
|
|||||||
// eslint-disable-next-line no-use-before-define
|
// eslint-disable-next-line no-use-before-define
|
||||||
options?: CommandOption[];
|
options?: CommandOption[];
|
||||||
type?: OptionType;
|
type?: OptionType;
|
||||||
|
strict?: boolean
|
||||||
|
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
choices?: Choice[];
|
choices?: Choice[];
|
||||||
@ -102,6 +103,7 @@ interface ICommandOption {
|
|||||||
options: CommandOption[];
|
options: CommandOption[];
|
||||||
choices: Choice[]
|
choices: Choice[]
|
||||||
type: OptionType;
|
type: OptionType;
|
||||||
|
strict: boolean;
|
||||||
|
|
||||||
required: boolean;
|
required: boolean;
|
||||||
|
|
||||||
@ -127,7 +129,7 @@ interface ICommandOption {
|
|||||||
|
|
||||||
clone(rawValue?: string[], resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>): CommandOption
|
clone(rawValue?: string[], resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>): CommandOption
|
||||||
|
|
||||||
parse(): Promise<ParseResult>
|
parse(guild: unknown): Promise<ParseResult>
|
||||||
|
|
||||||
get plural(): boolean
|
get plural(): boolean
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ interface IResolver <User, Member, Channel, Role, Guild> {
|
|||||||
* @return {Member} {Member}
|
* @return {Member} {Member}
|
||||||
* @memberof IResolver
|
* @memberof IResolver
|
||||||
*/
|
*/
|
||||||
resolveMember(resolveable: string, strict?: boolean, guild?: Guild): Promise<Member | null>
|
resolveMember(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 <User, Member, Channel, Role, Guild> {
|
|||||||
* @return {Channel} {Channel}
|
* @return {Channel} {Channel}
|
||||||
* @memberof IResolver
|
* @memberof IResolver
|
||||||
*/
|
*/
|
||||||
resolveChannel(resolveable: string, strict?: boolean, guild?: Guild): Promise<Channel | null>
|
resolveChannel(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 <User, Member, Channel, Role, Guild> {
|
|||||||
* @return {Role} {Role}
|
* @return {Role} {Role}
|
||||||
* @memberof IResolver
|
* @memberof IResolver
|
||||||
*/
|
*/
|
||||||
resolveRole(resolveable: string, strict?: boolean, guild?: Guild): Promise<Role | null>
|
resolveRole(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
|
||||||
|
Loading…
Reference in New Issue
Block a user