Recreate webhooks when they go missing, reuse older webhooks, and initiate connections to Redis cluster

This commit is contained in:
2025-08-20 23:20:21 -03:00
parent ee49b0a984
commit aba11cb2da
15 changed files with 553 additions and 34 deletions
+51 -10
View File
@@ -13,7 +13,9 @@ use thiserror::Error;
use time::{Duration, OffsetDateTime, UtcDateTime, UtcOffset};
use twilight_http::Client;
use twilight_model::{
channel::Message as TwilightMessage, guild::Guild as TwilightGuild, id::Id,
channel::{Message as TwilightMessage, Webhook},
guild::Guild as TwilightGuild,
id::Id,
user::User as TwilightUser,
};
@@ -478,6 +480,38 @@ impl<'a> Object<'a> for Channel {
}
}
async fn get_or_create_channel_webhook(
channel_id: u64,
http: &Client,
) -> Result<Webhook, DatabaseOrDiscordError> {
let webhooks = http
.channel_webhooks(Id::new(channel_id))
.await
.inspect_err(|e| log::error!("Failed to get webhooks from channel {channel_id}: {e:?}"))
.map_err(DatabaseOrDiscordError::Discord)?
.model()
.await
.inspect_err(|e| log::error!("Failed to get model for webhooks {channel_id}: {e:?}"))
.map_err(|_| DatabaseOrDiscordError::DiscordDeserializationFailed)?;
let webhook = if let Some(item) = webhooks.into_iter().find(|item| item.token.is_some()) {
item
} else {
http.create_webhook(Id::new(channel_id), "Payphone Messages")
.await
.inspect_err(|e| {
log::error!("Failed to create webhook for channel {channel_id}: {e:?}")
})
.map_err(DatabaseOrDiscordError::Discord)?
.model()
.await
.inspect_err(|e| log::error!("Failed to get model for webhook {channel_id}: {e:?}"))
.map_err(|_| DatabaseOrDiscordError::DiscordDeserializationFailed)?
};
Ok(webhook)
}
impl Channel {
pub async fn get_by_id(id: u64, session: &Session) -> Result<Option<Self>, DatabaseError> {
let id = u64_to_i64(id);
@@ -520,15 +554,7 @@ impl Channel {
{
Ok(channel)
} else {
let webhook = http
.create_webhook(Id::new(id), "Payphone Messages")
.await
.inspect_err(|e| log::error!("Failed to create webhook for channel {id}: {e:?}"))
.map_err(DatabaseOrDiscordError::Discord)?
.model()
.await
.inspect_err(|e| log::error!("Failed to get model for webhook {id}: {e:?}"))
.map_err(|_| DatabaseOrDiscordError::DiscordDeserializationFailed)?;
let webhook = get_or_create_channel_webhook(id, http).await?;
let response = http
.channel(Id::new(id))
@@ -593,6 +619,21 @@ impl Channel {
Ok(())
}
pub async fn update_webhook(
&mut self,
session: &Session,
http: &Client,
) -> Result<(), DatabaseOrDiscordError> {
let webhook = get_or_create_channel_webhook(self.id, http).await?;
self.webhook_id = Some(webhook.id.get());
self.webhook_token = webhook.token;
self.upsert(session).await?;
Ok(())
}
}
#[derive(Debug, Clone)]