0

So I basically want my discord bot to respond with the closest response from a list, to what the user asked for.

I want to have a big list of all the possible responses that the bot can then scan through and find the closest match to what the user is asking for and send it to them.

So if I entered the command "/hep" it would automatically find the closest command which would be "/help".

I know there are tutorials that show you how to set this up in Java Scrip but I need help getting it to work with my Discord Bot

The bot works with discord.js

I'm fairly new to Discord Bots so any help would be awesome! (If I missed anything just let me know :)

  • like ?https://stackoverflow.com/questions/5859561/getting-the-closest-string-match – xdtTransform Dec 21 '18 at 07:04
  • 1
    Possible duplicate of [Getting the closest string match](https://stackoverflow.com/questions/5859561/getting-the-closest-string-match) – xdtTransform Dec 21 '18 at 07:04
  • @xdtTransform Yeah, But I'm very new to JS and I don't know how to get it to work with my bot, If you could make up a demo that would be awsome! – Callan Piper Dec 21 '18 at 07:09
  • Hi, I guess you have missed a step, I will recommend reading [tour], [ask], and [mcve]. This is not about js, or new. String metrics](https://en.wikipedia.org/wiki/String_metric) is not a build in Js function even if it was there will still be a tons of rules and precise and detail information missing. – xdtTransform Dec 21 '18 at 07:14
  • Sure, in the quest for matching close string with a list of command you have at least a dictionnary of command. That's a huge step in fiding close command but thats not quite enought. sure with a well build dictionary of command you will have less issue because all word can be really different. Avoing close word like (help, hell and hello). There is no coding magic, humain brain are good at guessing missing char and expected words. Here your have and unclear question, on a duplicate topics, with a too broad needs. That's 3 close reasons. – xdtTransform Dec 21 '18 at 07:21

1 Answers1

0

One idea that I have implemented in my bots that would prove to be reliable to your question would be using a fuzzy search mechanic within your message checking. I use http://fusejs.io/ library for my fuzzy searches. You will need to handle making an array of commands first. Example:

const Fuse = require('fuse.js');
const commandarray = ['help','ping','commands','etc'];

var options = {
  shouldSort: true,
  threshold: 0.6,
  location: 0,
  distance: 100,
  maxPatternLength: 32,
  minMatchCharLength: 2,
  keys: undefined
};

Then use the fuzzy search library to interact with your incoming messages that begin with your prefix and send them thru the fuzzy. Its response will be the closest match to your command. Meaning if you typed "!hep", the response from the fuzzy would be "help" and then you can proceed to interact with the sender by initiating the help command. Just make sure to only make it fuzzy search messages sent with a prefix first, dont let it search every message sent in a channel or it will do a command closest to every message on every word sent. something like:

const prefix = '!';
const fuse = new Fuse(commandarray, options);


client.on('message', message => {
if (message.content.startsWith(`${prefix}`)) {

const fuzzyresult = fuse.search(message);
(now fuzzyresult will return the index of the command thats in the array that is the closest match to the message sent on discord)

(now you grab your array of commands, input the index, and turn it into a string)
let cmdslice = commandarray.slice(fuzzyresult);
let cmdslice.length = 1;

let cmd = cmdslice.toString();


if (cmd === 'help') {
    do this function
} else if (cmd === 'ping') {
    do this function instead
} etc etc etc

}
});

This is a little messy but should help you achieve your request.

  • Thanks for much for the help! But for some reason, I still can't get it to work and I don't know what I'm doing wrong. Do you think you would be able to make a working demo that I can then download and customise to work with my bot? - Thanks, – Callan Piper Jan 26 '19 at 21:08