From c3b66b8cb2e05f5c821b08c3bfa7c7b0fb3a4a68 Mon Sep 17 00:00:00 2001 From: Sakuhl <2012collector@gmail.com> Date: Fri, 16 Feb 2018 20:52:47 +0100 Subject: [PATCH] Add autocorrect --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 4e643cd..565f31b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -937,6 +937,11 @@ name = "stable_deref_trait" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "strsim" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "syn" version = "0.11.11" @@ -1227,6 +1232,7 @@ dependencies = [ "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "serenity 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "wynncraft 0.1.0 (git+https://gitlab.com/Sakuhl/wynncraft)", ] @@ -1339,6 +1345,7 @@ dependencies = [ "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" "checksum smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44db0ecb22921ef790d17ae13a3f6d15784183ff5f2a01aa32098c7498d2b4b9" "checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b" +"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd" diff --git a/Cargo.toml b/Cargo.toml index d8c1353..a5bc929 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ serde = "1.0.24" serde_derive = "1.0.24" serde_json = "1.0.8" serenity = "0.4.8" +strsim = "0.7.0" [dependencies.diesel] features = ["postgres"] diff --git a/src/main.rs b/src/main.rs index bcffcf5..ec36ed3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,8 @@ extern crate serde_derive; #[macro_use] extern crate lazy_static; +extern crate strsim; + use std::collections::HashMap; use std::env; use std::panic; @@ -178,7 +180,21 @@ impl Handler { } else if command.starts_with("prefix") { wc_prefix(command, msg)?; } else { - bail!(ResponseError::UnknownCommand { name: command.to_owned() }); + let commands = ["topguilds", "prefix", "warfeed", "unwarfeed", "livefeed", "unlivefeed", "status", "crop", "guild", "player", "territory", "info", "help"]; + let mut min = usize::max_value(); + let mut min_command = "help"; + for command_b in &commands { + let distance = strsim::levenshtein(command, command_b); + if distance <= min { + min = distance; + min_command = command_b; + } + } + if min < 25 { + msg.channel_id.say(&format!("Unknown command. Did you mean {:?}?", min_command))?; + } else { + bail!(ResponseError::UnknownCommand { name: command.to_owned() }); + } } return Ok(()); }