removed some duped code

This commit is contained in:
jellywx 2020-12-18 11:46:22 +00:00
parent fd4acc4b9d
commit 26825ff4e5
5 changed files with 65 additions and 283 deletions

View File

@ -6,6 +6,7 @@ use chrono::offset::Utc;
use crate::{ use crate::{
consts::{DEFAULT_PREFIX, HELP_STRINGS}, consts::{DEFAULT_PREFIX, HELP_STRINGS},
get_ctx_data,
language_manager::LanguageManager, language_manager::LanguageManager,
models::{GuildData, UserData}, models::{GuildData, UserData},
SQLPool, THEME_COLOR, SQLPool, THEME_COLOR,
@ -14,6 +15,7 @@ use crate::{
use levenshtein::levenshtein; use levenshtein::levenshtein;
use inflector::Inflector; use inflector::Inflector;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
#[command] #[command]
@ -38,7 +40,7 @@ async fn help(ctx: &Context, msg: &Message, args: String) {
async fn default_help( async fn default_help(
ctx: &Context, ctx: &Context,
msg: &Message, msg: &Message,
lm: &LanguageManager, lm: Arc<LanguageManager>,
prefix: &str, prefix: &str,
language: &str, language: &str,
) { ) {
@ -94,14 +96,7 @@ async fn help(ctx: &Context, msg: &Message, args: String) {
.await; .await;
} }
let data = ctx.data.read().await; let (pool, lm) = get_ctx_data(&ctx).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 language = UserData::language_of(&msg.author, &pool).await;
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;
@ -145,14 +140,7 @@ async fn help(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
async fn info(ctx: &Context, msg: &Message, _args: String) { async fn info(ctx: &Context, msg: &Message, _args: String) {
let data = ctx.data.read().await; let (pool, lm) = get_ctx_data(&ctx).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 language = UserData::language_of(&msg.author, &pool).await;
let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
@ -186,14 +174,7 @@ async fn info(ctx: &Context, msg: &Message, _args: String) {
#[command] #[command]
async fn donate(ctx: &Context, msg: &Message, _args: String) { async fn donate(ctx: &Context, msg: &Message, _args: String) {
let data = ctx.data.read().await; let (pool, lm) = get_ctx_data(&ctx).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 language = UserData::language_of(&msg.author, &pool).await;
let desc = lm.get(&language, "donate"); let desc = lm.get(&language, "donate");
@ -240,14 +221,7 @@ async fn dashboard(ctx: &Context, msg: &Message, _args: String) {
#[command] #[command]
async fn clock(ctx: &Context, msg: &Message, _args: String) { async fn clock(ctx: &Context, msg: &Message, _args: String) {
let data = ctx.data.read().await; let (pool, lm) = get_ctx_data(&ctx).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 language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await; let timezone = UserData::timezone_of(&msg.author, &pool).await;

View File

@ -17,6 +17,7 @@ use levenshtein::levenshtein;
use crate::{ 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,
language_manager::LanguageManager, language_manager::LanguageManager,
models::{ChannelData, GuildData, UserData}, models::{ChannelData, GuildData, UserData},
FrameworkCtx, SQLPool, FrameworkCtx, SQLPool,
@ -29,19 +30,7 @@ use std::{collections::HashMap, iter, time::Duration};
#[permission_level(Restricted)] #[permission_level(Restricted)]
#[can_blacklist(false)] #[can_blacklist(false)]
async fn blacklist(ctx: &Context, msg: &Message, args: String) { async fn blacklist(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
@ -97,19 +86,7 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
async fn timezone(ctx: &Context, msg: &Message, args: String) { async fn timezone(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -227,19 +204,7 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
async fn change_meridian(ctx: &Context, msg: &Message, args: String) { async fn change_meridian(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -292,19 +257,7 @@ async fn change_meridian(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
async fn language(ctx: &Context, msg: &Message, args: String) { async fn language(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -433,19 +386,7 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn prefix(ctx: &Context, msg: &Message, args: String) { async fn prefix(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
.await .await
@ -478,19 +419,7 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) {
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn restrict(ctx: &Context, msg: &Message, args: String) { async fn restrict(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
@ -631,19 +560,7 @@ WHERE
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn alias(ctx: &Context, msg: &Message, args: String) { async fn alias(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;

View File

@ -23,6 +23,7 @@ use crate::{
REGEX_REMIND_COMMAND, THEME_COLOR, REGEX_REMIND_COMMAND, THEME_COLOR,
}, },
framework::SendIterator, framework::SendIterator,
get_ctx_data,
language_manager::LanguageManager, language_manager::LanguageManager,
models::{ChannelData, GuildData, Timer, UserData}, models::{ChannelData, GuildData, Timer, UserData},
time_parser::TimeParser, time_parser::TimeParser,
@ -109,19 +110,7 @@ async fn create_webhook(
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn pause(ctx: &Context, msg: &Message, args: String) { async fn pause(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await; let timezone = UserData::timezone_of(&msg.author, &pool).await;
@ -185,19 +174,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn offset(ctx: &Context, msg: &Message, args: String) { async fn offset(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -269,19 +246,7 @@ UPDATE reminders SET `time` = `time` + ? WHERE reminders.channel_id = ?
#[command] #[command]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn nudge(ctx: &Context, msg: &Message, args: String) { async fn nudge(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await; let timezone = UserData::timezone_of(&msg.author, &pool).await;
@ -411,19 +376,7 @@ impl LookReminder {
#[command("look")] #[command("look")]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn look(ctx: &Context, msg: &Message, args: String) { async fn look(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await; let timezone = UserData::timezone_of(&msg.author, &pool).await;
@ -550,19 +503,7 @@ LIMIT
#[command("del")] #[command("del")]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn delete(ctx: &Context, msg: &Message, _args: String) { async fn delete(ctx: &Context, msg: &Message, _args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -745,19 +686,7 @@ async fn timer(ctx: &Context, msg: &Message, args: String) {
format!("{:02}:{:02}:{:02}", hours, minutes, seconds) format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
} }
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
@ -1077,36 +1006,24 @@ async fn interval(ctx: &Context, msg: &Message, args: String) {
} }
} }
fn parse_mention_list(mentions: &str) -> Vec<ReminderScope> {
REGEX_CHANNEL_USER
.captures_iter(mentions)
.map(|i| {
let pref = i.get(1).unwrap().as_str();
let id = i.get(2).unwrap().as_str().parse::<u64>().unwrap();
if pref == "#" {
ReminderScope::Channel(id)
} else {
ReminderScope::User(id)
}
})
.collect::<Vec<ReminderScope>>()
}
async fn remind_command(ctx: &Context, msg: &Message, args: String, command: RemindCommand) { async fn remind_command(ctx: &Context, msg: &Message, args: String, command: RemindCommand) {
fn parse_mention_list(mentions: &str) -> Vec<ReminderScope> { let (pool, lm) = get_ctx_data(&ctx).await;
REGEX_CHANNEL_USER
.captures_iter(mentions)
.map(|i| {
let pref = i.get(1).unwrap().as_str();
let id = i.get(2).unwrap().as_str().parse::<u64>().unwrap();
if pref == "#" {
ReminderScope::Channel(id)
} else {
ReminderScope::User(id)
}
})
.collect::<Vec<ReminderScope>>()
}
let pool;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -1318,19 +1235,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
#[command("natural")] #[command("natural")]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn natural(ctx: &Context, msg: &Message, args: String) { async fn natural(ctx: &Context, msg: &Message, args: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let now = SystemTime::now(); let now = SystemTime::now();
let since_epoch = now let since_epoch = now
@ -1384,19 +1289,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
let mentions = captures.name("mentions").unwrap().as_str(); let mentions = captures.name("mentions").unwrap().as_str();
location_ids = REGEX_CHANNEL_USER location_ids = parse_mention_list(mentions);
.captures_iter(mentions)
.map(|i| {
let pref = i.get(1).unwrap().as_str();
let id = i.get(2).unwrap().as_str().parse::<u64>().unwrap();
if pref == "#" {
ReminderScope::Channel(id)
} else {
ReminderScope::User(id)
}
})
.collect::<Vec<ReminderScope>>();
} }
} }

View File

@ -13,6 +13,7 @@ use std::fmt;
use crate::{ use crate::{
consts::THEME_COLOR, consts::THEME_COLOR,
get_ctx_data,
models::{GuildData, UserData}, models::{GuildData, UserData},
SQLPool, SQLPool,
}; };
@ -237,19 +238,7 @@ DELETE FROM todos WHERE user_id = (SELECT id FROM users WHERE user = ?) AND guil
} }
async fn execute(&self, ctx: &Context, msg: &Message, subcommand: SubCommand, extra: String) { async fn execute(&self, ctx: &Context, msg: &Message, subcommand: SubCommand, extra: String) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;
@ -442,19 +431,7 @@ async fn todo_guild(ctx: &Context, msg: &Message, args: String) {
} }
async fn show_help(ctx: &Context, msg: &Message, target: Option<TodoTarget>) { async fn show_help(ctx: &Context, msg: &Message, target: Option<TodoTarget>) {
let pool; let (pool, lm) = get_ctx_data(&ctx).await;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;

View File

@ -322,3 +322,24 @@ pub async fn check_subscription_on_message(
false false
} }
} }
pub async fn get_ctx_data(ctx: &&Context) -> (MySqlPool, Arc<LanguageManager>) {
let pool;
let lm;
{
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool");
lm = data
.get::<LanguageManager>()
.cloned()
.expect("Could not get LanguageManager");
}
(pool, lm)
}