From 67913985886b465a977925c7bb0af37a7dd4091a Mon Sep 17 00:00:00 2001 From: Peery Date: Wed, 18 Oct 2017 00:01:15 +0200 Subject: [PATCH] Fixed handling of non-command messages - fixes #17 Added if's when the regex for command detection returns null and notify the caller via either a failed promise or a null return. --- commands.js | 8 ++++++++ index.js | 13 +++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/commands.js b/commands.js index eb29e3c..1d19a8e 100644 --- a/commands.js +++ b/commands.js @@ -60,6 +60,10 @@ function suggestFix(msg, projects){ const shortcuts = msg.match(msgRegex); const expansion = {}; + if(shortcuts === null){ + return null; + } + expansion.project = findShortcut(shortcuts[1], projects); if(expansion.project) { expansion.project = expansion.project.toLowerCase(); @@ -110,6 +114,10 @@ commands.expandCommand = (msg, projects) => { const shortcuts = msg.match(msgRegex); const expansion = {}; + if(shortcuts === null){ + reject('No regex match.'); + return; + } expansion.project = findShortcut(shortcuts[1], projects); if(expansion.project) { expansion.project = expansion.project.toLowerCase(); diff --git a/index.js b/index.js index 3d28ab3..3236200 100644 --- a/index.js +++ b/index.js @@ -50,13 +50,14 @@ matrixClient.on('Room.timeline', function(event, room, resetTimeline) { subcommand.exec(args, room, event); }, (fail) => { // command not found let tmp = commands.suggestFix(body, commands.projects); - let suggestions = ""; - for(let i = 0; i < tmp.length; i++){ - suggestions += "\n"+tmp[i]; + if(tmp !== null){ //when the regex matches nothing + let suggestions = ""; + for(let i = 0; i < tmp.length; i++){ + suggestions += "\n"+tmp[i]; + } + matrixClient.sendNotice(room.roomId, 'Unrecognized command.\n' + + 'Did you mean:'+suggestions); } - matrixClient.sendNotice(room.roomId, 'Unrecognized command.\n' + - 'Did you mean:'+suggestions); - }); });