From 26825ff4e58c8f0f0a14cb1a9d3862d806ac6716 Mon Sep 17 00:00:00 2001 From: jellywx Date: Fri, 18 Dec 2020 11:46:22 +0000 Subject: [PATCH] removed some duped code --- src/commands/info_cmds.rs | 40 ++------ src/commands/moderation_cmds.rs | 99 ++------------------ src/commands/reminder_cmds.rs | 159 ++++++-------------------------- src/commands/todo_cmds.rs | 29 +----- src/main.rs | 21 +++++ 5 files changed, 65 insertions(+), 283 deletions(-) diff --git a/src/commands/info_cmds.rs b/src/commands/info_cmds.rs index 2382845..110e318 100644 --- a/src/commands/info_cmds.rs +++ b/src/commands/info_cmds.rs @@ -6,6 +6,7 @@ use chrono::offset::Utc; use crate::{ consts::{DEFAULT_PREFIX, HELP_STRINGS}, + get_ctx_data, language_manager::LanguageManager, models::{GuildData, UserData}, SQLPool, THEME_COLOR, @@ -14,6 +15,7 @@ use crate::{ use levenshtein::levenshtein; use inflector::Inflector; +use std::sync::Arc; use std::time::{SystemTime, UNIX_EPOCH}; #[command] @@ -38,7 +40,7 @@ async fn help(ctx: &Context, msg: &Message, args: String) { async fn default_help( ctx: &Context, msg: &Message, - lm: &LanguageManager, + lm: Arc, prefix: &str, language: &str, ) { @@ -94,14 +96,7 @@ async fn help(ctx: &Context, msg: &Message, args: String) { .await; } - let data = ctx.data.read().await; - - let pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - let lm = data.get::().unwrap(); + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &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] async fn info(ctx: &Context, msg: &Message, _args: String) { - let data = ctx.data.read().await; - - let pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - let lm = data.get::().unwrap(); + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; 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] async fn donate(ctx: &Context, msg: &Message, _args: String) { - let data = ctx.data.read().await; - - let pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - let lm = data.get::().unwrap(); + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; let desc = lm.get(&language, "donate"); @@ -240,14 +221,7 @@ async fn dashboard(ctx: &Context, msg: &Message, _args: String) { #[command] async fn clock(ctx: &Context, msg: &Message, _args: String) { - let data = ctx.data.read().await; - - let pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - let lm = data.get::().unwrap(); + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; let timezone = UserData::timezone_of(&msg.author, &pool).await; diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 498545b..fd6dcad 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -17,6 +17,7 @@ use levenshtein::levenshtein; use crate::{ consts::{REGEX_ALIAS, REGEX_CHANNEL, REGEX_COMMANDS, REGEX_ROLE, THEME_COLOR}, framework::SendIterator, + get_ctx_data, language_manager::LanguageManager, models::{ChannelData, GuildData, UserData}, FrameworkCtx, SQLPool, @@ -29,19 +30,7 @@ use std::{collections::HashMap, iter, time::Duration}; #[permission_level(Restricted)] #[can_blacklist(false)] async fn blacklist(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; @@ -97,19 +86,7 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) { #[command] async fn timezone(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; 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] async fn change_meridian(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; 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] async fn language(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; 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)] #[permission_level(Restricted)] async fn prefix(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) .await @@ -478,19 +419,7 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) { #[supports_dm(false)] #[permission_level(Restricted)] async fn restrict(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) @@ -631,19 +560,7 @@ WHERE #[supports_dm(false)] #[permission_level(Managed)] async fn alias(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 09cccd1..622ecd0 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -23,6 +23,7 @@ use crate::{ REGEX_REMIND_COMMAND, THEME_COLOR, }, framework::SendIterator, + get_ctx_data, language_manager::LanguageManager, models::{ChannelData, GuildData, Timer, UserData}, time_parser::TimeParser, @@ -109,19 +110,7 @@ async fn create_webhook( #[supports_dm(false)] #[permission_level(Restricted)] async fn pause(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_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] #[permission_level(Restricted)] async fn offset(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; 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] #[permission_level(Restricted)] async fn nudge(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; let timezone = UserData::timezone_of(&msg.author, &pool).await; @@ -411,19 +376,7 @@ impl LookReminder { #[command("look")] #[permission_level(Managed)] async fn look(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let language = UserData::language_of(&msg.author, &pool).await; let timezone = UserData::timezone_of(&msg.author, &pool).await; @@ -550,19 +503,7 @@ LIMIT #[command("del")] #[permission_level(Managed)] async fn delete(ctx: &Context, msg: &Message, _args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; 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) } - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).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 { + 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::().unwrap(); + + if pref == "#" { + ReminderScope::Channel(id) + } else { + ReminderScope::User(id) + } + }) + .collect::>() +} + async fn remind_command(ctx: &Context, msg: &Message, args: String, command: RemindCommand) { - fn parse_mention_list(mentions: &str) -> Vec { - 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::().unwrap(); - - if pref == "#" { - ReminderScope::Channel(id) - } else { - ReminderScope::User(id) - } - }) - .collect::>() - } - - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; 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")] #[permission_level(Managed)] async fn natural(ctx: &Context, msg: &Message, args: String) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let now = SystemTime::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(); - location_ids = 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::().unwrap(); - - if pref == "#" { - ReminderScope::Channel(id) - } else { - ReminderScope::User(id) - } - }) - .collect::>(); + location_ids = parse_mention_list(mentions); } } diff --git a/src/commands/todo_cmds.rs b/src/commands/todo_cmds.rs index a3061b1..783a000 100644 --- a/src/commands/todo_cmds.rs +++ b/src/commands/todo_cmds.rs @@ -13,6 +13,7 @@ use std::fmt; use crate::{ consts::THEME_COLOR, + get_ctx_data, models::{GuildData, UserData}, 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) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); 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) { - let pool; - let lm; - - { - let data = ctx.data.read().await; - - pool = data - .get::() - .cloned() - .expect("Could not get SQLPool from data"); - - lm = data.get::().cloned().unwrap(); - } + let (pool, lm) = get_ctx_data(&ctx).await; let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; diff --git a/src/main.rs b/src/main.rs index 039f6cd..f79463b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -322,3 +322,24 @@ pub async fn check_subscription_on_message( false } } + +pub async fn get_ctx_data(ctx: &&Context) -> (MySqlPool, Arc) { + let pool; + let lm; + + { + let data = ctx.data.read().await; + + pool = data + .get::() + .cloned() + .expect("Could not get SQLPool"); + + lm = data + .get::() + .cloned() + .expect("Could not get LanguageManager"); + } + + (pool, lm) +}