Skip to content

Commit

Permalink
refactor: Make calculateCommandSuggestion() a CommandHandler prototype
Browse files Browse the repository at this point in the history
This helper is used in the native CommandHandler function runCommand() instead of in a command. It therefore makes sense to link it directly to the CommandHandler itself.
  • Loading branch information
3urobeat committed Jan 12, 2025
1 parent e91c731 commit be319c4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
20 changes: 16 additions & 4 deletions src/commands/commandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Created Date: 2023-04-01 21:54:21
* Author: 3urobeat
*
* Last Modified: 2024-12-24 13:26:21
* Last Modified: 2025-01-12 11:41:44
* Modified By: 3urobeat
*
* Copyright (c) 2023 - 2024 3urobeat <https://github.com/3urobeat>
* Copyright (c) 2023 - 2025 3urobeat <https://github.com/3urobeat>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Expand All @@ -18,7 +18,6 @@
const fs = require("fs");

const Controller = require("../controller/controller.js"); // eslint-disable-line
const { calculateCommandSuggestions } = require("./helpers/calculateSuggestion.js");


/**
Expand Down Expand Up @@ -58,6 +57,9 @@ const CommandHandler = function(controller) {
*/
this.commands = [];

// Load CommandHandler helper to calculate suggestions for runCommand()
require("./helpers/calculateSuggestion.js");

};


Expand Down Expand Up @@ -201,7 +203,7 @@ CommandHandler.prototype.runCommand = async function(name, args, respondModule,

if (!thisCmd) {
// Calculate a command suggestion from user input
const suggestions = calculateCommandSuggestions(this, name);
const suggestions = this.calculateCommandSuggestions(name);

logger("warn", `CommandHandler runCommand(): Command '${name}' was not found! Suggesting user command '${suggestions[0].name}' with a similarity of ${suggestions[0].closeness}%`);

Expand Down Expand Up @@ -273,3 +275,13 @@ CommandHandler.prototype.reloadCommands = function() {


module.exports = CommandHandler;


/* -------- Register functions to let the IntelliSense know what's going on in helper files -------- */

/**
* Calculates command suggestions using the Jaro Winkler distance of `input` to all registered commands
* @param {string} input String to get the nearest registered commands of
* @returns {{ name: string, closeness: number }[]} Returns a sorted Array of Objects, containing the command name and closeness in percent of name to `input` of every registered command
*/
CommandHandler.prototype.calculateCommandSuggestions = function(input) {}; // eslint-disable-line
25 changes: 16 additions & 9 deletions src/commands/helpers/calculateSuggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@
* Created Date: 2024-12-23 14:10:58
* Author: 3urobeat
*
* Last Modified: 2024-12-23 16:48:52
* Last Modified: 2025-01-12 11:41:33
* Modified By: 3urobeat
*
* Copyright (c) 2024 3urobeat <https://github.com/3urobeat>
* Copyright (c) 2024 - 2025 3urobeat <https://github.com/3urobeat>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @module CommandHandler
*/

const CommandHandler = require("../commandHandler.js"); // eslint-disable-line
const CommandHandler = require("../commandHandler.js");


// Credit: https://sumn2u.medium.com/string-similarity-comparision-in-js-with-examples-4bae35f13968 & https://gist.github.com/sumn2u/0e0b5d9505ad096284928a987ace13fb#file-jaro-wrinker-js
/**
* Calculate JaroWinkler distance between two inputs. Credit: https://sumn2u.medium.com/string-similarity-comparision-in-js-with-examples-4bae35f13968 & https://gist.github.com/sumn2u/0e0b5d9505ad096284928a987ace13fb#file-jaro-wrinker-js
* @param {string} s1 First input
* @param {string} s2 Second input
* @returns {number} Returns closeness
*/
function jaroWinkler(s1, s2) {
let m = 0;

Expand Down Expand Up @@ -93,19 +101,18 @@ function jaroWinkler(s1, s2) {

/**
* Calculates command suggestions using the Jaro Winkler distance of `input` to all registered commands
* @param {CommandHandler} commandHandler The commandHandler object
* @param {string} input String to get the nearest registered commands of
* @returns {{ name: string, closeness: number }[]} Returns a sorted Array of Objects, containing the command name and closeness in percent of name to `input` of every registered command
*/
module.exports.calculateCommandSuggestions = (commandHandler, input) => {
CommandHandler.prototype.calculateCommandSuggestions = function(input) {
const result = [];

// Loop through all registered commands
for (let i = 0; i < commandHandler.commands.length; i++) {
for (let i = 0; i < this.commands.length; i++) {
// Loop through all names of this command
for (let j = 0; j < commandHandler.commands[i].names.length; j++) {
for (let j = 0; j < this.commands[i].names.length; j++) {

const thisCommandName = commandHandler.commands[i].names[j];
const thisCommandName = this.commands[i].names[j];

result.push({
name: thisCommandName,
Expand Down
4 changes: 2 additions & 2 deletions src/data/fileStructure.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
{
"path": "src/commands/commandHandler.js",
"url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/commands/commandHandler.js",
"checksum": "dffe80a82f8dbaf4d0419ea642b4d5cd"
"checksum": "8c81b4ec3839f2f006c994312e37e065"
},
{
"path": "src/commands/core/block.js",
Expand Down Expand Up @@ -213,7 +213,7 @@
{
"path": "src/commands/helpers/calculateSuggestion.js",
"url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/commands/helpers/calculateSuggestion.js",
"checksum": "a0bec355ce4fe618f9d4893593f1364e"
"checksum": "cdbd3e11fbb8c2b85ec453704ba00fb9"
},
{
"path": "src/commands/helpers/getCommentArgs.js",
Expand Down

0 comments on commit be319c4

Please sign in to comment.