From 44d06e4298b41f8c634048d4972354354443f703 Mon Sep 17 00:00:00 2001 From: jellywx Date: Tue, 15 Dec 2020 23:03:41 +0000 Subject: [PATCH] silent mentioning for roles in reminders --- 1.3.0-changelog | 2 ++ src/commands/reminder_cmds.rs | 54 +++++++++++++++++++++++------------ src/consts.rs | 2 +- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/1.3.0-changelog b/1.3.0-changelog index 7949d5c..754e114 100644 --- a/1.3.0-changelog +++ b/1.3.0-changelog @@ -31,4 +31,6 @@ + Remind commands now provide output that is more detailed. This includes showing the exact errors that occurred for each reminder that couldn't be set, if bulk-setting reminders + Translations are now loaded from a JSON file included within the executable, which should be faster + Reduced user caching by individually querying some attributes like language and timezone + + Simplified some types and tried to remove some copying + + Roles can now be silently mentioned with <> diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 5632061..7575065 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -33,7 +33,7 @@ use chrono::{offset::TimeZone, NaiveDateTime}; use rand::{rngs::OsRng, seq::IteratorRandom}; -use sqlx::{encode::Encode, MySql, MySqlPool, Type}; +use sqlx::MySqlPool; use std::str::from_utf8; @@ -48,7 +48,9 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; -use regex::RegexBuilder; +use regex::{Captures, RegexBuilder}; +use serenity::cache::Cache; +use serenity::model::guild::Guild; fn shorthand_displacement(seconds: u64) -> String { let (days, seconds) = seconds.div_rem(&DAY); @@ -1397,31 +1399,47 @@ async fn natural(ctx: &Context, msg: &Message, args: String) { } } -async fn create_reminder< - 'a, - U: Into, - T: TryInto, - S: ToString + Type + Encode<'a, MySql>, ->( - ctx: impl CacheHttp, +fn substitute_content(guild: Option, content: &str) -> String { + if let Some(guild) = guild { + REGEX_CONTENT_SUBSTITUTION + .replace(content, |caps: &Captures| { + if let Some(user) = caps.name("user") { + format!("<@{}>", user.as_str()) + } else if let Some(role_name) = caps.name("role") { + if let Some(role) = guild.role_by_name(role_name.as_str()) { + role.mention() + } else { + role_name.as_str().to_string() + } + } else { + String::new() + } + }) + .to_string() + } else { + content.to_string() + } + .replace("<>", "@everyone") + .replace("<>", "@here") +} + +async fn create_reminder<'a, U: Into, T: TryInto>( + ctx: impl CacheHttp + AsRef, pool: &MySqlPool, user_id: U, guild_id: Option, scope_id: &ReminderScope, time_parser: T, interval: Option, - content: S, + content: &str, ) -> Result<(), ReminderError> { let user_id = user_id.into(); - let mut content_string = content.to_string(); - - // substitution filters - content_string = content_string.replace("<>", "@everyone"); - content_string = content_string.replace("<>", "@here"); - content_string = REGEX_CONTENT_SUBSTITUTION - .replace(&content_string, "<@$1>") - .to_string(); + let content_string = if let Some(g_id) = guild_id { + substitute_content(g_id.to_guild_cached(&ctx).await, content) + } else { + content.to_string() + }; let mut nudge = 0; diff --git a/src/consts.rs b/src/consts.rs index e3a000a..11e3ee5 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -44,7 +44,7 @@ lazy_static! { pub static ref REGEX_ALIAS: Regex = Regex::new(r#"(?P[\S]{1,12})(?:(?: (?P.*)$)|$)"#).unwrap(); - pub static ref REGEX_CONTENT_SUBSTITUTION: Regex = Regex::new(r#"<<(\d+)>>"#).unwrap(); + pub static ref REGEX_CONTENT_SUBSTITUTION: Regex = Regex::new(r#"<<((?P\d+)|(?P.{1,100}))>>"#).unwrap(); pub static ref REGEX_CHANNEL_USER: Regex = Regex::new(r#"\s*<(#|@)(?:!)?(\d+)>\s*"#).unwrap();