silent mentioning for roles in reminders

This commit is contained in:
jellywx 2020-12-15 23:03:41 +00:00
parent 6402a9b705
commit 44d06e4298
3 changed files with 39 additions and 19 deletions

View File

@ -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 + 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 + 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 + 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 <<Role name>>

View File

@ -33,7 +33,7 @@ use chrono::{offset::TimeZone, NaiveDateTime};
use rand::{rngs::OsRng, seq::IteratorRandom}; use rand::{rngs::OsRng, seq::IteratorRandom};
use sqlx::{encode::Encode, MySql, MySqlPool, Type}; use sqlx::MySqlPool;
use std::str::from_utf8; use std::str::from_utf8;
@ -48,7 +48,9 @@ use std::{
time::{SystemTime, UNIX_EPOCH}, 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 { fn shorthand_displacement(seconds: u64) -> String {
let (days, seconds) = seconds.div_rem(&DAY); let (days, seconds) = seconds.div_rem(&DAY);
@ -1397,31 +1399,47 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
} }
} }
async fn create_reminder< fn substitute_content(guild: Option<Guild>, content: &str) -> String {
'a, if let Some(guild) = guild {
U: Into<u64>, REGEX_CONTENT_SUBSTITUTION
T: TryInto<i64>, .replace(content, |caps: &Captures| {
S: ToString + Type<MySql> + Encode<'a, MySql>, if let Some(user) = caps.name("user") {
>( format!("<@{}>", user.as_str())
ctx: impl CacheHttp, } 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>>", "@everyone")
.replace("<<here>>", "@here")
}
async fn create_reminder<'a, U: Into<u64>, T: TryInto<i64>>(
ctx: impl CacheHttp + AsRef<Cache>,
pool: &MySqlPool, pool: &MySqlPool,
user_id: U, user_id: U,
guild_id: Option<GuildId>, guild_id: Option<GuildId>,
scope_id: &ReminderScope, scope_id: &ReminderScope,
time_parser: T, time_parser: T,
interval: Option<i64>, interval: Option<i64>,
content: S, content: &str,
) -> Result<(), ReminderError> { ) -> Result<(), ReminderError> {
let user_id = user_id.into(); let user_id = user_id.into();
let mut content_string = content.to_string(); let content_string = if let Some(g_id) = guild_id {
substitute_content(g_id.to_guild_cached(&ctx).await, content)
// substitution filters } else {
content_string = content_string.replace("<<everyone>>", "@everyone"); content.to_string()
content_string = content_string.replace("<<here>>", "@here"); };
content_string = REGEX_CONTENT_SUBSTITUTION
.replace(&content_string, "<@$1>")
.to_string();
let mut nudge = 0; let mut nudge = 0;

View File

@ -44,7 +44,7 @@ lazy_static! {
pub static ref REGEX_ALIAS: Regex = pub static ref REGEX_ALIAS: Regex =
Regex::new(r#"(?P<name>[\S]{1,12})(?:(?: (?P<cmd>.*)$)|$)"#).unwrap(); Regex::new(r#"(?P<name>[\S]{1,12})(?:(?: (?P<cmd>.*)$)|$)"#).unwrap();
pub static ref REGEX_CONTENT_SUBSTITUTION: Regex = Regex::new(r#"<<(\d+)>>"#).unwrap(); pub static ref REGEX_CONTENT_SUBSTITUTION: Regex = Regex::new(r#"<<((?P<user>\d+)|(?P<role>.{1,100}))>>"#).unwrap();
pub static ref REGEX_CHANNEL_USER: Regex = Regex::new(r#"\s*<(#|@)(?:!)?(\d+)>\s*"#).unwrap(); pub static ref REGEX_CHANNEL_USER: Regex = Regex::new(r#"\s*<(#|@)(?:!)?(\d+)>\s*"#).unwrap();