$look has new flag all which views reminders from entire guild. reminders now support basic substitution: <<id>> for a user, or <<everyone>> and <<here>> for the everyone and here tags respectively

This commit is contained in:
jellywx 2020-11-09 13:47:11 +00:00
parent 0f04dddeb4
commit 4bc7b36fd1
4 changed files with 80 additions and 22 deletions

2
Cargo.lock generated
View File

@ -1175,7 +1175,7 @@ dependencies = [
[[package]] [[package]]
name = "reminder_rs" name = "reminder_rs"
version = "1.1.10" version = "1.2.0"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"async-trait", "async-trait",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_rs" name = "reminder_rs"
version = "1.1.10" version = "1.2.0"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -22,7 +22,7 @@ use crate::{
check_subscription_on_message, check_subscription_on_message,
consts::{ consts::{
CHARACTERS, DAY, HOUR, LOCAL_TIMEZONE, MAX_TIME, MINUTE, MIN_INTERVAL, PYTHON_LOCATION, CHARACTERS, DAY, HOUR, LOCAL_TIMEZONE, MAX_TIME, MINUTE, MIN_INTERVAL, PYTHON_LOCATION,
REGEX_CHANNEL, REGEX_CHANNEL_USER, REGEX_CHANNEL, REGEX_CHANNEL_USER, REGEX_CONTENT_SUBSTITUTION,
}, },
framework::SendIterator, framework::SendIterator,
models::{ChannelData, GuildData, Timer, UserData}, models::{ChannelData, GuildData, Timer, UserData},
@ -306,10 +306,16 @@ enum TimeDisplayType {
Relative, Relative,
} }
enum Selection<T> {
None,
Single(T),
All,
}
struct LookFlags { struct LookFlags {
pub limit: u16, pub limit: u16,
pub show_disabled: bool, pub show_disabled: bool,
pub channel_id: Option<u64>, pub channel_id: Selection<u64>,
time_display: TimeDisplayType, time_display: TimeDisplayType,
} }
@ -318,7 +324,7 @@ impl Default for LookFlags {
Self { Self {
limit: u16::MAX, limit: u16::MAX,
show_disabled: true, show_disabled: true,
channel_id: None, channel_id: Selection::None,
time_display: TimeDisplayType::Relative, time_display: TimeDisplayType::Relative,
} }
} }
@ -338,6 +344,10 @@ impl LookFlags {
new_flags.time_display = TimeDisplayType::Absolute; new_flags.time_display = TimeDisplayType::Absolute;
} }
"all" => {
new_flags.channel_id = Selection::All;
}
param => { param => {
if let Ok(val) = param.parse::<u16>() { if let Ok(val) = param.parse::<u16>() {
new_flags.limit = val; new_flags.limit = val;
@ -346,7 +356,8 @@ impl LookFlags {
.captures(&args) .captures(&args)
.map(|cap| cap.get(1)) .map(|cap| cap.get(1))
.flatten() .flatten()
.map(|c| c.as_str().parse::<u64>().unwrap()); .map(|c| c.as_str().parse::<u64>().unwrap())
.map_or(Selection::None, Selection::Single);
} }
} }
} }
@ -392,13 +403,16 @@ async fn look(ctx: &Context, msg: &Message, args: String) {
let enabled = if flags.show_disabled { "0,1" } else { "1" }; let enabled = if flags.show_disabled { "0,1" } else { "1" };
let reminders = if let Some(guild_id) = msg.guild_id.map(|f| f.as_u64().to_owned()) { let reminders = if let Some(guild_id) = msg.guild_id.map(|f| f.as_u64().to_owned()) {
let channel_id = flags let channel_id_opt = match flags.channel_id {
.channel_id Selection::None => Some(msg.channel_id.as_u64().to_owned()),
.unwrap_or_else(|| msg.channel_id.as_u64().to_owned()); Selection::Single(id) => Some(id),
Selection::All => None,
};
sqlx::query_as!( if let Some(channel_id) = channel_id_opt {
LookReminder, sqlx::query_as!(
" LookReminder,
"
SELECT SELECT
reminders.id, reminders.time, channels.channel, messages.content, embeds.description reminders.id, reminders.time, channels.channel, messages.content, embeds.description
FROM FROM
@ -424,13 +438,48 @@ ORDER BY
LIMIT LIMIT
? ?
", ",
guild_id, guild_id,
channel_id, channel_id,
enabled, enabled,
flags.limit flags.limit
) )
.fetch_all(&pool) .fetch_all(&pool)
.await .await
} else {
sqlx::query_as!(
LookReminder,
"
SELECT
reminders.id, reminders.time, channels.channel, messages.content, embeds.description
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
INNER JOIN
messages
ON
messages.id = reminders.message_id
LEFT JOIN
embeds
ON
embeds.id = messages.embed_id
WHERE
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?) AND
FIND_IN_SET(reminders.enabled, ?)
ORDER BY
reminders.time
LIMIT
?
",
guild_id,
enabled,
flags.limit
)
.fetch_all(&pool)
.await
}
} else { } else {
sqlx::query_as!( sqlx::query_as!(
LookReminder, LookReminder,
@ -1253,7 +1302,15 @@ async fn create_reminder<T: TryInto<i64>, S: ToString + Type<MySql> + Encode<MyS
interval: Option<i64>, interval: Option<i64>,
content: S, content: S,
) -> Result<(), ReminderError> { ) -> Result<(), ReminderError> {
let content_string = content.to_string(); let mut content_string = content.to_string();
// substitution filters
content_string = content_string.replace("<<everyone>>", "@everyone");
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;
let db_channel_id = match scope_id { let db_channel_id = match scope_id {
@ -1319,7 +1376,7 @@ async fn create_reminder<T: TryInto<i64>, S: ToString + Type<MySql> + Encode<MyS
" "
INSERT INTO messages (content) VALUES (?) INSERT INTO messages (content) VALUES (?)
", ",
content content_string
) )
.execute(&pool.clone()) .execute(&pool.clone())
.await .await
@ -1334,7 +1391,7 @@ INSERT INTO reminders (uid, message_id, channel_id, time, `interval`, method, se
(SELECT id FROM users WHERE user = ? LIMIT 1)) (SELECT id FROM users WHERE user = ? LIMIT 1))
", ",
generate_uid(), generate_uid(),
content, content_string,
db_channel_id, db_channel_id,
time as u32, time as u32,
interval, interval,

View File

@ -28,6 +28,7 @@ lazy_static! {
pub static ref REGEX_COMMANDS: Regex = Regex::new(r#"([a-z]+)"#).unwrap(); pub static ref REGEX_COMMANDS: Regex = Regex::new(r#"([a-z]+)"#).unwrap();
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_CHANNEL_USER: Regex = Regex::new(r#"\s*<(#|@)(?:!)?(\d+)>\s*"#).unwrap(); pub static ref REGEX_CHANNEL_USER: Regex = Regex::new(r#"\s*<(#|@)(?:!)?(\d+)>\s*"#).unwrap();
pub static ref MIN_INTERVAL: i64 = env::var("MIN_INTERVAL") pub static ref MIN_INTERVAL: i64 = env::var("MIN_INTERVAL")
.ok() .ok()