command handler

This commit is contained in:
Erik 2022-04-25 14:08:41 +03:00
parent 17841a4c00
commit 423337a7f0
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -29,6 +29,9 @@ class CommandHandler extends Observer {
|| message.author.bot || message.author.bot
|| message.guild && !message.guild.available) return undefined; || message.guild && !message.guild.available) return undefined;
const userWrapper = await this.client.getUserWrapper(message.author.id);
if (!userWrapper.developer) return;
if(message.guild) { if(message.guild) {
if(!message.member) await message.guild.members.fetch(message.author.id); if(!message.member) await message.guild.members.fetch(message.author.id);
} }
@ -42,10 +45,12 @@ class CommandHandler extends Observer {
const inhibitors = await this._handleInhibitors(invoker); const inhibitors = await this._handleInhibitors(invoker);
if (inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] }); if (inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] });
const options = await this._parseMessage(invoker, parameters); const response = await this._parseMessage(invoker, parameters);
// There was an error if _parseResponse return value is truthy, i.e. an error message was sent
if (await this._parseResponse(invoker, response)) return;
// Temp setup // Temp setup
//this._executeCommand(invoker, parameters); this._executeCommand(invoker, response.options);
} }
@ -58,7 +63,6 @@ class CommandHandler extends Observer {
|| interaction.guild && !interaction.guild.available) return undefined; || interaction.guild && !interaction.guild.available) return undefined;
const command = this._matchCommand(interaction.commandName); const command = this._matchCommand(interaction.commandName);
// interaction.command = command;
const invoker = new InvokerWrapper(this.client, interaction, command); const invoker = new InvokerWrapper(this.client, interaction, command);
if (!command) return invoker.reply({ if (!command) return invoker.reply({
@ -70,6 +74,15 @@ class CommandHandler extends Observer {
if(inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] }); if(inhibitors.length) return this._generateError(invoker, { type: 'inhibitor', ...inhibitors[0] });
const response = await this._parseInteraction(interaction, command); const response = await this._parseInteraction(interaction, command);
if (await this._parseResponse(invoker, response)) return;
this.logger.info(`${interaction.user.tag} (${interaction.user.id}) is executing ${command.name}`);
await this._executeCommand(invoker, response.options);
}
_parseResponse(invoker, response) {
const { command } = invoker;
if (response.error) { if (response.error) {
let content = invoker.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, { let content = invoker.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, {
option: response.option.name, min: response.option.minimum, max: response.option.maximum option: response.option.name, min: response.option.minimum, max: response.option.maximum
@ -81,13 +94,9 @@ class CommandHandler extends Observer {
emoji: 'failure', emoji: 'failure',
ephemeral: true ephemeral: true
}); });
} else if (command.showUsage && !Object.keys(response.options).length) { } else if (command.showUsage && response.options && !Object.keys(response.options).length || response.showUsage) {
return invoker.reply({ embeds: [command.usageEmbed(invoker)] }); return invoker.reply({ embeds: [command.usageEmbed(invoker, response.verbose)] });
} }
this.logger.info(`${interaction.user.tag} (${interaction.user.id}) is executing ${command.name}`);
await this._executeCommand(invoker, response.options);
} }
async _handleInhibitors(invoker) { async _handleInhibitors(invoker) {
@ -139,9 +148,10 @@ class CommandHandler extends Observer {
const { subcommand } = interaction; const { subcommand } = interaction;
if(!interaction.guild && command.guildOnly) { // Handled by inhibitors -- cleanup later
return interaction.reply({ index: 'O_COMMANDHANDLER_GUILDONLY', emoji: 'failure', ephemeral: true }); // if(!interaction.guild && command.guildOnly) {
} // return interaction.reply({ index: 'O_COMMANDHANDLER_GUILDONLY', emoji: 'failure', ephemeral: true });
// }
let error = null; let error = null;
const options = {}; const options = {};
@ -158,12 +168,13 @@ class CommandHandler extends Observer {
continue; continue;
} }
const newOption = new CommandOption({ // const newOption = new CommandOption({
name: matched.name, type: matched.type, // name: matched.name, type: matched.type,
minimum: matched.minimum, maximum: matched.maximum, // minimum: matched.minimum, maximum: matched.maximum,
_rawValue: option.value, // _rawValue: option.value,
dependsOn: matched.dependsOn, dependsOnMode: matched.dependsOnMode // dependsOn: matched.dependsOn, dependsOnMode: matched.dependsOnMode
}); // });
const newOption = matched.clone(option.value);
const parsed = await this._parseOption(interaction, newOption); const parsed = await this._parseOption(interaction, newOption);
if(parsed.error) { if(parsed.error) {
@ -200,8 +211,34 @@ class CommandHandler extends Observer {
async _parseMessage(invoker, params) { async _parseMessage(invoker, params) {
const { options } = invoker.command; const { command, target: message } = invoker;
console.log(options); const { subcommands, subcommandGroups } = command;
const args = {};
// console.log(options);
let group = null,
subcommand = null;
if (subcommandGroups.length || subcommands.length) {
const [first, second, ...rest] = params;
group = command.subcommandGroup(first);
// Depending on how thoroughly I want to support old style commands this might have to try and resolve to other options
// But for now I'm followin discord's structure for commands
if (!group) {
subcommand = command.subcommand(first)?.raw;
params = [second, ...rest];
} else {
subcommand = command.subcommand(second)?.raw;
params = rest;
}
message.subcommand = subcommand;
message.subcommandGroup = group;
if(!subcommand) return { showUsage: true, verbose: true };
}
return { options: { args, parameters: params }, verbose: true };
} }