use http client from context
This commit is contained in:
parent
921540e249
commit
84a7cfe151
26 changed files with 148 additions and 149 deletions
|
@ -192,19 +192,20 @@ async fn outdated_launcher(log: &str, data: &Data) -> Result<Issue> {
|
|||
return Ok(None);
|
||||
};
|
||||
|
||||
let octocrab = &data.octocrab;
|
||||
let version_from_log = captures[0].replace("Prism Launcher version: ", "");
|
||||
|
||||
let latest_version = if let Some(storage) = &data.storage {
|
||||
if let Ok(version) = storage.launcher_version().await {
|
||||
version
|
||||
} else {
|
||||
let version = api::github::get_latest_prism_version().await?;
|
||||
let version = api::github::get_latest_prism_version(octocrab).await?;
|
||||
storage.cache_launcher_version(&version).await?;
|
||||
version
|
||||
}
|
||||
} else {
|
||||
trace!("Not caching launcher version, as we're running without a storage backend");
|
||||
api::github::get_latest_prism_version().await?
|
||||
api::github::get_latest_prism_version(octocrab).await?
|
||||
};
|
||||
|
||||
if version_from_log < latest_version {
|
||||
|
|
|
@ -19,7 +19,7 @@ pub async fn handle(ctx: &Context, message: &Message, data: &Data) -> Result<()>
|
|||
);
|
||||
let channel = message.channel_id;
|
||||
|
||||
let log = find_log(message).await;
|
||||
let log = find_log(&data.http_client, message).await;
|
||||
|
||||
if log.is_err() {
|
||||
let embed = CreateEmbed::new()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::api;
|
||||
use crate::api::{HttpClient, HttpClientExt};
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
|
@ -21,8 +21,8 @@ impl super::LogProvider for _0x0 {
|
|||
.nth(0)
|
||||
}
|
||||
|
||||
async fn fetch(&self, content: &str) -> Result<String> {
|
||||
let log = api::text_from_url(content).await?;
|
||||
async fn fetch(&self, http: &HttpClient, content: &str) -> Result<String> {
|
||||
let log = http.get_request(content).await?.text().await?;
|
||||
|
||||
Ok(log)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::api::{HttpClient, HttpClientExt};
|
||||
|
||||
use eyre::Result;
|
||||
use log::trace;
|
||||
use poise::serenity_prelude::Message;
|
||||
|
||||
use crate::api;
|
||||
|
||||
pub struct Attachment;
|
||||
|
||||
impl super::LogProvider for Attachment {
|
||||
|
@ -21,9 +21,10 @@ impl super::LogProvider for Attachment {
|
|||
.nth(0)
|
||||
}
|
||||
|
||||
async fn fetch(&self, content: &str) -> Result<String> {
|
||||
let attachment = api::bytes_from_url(content).await?;
|
||||
async fn fetch(&self, http: &HttpClient, content: &str) -> Result<String> {
|
||||
let attachment = http.get_request(content).await?.bytes().await?.to_vec();
|
||||
let log = String::from_utf8(attachment)?;
|
||||
|
||||
Ok(log)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::api;
|
||||
use crate::api::{HttpClient, HttpClientExt};
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
|
@ -22,9 +22,9 @@ impl super::LogProvider for Haste {
|
|||
super::get_first_capture(regex, &message.content)
|
||||
}
|
||||
|
||||
async fn fetch(&self, content: &str) -> Result<String> {
|
||||
async fn fetch(&self, http: &HttpClient, content: &str) -> Result<String> {
|
||||
let url = format!("{HASTE}{RAW}/{content}");
|
||||
let log = api::text_from_url(&url).await?;
|
||||
let log = http.get_request(&url).await?.text().await?;
|
||||
|
||||
Ok(log)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::api;
|
||||
use crate::api::{HttpClient, HttpClientExt};
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
|
@ -21,9 +21,9 @@ impl super::LogProvider for MCLogs {
|
|||
super::get_first_capture(regex, &message.content)
|
||||
}
|
||||
|
||||
async fn fetch(&self, content: &str) -> Result<String> {
|
||||
async fn fetch(&self, http: &HttpClient, content: &str) -> Result<String> {
|
||||
let url = format!("{MCLOGS}{RAW}/{content}");
|
||||
let log = api::text_from_url(&url).await?;
|
||||
let log = http.get_request(&url).await?.text().await?;
|
||||
|
||||
Ok(log)
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use crate::api::HttpClient;
|
||||
|
||||
use std::slice::Iter;
|
||||
|
||||
use enum_dispatch::enum_dispatch;
|
||||
|
@ -21,7 +23,7 @@ mod pastebin;
|
|||
#[enum_dispatch]
|
||||
pub trait LogProvider {
|
||||
async fn find_match(&self, message: &Message) -> Option<String>;
|
||||
async fn fetch(&self, content: &str) -> Result<String>;
|
||||
async fn fetch(&self, http: &HttpClient, content: &str) -> Result<String>;
|
||||
}
|
||||
|
||||
fn get_first_capture(regex: &Regex, string: &str) -> Option<String> {
|
||||
|
@ -41,7 +43,7 @@ enum Provider {
|
|||
}
|
||||
|
||||
impl Provider {
|
||||
pub fn interator() -> Iter<'static, Provider> {
|
||||
pub fn iterator() -> Iter<'static, Provider> {
|
||||
static PROVIDERS: [Provider; 6] = [
|
||||
Provider::_0x0st(_0x0st),
|
||||
Provider::Attachment(Attachment),
|
||||
|
@ -54,12 +56,12 @@ impl Provider {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn find_log(message: &Message) -> Result<Option<String>> {
|
||||
let providers = Provider::interator();
|
||||
pub async fn find_log(http: &HttpClient, message: &Message) -> Result<Option<String>> {
|
||||
let providers = Provider::iterator();
|
||||
|
||||
for provider in providers {
|
||||
if let Some(found) = provider.find_match(message).await {
|
||||
let log = provider.fetch(&found).await?;
|
||||
let log = provider.fetch(http, &found).await?;
|
||||
return Ok(Some(log));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::api::paste_gg;
|
||||
use crate::api::{paste_gg, HttpClient};
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
|
@ -18,8 +18,8 @@ impl super::LogProvider for PasteGG {
|
|||
super::get_first_capture(regex, &message.content)
|
||||
}
|
||||
|
||||
async fn fetch(&self, content: &str) -> Result<String> {
|
||||
let files = paste_gg::files_from(content).await?;
|
||||
async fn fetch(&self, http: &HttpClient, content: &str) -> Result<String> {
|
||||
let files = paste_gg::files_from(http, content).await?;
|
||||
let result = files
|
||||
.result
|
||||
.ok_or_eyre("Got an empty result from paste.gg!")?;
|
||||
|
@ -30,7 +30,7 @@ impl super::LogProvider for PasteGG {
|
|||
.nth(0)
|
||||
.ok_or_eyre("Couldn't get file id from empty paste.gg response!")?;
|
||||
|
||||
let log = paste_gg::get_raw_file(content, file_id).await?;
|
||||
let log = paste_gg::get_raw_file(http, content, file_id).await?;
|
||||
|
||||
Ok(log)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::api;
|
||||
use crate::api::{HttpClient, HttpClientExt};
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
|
@ -22,9 +22,9 @@ impl super::LogProvider for PasteBin {
|
|||
super::get_first_capture(regex, &message.content)
|
||||
}
|
||||
|
||||
async fn fetch(&self, content: &str) -> Result<String> {
|
||||
async fn fetch(&self, http: &HttpClient, content: &str) -> Result<String> {
|
||||
let url = format!("{PASTEBIN}{RAW}/{content}");
|
||||
let log = api::text_from_url(&url).await?;
|
||||
let log = http.get_request(&url).await?.text().await?;
|
||||
|
||||
Ok(log)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use crate::{api::HttpClient, utils};
|
||||
|
||||
use eyre::Result;
|
||||
use poise::serenity_prelude::{Context, CreateAllowedMentions, CreateMessage, Message};
|
||||
|
||||
use crate::utils;
|
||||
|
||||
pub async fn handle(ctx: &Context, message: &Message) -> Result<()> {
|
||||
let embeds = utils::messages::from_message(ctx, message).await?;
|
||||
pub async fn handle(ctx: &Context, http: &HttpClient, message: &Message) -> Result<()> {
|
||||
let embeds = utils::messages::from_message(ctx, http, message).await?;
|
||||
|
||||
if !embeds.is_empty() {
|
||||
let allowed_mentions = CreateAllowedMentions::new().replied_user(false);
|
||||
|
|
|
@ -23,7 +23,8 @@ pub async fn handle(
|
|||
FullEvent::Ready { data_about_bot } => {
|
||||
info!("Logged in as {}!", data_about_bot.user.name);
|
||||
|
||||
let latest_minecraft_version = api::prism_meta::latest_minecraft_version().await?;
|
||||
let latest_minecraft_version =
|
||||
api::prism_meta::latest_minecraft_version(&data.http_client).await?;
|
||||
let activity = ActivityData::playing(format!("Minecraft {latest_minecraft_version}"));
|
||||
|
||||
info!("Setting presence to activity {activity:#?}");
|
||||
|
@ -37,7 +38,7 @@ pub async fn handle(
|
|||
}
|
||||
|
||||
FullEvent::Message { new_message } => {
|
||||
trace!("Recieved message {}", new_message.content);
|
||||
trace!("Received message {}", new_message.content);
|
||||
|
||||
// ignore new messages from bots
|
||||
// note: the webhook_id check allows us to still respond to PK users
|
||||
|
@ -49,11 +50,12 @@ pub async fn handle(
|
|||
}
|
||||
|
||||
if let Some(storage) = &data.storage {
|
||||
let http = &data.http_client;
|
||||
// detect PK users first to make sure we don't respond to unproxied messages
|
||||
pluralkit::handle(ctx, new_message, storage).await?;
|
||||
pluralkit::handle(ctx, http, storage, new_message).await?;
|
||||
|
||||
if storage.is_user_plural(new_message.author.id).await?
|
||||
&& pluralkit::is_message_proxied(new_message).await?
|
||||
&& pluralkit::is_message_proxied(http, new_message).await?
|
||||
{
|
||||
debug!("Not replying to unproxied PluralKit message");
|
||||
return Ok(());
|
||||
|
@ -61,13 +63,13 @@ pub async fn handle(
|
|||
}
|
||||
|
||||
eta::handle(ctx, new_message).await?;
|
||||
expand_link::handle(ctx, new_message).await?;
|
||||
expand_link::handle(ctx, &data.http_client, new_message).await?;
|
||||
analyze_logs::handle(ctx, new_message, data).await?;
|
||||
}
|
||||
|
||||
FullEvent::ReactionAdd { add_reaction } => {
|
||||
trace!(
|
||||
"Recieved reaction {} on message {} from {}",
|
||||
"Received reaction {} on message {} from {}",
|
||||
add_reaction.emoji,
|
||||
add_reaction.message_id.to_string(),
|
||||
add_reaction.user_id.unwrap_or_default().to_string()
|
||||
|
@ -78,7 +80,7 @@ pub async fn handle(
|
|||
}
|
||||
|
||||
FullEvent::ThreadCreate { thread } => {
|
||||
trace!("Recieved thread {}", thread.id);
|
||||
trace!("Received thread {}", thread.id);
|
||||
support_onboard::handle(ctx, thread).await?;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
use crate::{api, storage::Storage};
|
||||
use crate::{
|
||||
api::{self, HttpClient},
|
||||
storage::Storage,
|
||||
};
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use eyre::Result;
|
||||
|
@ -8,19 +12,24 @@ use tokio::time::sleep;
|
|||
|
||||
const PK_DELAY: Duration = Duration::from_secs(1);
|
||||
|
||||
pub async fn is_message_proxied(message: &Message) -> Result<bool> {
|
||||
pub async fn is_message_proxied(http: &HttpClient, message: &Message) -> Result<bool> {
|
||||
trace!(
|
||||
"Waiting on PluralKit API for {} seconds",
|
||||
PK_DELAY.as_secs()
|
||||
);
|
||||
sleep(PK_DELAY).await;
|
||||
|
||||
let proxied = api::pluralkit::sender_from(message.id).await.is_ok();
|
||||
let proxied = api::pluralkit::sender_from(http, message.id).await.is_ok();
|
||||
|
||||
Ok(proxied)
|
||||
}
|
||||
|
||||
pub async fn handle(_: &Context, msg: &Message, storage: &Storage) -> Result<()> {
|
||||
pub async fn handle(
|
||||
_: &Context,
|
||||
http: &HttpClient,
|
||||
storage: &Storage,
|
||||
msg: &Message,
|
||||
) -> Result<()> {
|
||||
if msg.webhook_id.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
|
@ -36,7 +45,7 @@ pub async fn handle(_: &Context, msg: &Message, storage: &Storage) -> Result<()>
|
|||
);
|
||||
sleep(PK_DELAY).await;
|
||||
|
||||
if let Ok(sender) = api::pluralkit::sender_from(msg.id).await {
|
||||
if let Ok(sender) = api::pluralkit::sender_from(http, msg.id).await {
|
||||
storage.store_user_plurality(sender).await?;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue