command handler

This commit is contained in:
Erik 2022-04-23 01:16:06 +03:00
parent 241fb4616a
commit ffef8ce3da
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -18,6 +18,8 @@ class CommandHandler extends Observer {
['interactionCreate', this.interactionCreate.bind(this)] ['interactionCreate', this.interactionCreate.bind(this)]
]; ];
this._mentionPattern = null;
} }
async messageCreate(message) { async messageCreate(message) {
@ -31,16 +33,19 @@ class CommandHandler extends Observer {
if(!message.member) await message.guild.members.fetch(message.author.id); if(!message.member) await message.guild.members.fetch(message.author.id);
} }
const { command, parameters } = await this._getCommand(message); const messageWrapper = new MessageWrapper(this.client, message);
const { command, parameters } = await this._getCommand(messageWrapper);
if (!command) return undefined; if (!command) return undefined;
const invoker = new InvokerWrapper(this.client, new MessageWrapper(this.client, message), command); const invoker = new InvokerWrapper(this.client, messageWrapper, command);
const inhibitors = 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 arguments = await this._parseArguments(invoker, parameters); const options = await this._parseMessage(invoker, parameters);
this._executeCommand(invoker, {}); // Temp setup
//this._executeCommand(invoker, parameters);
} }
@ -130,9 +135,6 @@ class CommandHandler extends Observer {
} }
/**
* @param {InteractionWrapper} interaction
*/
async _parseInteraction(interaction, command) { async _parseInteraction(interaction, command) {
const { subcommand } = interaction; const { subcommand } = interaction;
@ -196,6 +198,13 @@ class CommandHandler extends Observer {
} }
async _parseMessage(invoker, params) {
const { options } = invoker.command;
console.log(options);
}
async _parseOption(interaction, option) { async _parseOption(interaction, option) {
const { guild } = interaction; const { guild } = interaction;
@ -304,19 +313,30 @@ class CommandHandler extends Observer {
} }
async _getCommand(message) { async _getCommand(message) {
if (!this._mentionPattern) this._mentionPattern = new RegExp(`^(<@!?${this.client.user.id}>)`, 'iu');
const [arg1, arg2, ...args] = message.content.split(' ');
//TODO: Move this somewhere else. RegExp should not be created every method call, but it requires the client user to be loaded. if (message.guild) await message.guild.settings();
const mentionPattern = new RegExp(`^(<@!?${this.client.user.id}>)`, 'iu'); const userWrapper = await this.client.getUserWrapper(message.author.id);
await userWrapper.settings();
const { prefix } = message;
let command = null, let command = null,
parameters = []; remains = [];
if(mentionPattern.test(message.content)) { if (arg1 && arg1.startsWith(prefix)) {
const [ , commandName, ...rest] = message.content.split(" "); const commandName = arg1.slice(prefix.length); //Grabs the command name by slicing off the prefix.
command = this._matchCommand(commandName); command = await this._matchCommand(commandName);
parameters = rest.join(" "); remains = [arg2, ...args];
message._caller = commandName;
} else if (arg1 && arg2 && arg1.startsWith('<@')) { //Checks if the first argument is a mention and if a command is after it.
if (arg2 && this._mentionPattern.test(arg1))
command = await this._matchCommand(arg2);
remains = args;
message._caller = arg2;
} }
return { command, parameters }; return { command, parameters: remains };
} }