Callan Piper
Callan Piper

Reputation: 1

How can I make a Discord Bot that finds the closest response to the command you entered

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 :)

Upvotes: 0

Views: 1419

Answers (1)

Mr.Best
Mr.Best

Reputation: 1

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.

Upvotes: 0

Related Questions