cleared up imports, split command help into separate function, postponed some awaits

This commit is contained in:
jellywx 2020-12-18 12:37:05 +00:00
parent 26825ff4e5
commit ca68b45ebe
5 changed files with 49 additions and 45 deletions

View File

@ -5,16 +5,14 @@ use serenity::{client::Context, model::channel::Message};
use chrono::offset::Utc; use chrono::offset::Utc;
use crate::{ use crate::{
command_help,
consts::{DEFAULT_PREFIX, HELP_STRINGS}, consts::{DEFAULT_PREFIX, HELP_STRINGS},
get_ctx_data, get_ctx_data,
language_manager::LanguageManager, language_manager::LanguageManager,
models::{GuildData, UserData}, models::{GuildData, UserData},
SQLPool, THEME_COLOR, THEME_COLOR,
}; };
use levenshtein::levenshtein;
use inflector::Inflector;
use std::sync::Arc; use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
@ -98,43 +96,24 @@ async fn help(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await; let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool);
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool);
if !args.is_empty() { if !args.is_empty() {
let closest_match = HELP_STRINGS let matched = HELP_STRINGS
.iter() .iter()
.map(|h| (levenshtein(h.split_at(5).1, &args), h)) .filter(|h| h.split_at(5).1 == args)
.filter(|(dist, _)| dist < &3) .next();
.min_by(|(dist_a, _), (dist_b, _)| dist_a.cmp(dist_b))
.map(|(_, string)| string);
if let Some(help_str) = closest_match { if let Some(help_str) = matched {
let desc = lm.get(&language, help_str);
let command_name = help_str.split_at(5).1; let command_name = help_str.split_at(5).1;
let _ = msg command_help(ctx, msg, lm, &prefix.await, &language.await, command_name).await
.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 { } else {
default_help(ctx, msg, lm, &prefix, &language).await; default_help(ctx, msg, lm, &prefix.await, &language.await).await;
} }
} else { } else {
default_help(ctx, msg, lm, &prefix, &language).await; default_help(ctx, msg, lm, &prefix.await, &language.await).await;
} }
} }
@ -142,16 +121,15 @@ async fn help(ctx: &Context, msg: &Message, args: String) {
async fn info(ctx: &Context, msg: &Message, _args: String) { async fn info(ctx: &Context, msg: &Message, _args: String) {
let (pool, lm) = get_ctx_data(&ctx).await; let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool);
let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let prefix = GuildData::prefix_from_id(msg.guild_id, &pool);
.await let current_user = ctx.cache.current_user();
.unwrap();
let desc = lm let desc = lm
.get(&language, "info") .get(&language.await, "info")
.replacen("{user}", &ctx.cache.current_user().await.name, 1) .replacen("{user}", &current_user.await.name, 1)
.replace("{default_prefix}", &*DEFAULT_PREFIX) .replace("{default_prefix}", &*DEFAULT_PREFIX)
.replace("{prefix}", &guild_data.prefix); .replace("{prefix}", &prefix.await);
let _ = msg let _ = msg
.channel_id .channel_id

View File

@ -18,9 +18,8 @@ use crate::{
consts::{REGEX_ALIAS, REGEX_CHANNEL, REGEX_COMMANDS, REGEX_ROLE, THEME_COLOR}, consts::{REGEX_ALIAS, REGEX_CHANNEL, REGEX_COMMANDS, REGEX_ROLE, THEME_COLOR},
framework::SendIterator, framework::SendIterator,
get_ctx_data, get_ctx_data,
language_manager::LanguageManager,
models::{ChannelData, GuildData, UserData}, models::{ChannelData, GuildData, UserData},
FrameworkCtx, SQLPool, FrameworkCtx,
}; };
use std::{collections::HashMap, iter, time::Duration}; use std::{collections::HashMap, iter, time::Duration};

View File

@ -24,10 +24,8 @@ use crate::{
}, },
framework::SendIterator, framework::SendIterator,
get_ctx_data, get_ctx_data,
language_manager::LanguageManager,
models::{ChannelData, GuildData, Timer, UserData}, models::{ChannelData, GuildData, Timer, UserData},
time_parser::TimeParser, time_parser::TimeParser,
SQLPool,
}; };
use chrono::{offset::TimeZone, NaiveDateTime}; use chrono::{offset::TimeZone, NaiveDateTime};

View File

@ -15,12 +15,10 @@ use crate::{
consts::THEME_COLOR, consts::THEME_COLOR,
get_ctx_data, get_ctx_data,
models::{GuildData, UserData}, models::{GuildData, UserData},
SQLPool,
}; };
use sqlx::MySqlPool; use sqlx::MySqlPool;
use std::convert::TryFrom; use std::convert::TryFrom;
use crate::language_manager::LanguageManager;
use async_trait::async_trait; use async_trait::async_trait;
#[derive(Debug)] #[derive(Debug)]

View File

@ -39,6 +39,7 @@ use crate::{
use serenity::futures::TryFutureExt; use serenity::futures::TryFutureExt;
use inflector::Inflector;
use log::info; use log::info;
struct SQLPool; struct SQLPool;
@ -343,3 +344,33 @@ pub async fn get_ctx_data(ctx: &&Context) -> (MySqlPool, Arc<LanguageManager>) {
(pool, lm) (pool, lm)
} }
async fn command_help(
ctx: &Context,
msg: &Message,
lm: Arc<LanguageManager>,
prefix: &str,
language: &str,
command_name: &str,
) {
let _ = msg
.channel_id
.send_message(ctx, |m| {
m.embed(move |e| {
e.title(format!("{} Help", command_name.to_title_case()))
.description(
lm.get(&language, &format!("help/{}", command_name))
.replace("{prefix}", &prefix),
)
.footer(|f| {
f.text(concat!(
env!("CARGO_PKG_NAME"),
" ver ",
env!("CARGO_PKG_VERSION")
))
})
.color(*THEME_COLOR)
})
})
.await;
}