-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reminder task, refactor & cleanup
- Loading branch information
Showing
13 changed files
with
191 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod get_patrons; | ||
pub mod get_premium_users; | ||
pub mod reminders; | ||
pub mod top_gg_stats; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use assyst_common::err; | ||
use assyst_common::util::discord::message_link; | ||
use assyst_database::model::reminder::Reminder; | ||
use twilight_model::channel::message::AllowedMentions; | ||
use twilight_model::id::marker::{ChannelMarker, UserMarker}; | ||
use twilight_model::id::Id; | ||
|
||
use crate::assyst::ThreadSafeAssyst; | ||
|
||
// 30 seconds | ||
pub static FETCH_INTERVAL: i64 = 30000; | ||
|
||
async fn process_single_reminder(assyst: ThreadSafeAssyst, reminder: &Reminder) -> anyhow::Result<()> { | ||
assyst | ||
.http_client | ||
.create_message(Id::<ChannelMarker>::new(reminder.channel_id as u64)) | ||
.allowed_mentions(Some(&AllowedMentions { | ||
parse: vec![], | ||
replied_user: false, | ||
roles: vec![], | ||
users: vec![Id::<UserMarker>::new(reminder.user_id as u64)], | ||
})) | ||
.content(&format!( | ||
"<@{}> Reminder: {}\n{}", | ||
reminder.user_id, | ||
reminder.message, | ||
message_link( | ||
reminder.guild_id as u64, | ||
reminder.channel_id as u64, | ||
reminder.message_id as u64 | ||
) | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn process_reminders(assyst: ThreadSafeAssyst, reminders: Vec<Reminder>) -> Result<(), anyhow::Error> { | ||
if reminders.len() < 1 { | ||
return Ok(()); | ||
} | ||
|
||
for reminder in &reminders { | ||
if let Err(e) = process_single_reminder(assyst.clone(), &reminder).await { | ||
err!("Failed to process reminder: {:?}", e); | ||
} | ||
|
||
// Once we're done, delete them from database | ||
reminder.remove(&assyst.database_handler).await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn handle_reminders(assyst: ThreadSafeAssyst) { | ||
let reminders = Reminder::fetch_expiring_max(&assyst.database_handler, FETCH_INTERVAL).await; | ||
|
||
match reminders { | ||
Ok(reminders) => { | ||
if let Err(e) = process_reminders(assyst.clone(), reminders).await { | ||
err!("Processing reminder queue failed: {:?}", e); | ||
} | ||
}, | ||
Err(e) => { | ||
err!("Fetching reminders failed: {:?}", e); | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod free_tier_2_requests; | ||
pub mod global_blacklist; | ||
pub mod prefix; | ||
pub mod reminder; | ||
pub mod user_votes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::DatabaseHandler; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
#[derive(sqlx::FromRow, Debug)] | ||
pub struct Reminder { | ||
pub id: i32, | ||
pub user_id: i64, | ||
pub timestamp: i64, | ||
pub guild_id: i64, | ||
pub channel_id: i64, | ||
pub message_id: i64, | ||
pub message: String, | ||
} | ||
impl Reminder { | ||
pub async fn fetch_expiring_max(handler: &DatabaseHandler, time_delta: i64) -> Result<Vec<Self>, sqlx::Error> { | ||
let query = "SELECT * FROM reminders WHERE timestamp < $1"; | ||
|
||
let unix: i64 = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.expect("Time went backwards") | ||
.as_millis() | ||
.try_into() | ||
.expect("count not fit u128 into target type"); | ||
|
||
sqlx::query_as::<_, Self>(query) | ||
.bind(unix + time_delta) | ||
.fetch_all(&handler.pool) | ||
.await | ||
} | ||
|
||
/// True on successful remove, false otherwise | ||
pub async fn remove(&self, handler: &DatabaseHandler) -> Result<bool, sqlx::Error> { | ||
let query = r#"DELETE FROM reminders WHERE user_id = $1 AND id = $2 RETURNING *"#; | ||
|
||
sqlx::query(query) | ||
.bind(self.user_id as i64) | ||
.bind(self.id) | ||
.fetch_all(&handler.pool) | ||
.await | ||
.map(|s| !s.is_empty()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters