-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
Signed-off-by: seth <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use once_cell::sync::Lazy; | ||
|
||
pub mod dadjoke; | ||
pub mod pluralkit; | ||
pub mod prism_meta; | ||
pub mod rory; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::api::REQWEST_CLIENT; | ||
|
||
use color_eyre::eyre::{eyre, Context, Result}; | ||
use log::*; | ||
use poise::serenity_prelude::{MessageId, UserId}; | ||
use reqwest::StatusCode; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct PluralKitMessage { | ||
pub sender: String, | ||
} | ||
|
||
const PLURAL_KIT: &str = "https://api.pluralkit.me/v2"; | ||
const MESSAGES_ENDPOINT: &str = "/messages"; | ||
|
||
pub async fn get_sender(message_id: MessageId) -> Result<UserId> { | ||
let req = REQWEST_CLIENT | ||
.get(format!("{PLURAL_KIT}{MESSAGES_ENDPOINT}/{message_id}")) | ||
.build()?; | ||
|
||
info!("Making request to {}", req.url()); | ||
let resp = REQWEST_CLIENT.execute(req).await?; | ||
let status = resp.status(); | ||
|
||
if let StatusCode::OK = status { | ||
let data = resp.json::<PluralKitMessage>().await?; | ||
let id: u64 = data.sender.parse().wrap_err_with(|| format!("Couldn't parse response from PluralKit as a UserId! Here's the response:\n{data:#?}"))?; | ||
let sender = UserId::from(id); | ||
|
||
Ok(sender) | ||
} else { | ||
Err(eyre!( | ||
"Failed to get PluralKit message information from {PLURAL_KIT}{MESSAGES_ENDPOINT}/{message_id} with {status}", | ||
)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use color_eyre::eyre::Result; | ||
use log::*; | ||
use poise::serenity_prelude::UserId; | ||
use redis::{AsyncCommands as _, Client}; | ||
|
||
pub const USER_KEY: &str = "users-v1"; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Storage { | ||
client: Client, | ||
} | ||
|
||
impl Storage { | ||
pub fn new(redis_url: &str) -> Result<Self> { | ||
let client = Client::open(redis_url)?; | ||
|
||
Ok(Self { client }) | ||
} | ||
|
||
pub async fn store_user_plurality(&self, sender: UserId) -> Result<()> { | ||
let mut con = self.client.get_async_connection().await?; | ||
|
||
info!("Marking {sender} as a PluralKit user"); | ||
let key = format!("{USER_KEY}:{sender}:pk"); | ||
|
||
// Just store some value. We only care about the presence of this key | ||
con.set(&key, 0).await?; | ||
con.expire(key, 7 * 24 * 60 * 60).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn is_user_plural(&self, user_id: UserId) -> Result<bool> { | ||
let key = format!("{USER_KEY}:{user_id}:pk"); | ||
let mut con = self.client.get_async_connection().await?; | ||
|
||
let exists: bool = con.exists(key).await?; | ||
Ok(exists) | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
getchoo
Author
Member
|
||
} |
Maybe pluralkit storage should be separate to mod storage?