help command rework started

This commit is contained in:
jellywx 2020-11-30 16:06:05 +00:00
parent d8aaf92335
commit 268fcab8c5
4 changed files with 125 additions and 30 deletions

7
Cargo.lock generated
View File

@ -292,12 +292,6 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "custom_error"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51ac5e99a7fea3ee8a03fa4721a47e2efd3fbb38358fc61192a54d4c6f866c12"
[[package]]
name = "digest"
version = "0.9.0"
@ -1348,7 +1342,6 @@ dependencies = [
"async-trait",
"chrono",
"chrono-tz",
"custom_error",
"dotenv",
"env_logger",
"lazy_static",

View File

@ -18,7 +18,6 @@ chrono-tz = "0.5"
lazy_static = "1.4"
num-integer = "0.1"
num-traits = "0.2"
custom_error = "1.7"
serde = "1.0"
serde_json = "1.0"
rand = "0.7"

View File

@ -5,12 +5,15 @@ use serenity::{client::Context, model::channel::Message};
use chrono::offset::Utc;
use crate::{
consts::DEFAULT_PREFIX,
consts::{DEFAULT_PREFIX, HELP_STRINGS},
language_manager::LanguageManager,
models::{GuildData, UserData},
SQLPool, THEME_COLOR,
};
use crate::language_manager::LanguageManager;
use levenshtein::levenshtein;
use inflector::Inflector;
use std::time::{SystemTime, UNIX_EPOCH};
#[command]
@ -31,19 +34,15 @@ async fn ping(ctx: &Context, msg: &Message, _args: String) {
#[command]
#[can_blacklist(false)]
async fn help(ctx: &Context, msg: &Message, _args: String) {
let data = ctx.data.read().await;
let pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
let lm = data.get::<LanguageManager>().unwrap();
let language = UserData::language_of(&msg.author, &pool).await;
let desc = lm.get(&language, "help");
async fn help(ctx: &Context, msg: &Message, args: String) {
async fn default_help(
ctx: &Context,
msg: &Message,
lm: &LanguageManager,
prefix: &str,
language: &str,
) {
let desc = lm.get(language, "help/desc").replace("{prefix}", prefix);
let _ = msg
.channel_id
@ -51,6 +50,37 @@ async fn help(ctx: &Context, msg: &Message, _args: String) {
m.embed(move |e| {
e.title("Help Menu")
.description(desc)
.field(
lm.get(language, "help/setup_title"),
"`lang` `timezone`",
true,
)
.field(
lm.get(language, "help/mod_title"),
"`prefix` `blacklist` `restrict` `alias`",
true,
)
.field(
lm.get(language, "help/reminder_title"),
"`remind` `interval` `natural` `look`",
true,
)
.field(
lm.get(language, "help/reminder_mod_title"),
"`del` `offset` `pause` `nudge`",
true,
)
.field(
lm.get(language, "help/info_title"),
"`help` `info` `donate` `clock`",
true,
)
.field(
lm.get(language, "help/todo_title"),
"`todo` `todos` `todoc`",
true,
)
.field(lm.get(language, "help/other_title"), "`timer`", true)
.footer(|f| {
f.text(concat!(
env!("CARGO_PKG_NAME"),
@ -62,6 +92,55 @@ async fn help(ctx: &Context, msg: &Message, _args: String) {
})
})
.await;
}
let data = ctx.data.read().await;
let pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
let lm = data.get::<LanguageManager>().unwrap();
let language = UserData::language_of(&msg.author, &pool).await;
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;
if !args.is_empty() {
let closest_match = HELP_STRINGS
.iter()
.map(|h| (levenshtein(h.split_at(5).1, &args), h))
.filter(|(dist, _)| dist < &3)
.min_by(|(dist_a, _), (dist_b, _)| dist_a.cmp(dist_b))
.map(|(_, string)| string);
if let Some(help_str) = closest_match {
let desc = lm.get(&language, help_str);
let command_name = help_str.split_at(5).1;
let _ = msg
.channel_id
.send_message(ctx, |m| {
m.embed(move |e| {
e.title(format!("{} Help", command_name.to_title_case()))
.description(desc.replace("{prefix}", &prefix))
.footer(|f| {
f.text(concat!(
env!("CARGO_PKG_NAME"),
" ver ",
env!("CARGO_PKG_VERSION")
))
})
.color(*THEME_COLOR)
})
})
.await;
} else {
default_help(ctx, msg, lm, &prefix, &language).await;
}
} else {
default_help(ctx, msg, lm, &prefix, &language).await;
}
}
#[command]

View File

@ -1,6 +1,29 @@
pub const DAY: u64 = 86_400;
pub const HOUR: u64 = 3_600;
pub const MINUTE: u64 = 60;
pub const HELP_STRINGS: [&'static str; 21] = [
"help/lang",
"help/timezone",
"help/prefix",
"help/blacklist",
"help/restrict",
"help/alias",
"help/remind",
"help/interval",
"help/natural",
"help/look",
"help/del",
"help/offset",
"help/pause",
"help/nudge",
"help/info",
"help/help",
"help/donate",
"help/clock",
"help/todo",
"help/todos",
"help/todoc",
];
pub const CHARACTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
@ -40,6 +63,7 @@ lazy_static! {
.collect::<Vec<u64>>())
.unwrap_or_else(|_| vec![])
);
pub static ref CNC_GUILD: Option<u64> = env::var("CNC_GUILD")
.map(|var| var.parse::<u64>().ok())
.ok()