feat: reintroduce PK support

Signed-off-by: seth <getchoo@tuta.io>
This commit is contained in:
seth 2023-12-04 19:15:59 -05:00
parent 95fe62051b
commit f955cbb933
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
6 changed files with 138 additions and 5 deletions

View file

@ -8,13 +8,14 @@ use poise::{Event, FrameworkContext};
mod delete;
mod eta;
mod expand_link;
pub mod pluralkit;
mod support_onboard;
pub async fn handle(
ctx: &Context,
event: &Event<'_>,
_framework: FrameworkContext<'_, Data, Report>,
_data: &Data,
data: &Data,
) -> Result<()> {
match event {
Event::Ready { data_about_bot } => {
@ -35,6 +36,16 @@ pub async fn handle(
return Ok(());
}
// detect PK users first to make sure we don't respond to unproxied messages
pluralkit::handle(ctx, new_message, data).await?;
if data.storage.is_user_plural(new_message.author.id).await?
&& pluralkit::is_message_proxied(new_message).await?
{
debug!("Not replying to unproxied PluralKit message");
return Ok(());
}
eta::handle(ctx, new_message).await?;
expand_link::handle(ctx, new_message).await?;
}

View file

@ -0,0 +1,42 @@
use crate::{api, Data};
use std::time::Duration;
use color_eyre::eyre::Result;
use log::*;
use poise::serenity_prelude::{Context, Message};
use tokio::time::sleep;
const PK_DELAY_SEC: Duration = Duration::from_secs(1000);
pub async fn is_message_proxied(message: &Message) -> Result<bool> {
debug!(
"Waiting on PluralKit API for {} seconds",
PK_DELAY_SEC.as_secs()
);
sleep(PK_DELAY_SEC).await;
let proxied = api::pluralkit::get_sender(message.id).await.is_ok();
Ok(proxied)
}
pub async fn handle(_ctx: &Context, msg: &Message, data: &Data) -> Result<()> {
if msg.webhook_id.is_some() {
debug!(
"Message {} has a webhook ID. Checking if it was sent through PluralKit",
msg.id
);
debug!(
"Waiting on PluralKit API for {} seconds",
PK_DELAY_SEC.as_secs()
);
sleep(PK_DELAY_SEC).await;
if let Ok(sender) = api::pluralkit::get_sender(msg.id).await {
data.storage.store_user_plurality(sender).await?;
}
}
Ok(())
}