-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(intelligence): trigger by direct mention
- Loading branch information
Showing
14 changed files
with
124 additions
and
73 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
pub mod autoreply; | ||
pub mod intelligence; | ||
pub mod owo; | ||
pub mod shiggy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::time::Duration; | ||
|
||
use color_eyre::eyre::Result; | ||
use poise::serenity_prelude as serenity; | ||
use tokio::{task, time}; | ||
|
||
use crate::intelligence; | ||
|
||
#[tracing::instrument(skip_all, fields(message_id = message.id.get()))] | ||
pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Result<()> { | ||
if message | ||
.flags | ||
.is_some_and(|flags| flags.contains(serenity::MessageFlags::SUPPRESS_NOTIFICATIONS)) | ||
{ | ||
return Ok(()); | ||
} | ||
|
||
if let Some(query) = message | ||
.content | ||
.strip_prefix(&format!("<@{}>", ctx.cache.current_user().id)) | ||
.map(|s| s.trim()) | ||
{ | ||
if query.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
let typing_task = task::spawn({ | ||
let http = ctx.http.clone(); | ||
let channel = message.channel_id; | ||
|
||
async move { | ||
let mut interval = time::interval(Duration::from_secs(10)); | ||
|
||
loop { | ||
interval.tick().await; | ||
let _ = http.broadcast_typing(channel).await; | ||
} | ||
} | ||
}); | ||
|
||
let username = message.author.tag(); | ||
let display_name = message.author.global_name.clone().map(|s| s.into_string()); | ||
|
||
let nick = message | ||
.member | ||
.as_ref() | ||
.and_then(|m| m.nick.as_ref().map(|s| s.to_owned().into_string())); | ||
|
||
let resp = intelligence::query(intelligence::Request { | ||
query: query.to_owned(), | ||
metadata: intelligence::RequestMetadata { | ||
username, | ||
display_name, | ||
nick, | ||
}, | ||
}) | ||
.await?; | ||
|
||
message.reply_ping(&ctx.http, resp).await?; | ||
typing_task.abort(); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::env; | ||
|
||
use color_eyre::eyre::{eyre, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::reqwest_client::HTTP; | ||
|
||
static API_URL: &str = "https://intelligence.valfisk.ryanccn.dev/v2"; | ||
|
||
#[derive(Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RequestMetadata { | ||
pub username: String, | ||
pub display_name: Option<String>, | ||
pub nick: Option<String>, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct Request { | ||
pub query: String, | ||
pub metadata: RequestMetadata, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct Response { | ||
pub response: String, | ||
} | ||
|
||
pub async fn query(request: Request) -> Result<String> { | ||
let secret = env::var("INTELLIGENCE_SECRET") | ||
.map_err(|_| eyre!("Valfisk Intelligence API secret is not set!"))?; | ||
|
||
let resp: Response = HTTP | ||
.post(API_URL) | ||
.bearer_auth(secret) | ||
.json(&request) | ||
.send() | ||
.await? | ||
.error_for_status()? | ||
.json() | ||
.await?; | ||
|
||
Ok(resp.response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters