Skip to content

Commit

Permalink
chore: added comments and fixed various code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
NichArchA82 committed Jan 7, 2025
1 parent e164152 commit 94daea9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
2 changes: 1 addition & 1 deletion command-handler/src/cmd-handler/command-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class CommandHandler {
/*
call the init function that was destructured
from the command object. Since the init function
is async, we await the function to complete before
is async, we await for the function to complete before
continuing. This function is an optional function
that commands can use to run any initialization code
that the command depends on before being registered.
Expand Down
22 changes: 16 additions & 6 deletions command-handler/src/cmd-handler/run-button.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
/*
This file is responsible for running the button
That is associated with a command
*/

export default async ({
commandName,
actionId,
handler,
app,
body,
say,
commandName, // the name of the command
actionId, // the action ID of the button
handler, // the command handler
app, // the slack app
body, // the body of the event
say, // the say function to send a message
}) => {
// destructure the commands and command handler from the handler
const { commandHandler } = handler;
const { commands } = commandHandler;

//retrieve the command object from the commands map
const command = commands.get(commandName);

//create a response function to send a message
const response = (obj) => {
say(obj)
};

//check if the command does not exist, or has no button handler, if so return.
if (!command || !command.button) {
response({
text: `No button handler is registered.`
})
return
}

// run the command's button handler
command.button({ handler, app, body, response, actionId });
};
22 changes: 16 additions & 6 deletions command-handler/src/cmd-handler/run-command.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
/*
This file is responsible for running the command
*/

export default async ({
commandName,
handler,
app,
event,
args,
say
commandName, // the name of the command
handler, // the command handler
app, // the slack app
event, // the event object
args, // the command arguments
say // the say function to send a message
}) => {
// destructure the commands and command handler from the handler
const { commandHandler } = handler;
const { commands } = commandHandler;

//retrieve the command object from the commands map
const command = commands.get(commandName);

//check if the command does not exist, or has no run function, if so return.
if (!command || !command.run) {
return
}

// retrieve the text from the arguments joined by a space
const text = args.join(' ');

//create a response function to send a message
const response = (obj) => {
say(obj)
};

// run the command's run function
command.run({ handler, app, event, response, text, args });
};
18 changes: 17 additions & 1 deletion command-handler/src/events/button-click.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/*
This file is responsible for handling button click events.
*/

import runButton from "../cmd-handler/run-button.js";
import registeredButtons from '../cmd-handler/buttons.js';

export default function button(app, handler) {

// Register a single action handler for all actions
app.action(/button_/, async ({ body, ack, say }) => {
// Acknowledge the action so the app doesn't timeout
await ack();

// Extract the action ID from the event body
Expand All @@ -13,17 +18,28 @@ export default function button(app, handler) {
let commandName = null;
// Check for an exact match first
if (registeredButtons[actionId]) {
// set the command to handle the button to the matched button
commandName = registeredButtons[actionId].command;
} else {
// If no exact match, check for a regex pattern match
/*
Iterate all of the registered buttons to check for a regex match
destructuring the pattern and config from the registeredButtons object
*/
for (const [pattern, config] of Object.entries(registeredButtons)) {
/*
check if the registered button is a regex pattern and
if the pattern matches the action ID
*/
if (config.isRegex && new RegExp(pattern).test(actionId)) {
// set the command to handle the button to the matched button
commandName = config.command;
// break out of the loop if a match is found
break;
}
}
}

// call the runButton function to run the button handler
runButton({
commandName,
actionId,
Expand Down
31 changes: 29 additions & 2 deletions command-handler/src/events/legacy-command.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
/*
This file is responsible for handling legacy commands.
These commands are sent as messages and start with a prefix.
*/

import runCommand from '../cmd-handler/run-command.js';

export default function command(app, handler) {
const prefix = '!'
// Set the prefix for the commands
const prefix = '!';
/*
Register a single event handler for all messages
This event handler will listen for messages that
start with the prefix
*/
app.event('message', async ({ event, say }) => {

// get the content of the message
const content = event.text;
/*
if the content does not start with the prefix,
return as this is not a command
*/
if (!content.startsWith(prefix)) return;

/*
create an array of arguments by splitting
the content by spaces and removing the prefix
*/
const args = content.slice(prefix.length).trim().split(/ +/g);
// if there are no arguments, return.
// i.e. the message is just the prefix
if (args.length === 0) return;

/*
get the command name which is the first argument
in the args array, and convert it to lowercase
for case-insensitive matching
*/
const commandName = args.shift().toLowerCase();

// call the runCommand function to run the command
runCommand({
commandName,
handler,
Expand Down

0 comments on commit 94daea9

Please sign in to comment.