Skip to content

Fuzzy Command Suggestion

Mihou edited this page Jul 2, 2021 · 1 revision

🔍 Fuzzy Command Suggestion

As part of VelenUtils, the library supports fuzzy searching for a command. This is especially handy for cases like an help command where you have help [command] but the user enters in a typo and writes a non-existent command as an argument instead. Fuzzy Command Suggestion can help you guide the user to the potential command. An example would be:

// Some random commands.
VelenCommand.of("readbackwards", velen, (event, message, user, args1) -> message.reply("hello!")).attach();
VelenCommand.of("command", velen, (event, message, user, args1) -> message.reply("hello!")).attach();
VelenCommand.of("another", velen, (event, message, user, args1) -> message.reply("hello!")).attach();
VelenCommand.of("hello", velen, (event, message, user, args1) -> message.reply("hello!")).attach();

// The help command.
VelenCommand.of("help", velen, (event, message, user, args) -> {
  if (args.length > 0) {
    message.reply("Do you mean: " + VelenUtils.getCommandSuggestion(velen, args[0]));
  } else {
    message.reply("Help command: " + velen.getCommands().stream().map(VelenCommand::getName)
      .collect(Collectors.joining(", ")));
  }
}).attach();

The example below would output: command if you were to type: help com or would output: Help command: readbackwards, command, another, hello, help if you were to type: help.