Handle deleted channels in sender
This commit is contained in:
parent
82dab53744
commit
5ee9094bac
@ -237,11 +237,11 @@ impl Into<CreateEmbed> for Embed {
|
||||
pub struct Reminder {
|
||||
id: u32,
|
||||
|
||||
channel_id: u64,
|
||||
channel_id: Option<u64>,
|
||||
webhook_id: Option<u64>,
|
||||
webhook_token: Option<String>,
|
||||
|
||||
channel_paused: bool,
|
||||
channel_paused: Option<bool>,
|
||||
channel_paused_until: Option<NaiveDateTime>,
|
||||
enabled: bool,
|
||||
|
||||
@ -297,7 +297,7 @@ SELECT
|
||||
reminders.`username` AS username
|
||||
FROM
|
||||
reminders
|
||||
INNER JOIN
|
||||
LEFT JOIN
|
||||
channels
|
||||
ON
|
||||
reminders.channel_id = channels.id
|
||||
@ -310,7 +310,6 @@ WHERE
|
||||
reminders
|
||||
WHERE
|
||||
reminders.`utc_time` <= NOW() AND
|
||||
reminders.`channel_id` IS NOT NULL AND
|
||||
`status` = 'pending' AND
|
||||
(
|
||||
reminders.`interval_seconds` IS NOT NULL
|
||||
@ -344,7 +343,10 @@ WHERE
|
||||
|
||||
async fn reset_webhook(&self, pool: impl Executor<'_, Database = Database> + Copy) {
|
||||
let _ = sqlx::query!(
|
||||
"UPDATE channels SET webhook_id = NULL, webhook_token = NULL WHERE channel = ?",
|
||||
"
|
||||
UPDATE channels SET webhook_id = NULL, webhook_token = NULL
|
||||
WHERE channel = ?
|
||||
",
|
||||
self.channel_id
|
||||
)
|
||||
.execute(pool)
|
||||
@ -416,7 +418,9 @@ WHERE
|
||||
self.set_sent(pool).await;
|
||||
} else {
|
||||
sqlx::query!(
|
||||
"UPDATE reminders SET `utc_time` = ? WHERE `id` = ?",
|
||||
"
|
||||
UPDATE reminders SET `utc_time` = ? WHERE `id` = ?
|
||||
",
|
||||
updated_reminder_time.with_timezone(&Utc),
|
||||
self.id
|
||||
)
|
||||
@ -449,7 +453,10 @@ WHERE
|
||||
|
||||
if *LOG_TO_DATABASE {
|
||||
sqlx::query!(
|
||||
"INSERT INTO stat (type, reminder_id, message) VALUES ('reminder_failed', ?, ?)",
|
||||
"
|
||||
INSERT INTO stat (type, reminder_id, message)
|
||||
VALUES ('reminder_failed', ?, ?)
|
||||
",
|
||||
self.id,
|
||||
message,
|
||||
)
|
||||
@ -462,7 +469,10 @@ WHERE
|
||||
async fn log_success(&self, pool: impl Executor<'_, Database = Database> + Copy) {
|
||||
if *LOG_TO_DATABASE {
|
||||
sqlx::query!(
|
||||
"INSERT INTO stat (type, reminder_id) VALUES ('reminder_sent', ?)",
|
||||
"
|
||||
INSERT INTO stat (type, reminder_id)
|
||||
VALUES ('reminder_sent', ?)
|
||||
",
|
||||
self.id,
|
||||
)
|
||||
.execute(pool)
|
||||
@ -491,7 +501,11 @@ WHERE
|
||||
message: &'static str,
|
||||
) {
|
||||
sqlx::query!(
|
||||
"UPDATE reminders SET `status` = 'failed', `status_message` = ? WHERE `id` = ?",
|
||||
"
|
||||
UPDATE reminders
|
||||
SET `status` = 'failed', `status_message` = ?, `status_change_time` = NOW()
|
||||
WHERE `id` = ?
|
||||
",
|
||||
message,
|
||||
self.id
|
||||
)
|
||||
@ -501,7 +515,9 @@ WHERE
|
||||
}
|
||||
|
||||
async fn pin_message<M: Into<u64>>(&self, message_id: M, http: impl AsRef<Http>) {
|
||||
let _ = http.as_ref().pin_message(self.channel_id, message_id.into(), None).await;
|
||||
if let Some(channel_id) = self.channel_id {
|
||||
let _ = http.as_ref().pin_message(channel_id, message_id.into(), None).await;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send(
|
||||
@ -511,10 +527,11 @@ WHERE
|
||||
) {
|
||||
async fn send_to_channel(
|
||||
cache_http: impl CacheHttp,
|
||||
channel_id: u64,
|
||||
reminder: &Reminder,
|
||||
embed: Option<CreateEmbed>,
|
||||
) -> Result<()> {
|
||||
let channel = ChannelId(reminder.channel_id).to_channel(&cache_http).await;
|
||||
let channel = ChannelId(channel_id).to_channel(&cache_http).await;
|
||||
|
||||
match channel {
|
||||
Ok(Channel::Guild(channel)) => {
|
||||
@ -546,6 +563,7 @@ WHERE
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Channel::Private(channel)) => {
|
||||
match channel
|
||||
.send_message(&cache_http.http(), |m| {
|
||||
@ -575,7 +593,9 @@ WHERE
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
Err(e) => Err(e),
|
||||
|
||||
_ => Err(Error::Other("Channel not of valid type")),
|
||||
}
|
||||
}
|
||||
@ -630,14 +650,20 @@ WHERE
|
||||
}
|
||||
}
|
||||
|
||||
match self.channel_id {
|
||||
Some(channel_id) => {
|
||||
if self.enabled
|
||||
&& !(self.channel_paused
|
||||
&& !(self.channel_paused.unwrap_or(false)
|
||||
&& self
|
||||
.channel_paused_until
|
||||
.map_or(true, |inner| inner >= Utc::now().naive_local()))
|
||||
{
|
||||
let _ = sqlx::query!(
|
||||
"UPDATE `channels` SET paused = 0, paused_until = NULL WHERE `channel` = ?",
|
||||
"
|
||||
UPDATE `channels`
|
||||
SET paused = 0, paused_until = NULL
|
||||
WHERE `channel` = ?
|
||||
",
|
||||
self.channel_id
|
||||
)
|
||||
.execute(pool)
|
||||
@ -648,8 +674,10 @@ WHERE
|
||||
let result = if let (Some(webhook_id), Some(webhook_token)) =
|
||||
(self.webhook_id, &self.webhook_token)
|
||||
{
|
||||
let webhook_res =
|
||||
cache_http.http().get_webhook_with_token(webhook_id, webhook_token).await;
|
||||
let webhook_res = cache_http
|
||||
.http()
|
||||
.get_webhook_with_token(webhook_id, webhook_token)
|
||||
.await;
|
||||
|
||||
if let Ok(webhook) = webhook_res {
|
||||
send_to_webhook(cache_http, &self, webhook, embed).await
|
||||
@ -657,10 +685,10 @@ WHERE
|
||||
warn!("Webhook vanished for reminder {}: {:?}", self.id, webhook_res);
|
||||
|
||||
self.reset_webhook(pool).await;
|
||||
send_to_channel(cache_http, &self, embed).await
|
||||
send_to_channel(cache_http, channel_id, &self, embed).await
|
||||
}
|
||||
} else {
|
||||
send_to_channel(cache_http, &self, embed).await
|
||||
send_to_channel(cache_http, channel_id, &self, embed).await
|
||||
};
|
||||
|
||||
if let Err(e) = result {
|
||||
@ -687,7 +715,10 @@ WHERE
|
||||
None::<&'static str>,
|
||||
)
|
||||
.await;
|
||||
self.set_failed(pool, "Could not be sent as guild does not exist")
|
||||
self.set_failed(
|
||||
pool,
|
||||
"Could not be sent as guild does not exist",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
50001 => {
|
||||
@ -697,7 +728,11 @@ WHERE
|
||||
None::<&'static str>,
|
||||
)
|
||||
.await;
|
||||
self.set_failed(pool, "Could not be sent as missing access").await;
|
||||
self.set_failed(
|
||||
pool,
|
||||
"Could not be sent as missing access",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
50007 => {
|
||||
self.log_error(
|
||||
@ -706,7 +741,10 @@ WHERE
|
||||
None::<&'static str>,
|
||||
)
|
||||
.await;
|
||||
self.set_failed(pool, "Could not be sent as user has DMs disabled")
|
||||
self.set_failed(
|
||||
pool,
|
||||
"Could not be sent as user has DMs disabled",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
50013 => {
|
||||
@ -750,4 +788,13 @@ WHERE
|
||||
self.refresh(pool).await;
|
||||
}
|
||||
}
|
||||
|
||||
None => {
|
||||
info!("Reminder {} is orphaned", self.id);
|
||||
|
||||
self.log_error(pool, "Orphaned", Option::<u8>::None).await;
|
||||
self.set_failed(pool, "Could not be sent as channel was deleted").await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +75,8 @@ impl ChannelData {
|
||||
UPDATE channels
|
||||
SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhook_token = ?,
|
||||
paused = ?, paused_until = ?
|
||||
WHERE id = ?",
|
||||
WHERE id = ?
|
||||
",
|
||||
self.name,
|
||||
self.nudge,
|
||||
self.blacklisted,
|
||||
|
@ -173,6 +173,7 @@ pub async fn import_reminders(
|
||||
utc_time: record.utc_time,
|
||||
status: "pending".to_string(),
|
||||
status_change_time: None,
|
||||
status_message: None,
|
||||
};
|
||||
|
||||
create_reminder(
|
||||
|
@ -361,7 +361,8 @@ pub async fn get_reminders(
|
||||
reminders.username,
|
||||
reminders.utc_time,
|
||||
reminders.status,
|
||||
reminders.status_change_time
|
||||
reminders.status_change_time,
|
||||
reminders.status_message
|
||||
FROM reminders
|
||||
LEFT JOIN channels ON channels.id = reminders.channel_id
|
||||
WHERE FIND_IN_SET(`status`, ?) AND reminders.guild_id = (SELECT id FROM guilds WHERE guild = ?)",
|
||||
@ -549,7 +550,8 @@ pub async fn edit_reminder(
|
||||
|
||||
match sqlx::query_as_unchecked!(
|
||||
Reminder,
|
||||
"SELECT reminders.attachment,
|
||||
"
|
||||
SELECT reminders.attachment,
|
||||
reminders.attachment_name,
|
||||
reminders.avatar,
|
||||
channels.channel,
|
||||
@ -576,7 +578,8 @@ pub async fn edit_reminder(
|
||||
reminders.username,
|
||||
reminders.utc_time,
|
||||
reminders.status,
|
||||
reminders.status_change_time
|
||||
reminders.status_change_time,
|
||||
reminders.status_message
|
||||
FROM reminders
|
||||
LEFT JOIN channels ON channels.id = reminders.channel_id
|
||||
WHERE uid = ?",
|
||||
|
@ -151,6 +151,7 @@ pub struct Reminder {
|
||||
username: Option<String>,
|
||||
utc_time: NaiveDateTime,
|
||||
status: String,
|
||||
status_message: Option<String>,
|
||||
status_change_time: Option<NaiveDateTime>,
|
||||
}
|
||||
|
||||
@ -504,7 +505,8 @@ pub async fn create_reminder(
|
||||
|
||||
// write to db
|
||||
match sqlx::query!(
|
||||
"INSERT INTO reminders (
|
||||
"
|
||||
INSERT INTO reminders (
|
||||
uid,
|
||||
attachment,
|
||||
attachment_name,
|
||||
@ -594,7 +596,8 @@ pub async fn create_reminder(
|
||||
reminders.username,
|
||||
reminders.utc_time,
|
||||
reminders.status,
|
||||
reminders.status_change_time
|
||||
reminders.status_change_time,
|
||||
reminders.status_message
|
||||
FROM reminders
|
||||
LEFT JOIN channels ON channels.id = reminders.channel_id
|
||||
WHERE uid = ?",
|
||||
|
@ -758,6 +758,16 @@ div.reminderError .errorHead .reminderTime {
|
||||
border-style: solid;
|
||||
}
|
||||
|
||||
div.reminderError .reminderMessage {
|
||||
font-size: 1rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
color: rgb(54, 54, 54);
|
||||
flex-grow: 1;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* other stuff */
|
||||
|
||||
.half-rem {
|
||||
|
@ -30,6 +30,8 @@ document.addEventListener("paneLoad", (ev) => {
|
||||
{ zone: "UTC" }
|
||||
);
|
||||
newRow.querySelector(".reminderName").textContent = reminder.name;
|
||||
newRow.querySelector(".reminderMessage").textContent =
|
||||
reminder.status_message;
|
||||
newRow.querySelector(".reminderTime").textContent = statusTime
|
||||
.toLocal()
|
||||
.toLocaleString(luxon.DateTime.DATETIME_MED);
|
||||
|
@ -9,6 +9,9 @@
|
||||
</div>
|
||||
<div class="reminderName">
|
||||
Reminder
|
||||
</div>
|
||||
<div class="reminderMessage">
|
||||
|
||||
</div>
|
||||
<div class="reminderTime">
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user