Skip to content
This repository was archived by the owner on Jul 17, 2020. It is now read-only.

Commit 2e778e3

Browse files
committed
Remove commands from the suggestion dictionary on forget
1 parent bff952e commit 2e778e3

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

source/bot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ bot.Command = function ( cmd ) {
357357
cmd.del = function () {
358358
bot.info.forgotten += 1;
359359
delete bot.commands[ cmd.name ];
360+
bot.commandDictionary.trie.del(cmd.name);
360361
};
361362

362363
return cmd;

source/suggestionDict.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ TrieNode.prototype.add = function( word ) {
2222
node.word = word;
2323
};
2424

25+
TrieNode.prototype.del = function(word, i) {
26+
i = i || 0;
27+
var node = this;
28+
var char = word[i++];
29+
30+
// recursively delete all trie nodes that are left empty after removing the command from the leaf
31+
if (node.children[char]) {
32+
node.children[char].del(word, i);
33+
if (Object.keys(node.children[char].children).length === 0 && node.children[char].word === null) {
34+
delete node.children[char];
35+
}
36+
}
37+
38+
if (node.word === word) {
39+
node.word = null;
40+
}
41+
}
42+
2543
//Having a small maxCost will increase performance greatly, experiment with
2644
//values of 1-3
2745
function SuggestionDictionary ( maxCost ) {

0 commit comments

Comments
 (0)