Add autocorrect
This commit is contained in:
parent
dbf05365a9
commit
c3b66b8cb2
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -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"
|
||||
|
@ -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"]
|
||||
|
16
src/main.rs
16
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;
|
||||
@ -177,9 +179,23 @@ impl Handler {
|
||||
}
|
||||
} else if command.starts_with("prefix") {
|
||||
wc_prefix(command, msg)?;
|
||||
} else {
|
||||
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(());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user