feat: add replyTo for bot

This commit is contained in:
dusk 2024-12-16 17:43:43 +03:00
parent ab8ce81e18
commit b2490bb337
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
3 changed files with 55 additions and 12 deletions

16
gazebot/Cargo.lock generated
View File

@ -557,6 +557,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"dotenvy", "dotenvy",
"reqwest 0.12.9", "reqwest 0.12.9",
"scc",
"serenity", "serenity",
"shuttle-runtime", "shuttle-runtime",
"shuttle-serenity", "shuttle-serenity",
@ -1730,6 +1731,15 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "scc"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94b13f8ea6177672c49d12ed964cca44836f59621981b04a3e26b87e675181de"
dependencies = [
"sdd",
]
[[package]] [[package]]
name = "schannel" name = "schannel"
version = "0.1.26" version = "0.1.26"
@ -1755,6 +1765,12 @@ dependencies = [
"untrusted", "untrusted",
] ]
[[package]]
name = "sdd"
version = "3.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "478f121bb72bbf63c52c93011ea1791dca40140dfe13f8336c4c5ac952c33aa9"
[[package]] [[package]]
name = "secrecy" name = "secrecy"
version = "0.8.0" version = "0.8.0"

View File

@ -12,3 +12,4 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
shuttle-serenity = "0.48.0" shuttle-serenity = "0.48.0"
shuttle-runtime = "0.48.0" shuttle-runtime = "0.48.0"
reqwest = { version = "0.12", features = ["json"] } reqwest = { version = "0.12", features = ["json"] }
scc = "2"

View File

@ -1,10 +1,12 @@
use reqwest::header::AUTHORIZATION; use reqwest::header::AUTHORIZATION;
use serenity::all::ActivityData; use scc::HashMap as ConcurrentHashMap;
use serenity::async_trait; use serenity::{
use serenity::json::json; all::ActivityData,
use serenity::model::channel::Message; async_trait,
use serenity::model::prelude::*; json::Value as JsonValue,
use serenity::prelude::*; model::{channel::Message, prelude::*},
prelude::*,
};
use shuttle_runtime::SecretStore; use shuttle_runtime::SecretStore;
const ADMIN_USER_ID: u64 = 853064602904166430; const ADMIN_USER_ID: u64 = 853064602904166430;
@ -12,6 +14,7 @@ const ADMIN_USER_ID: u64 = 853064602904166430;
struct Handler { struct Handler {
http: reqwest::Client, http: reqwest::Client,
secrets: SecretStore, secrets: SecretStore,
notes: ConcurrentHashMap<MessageId, String>,
} }
#[async_trait] #[async_trait]
@ -33,7 +36,22 @@ impl EventHandler for Handler {
note_content = note_content.replace(BSKY_TAG, ""); note_content = note_content.replace(BSKY_TAG, "");
} }
let note_data = json!({"content": note_content.trim(), "bskyPosse": !no_bsky_posse}); let mut note_data = serenity::json::JsonMap::new();
note_data.insert(
"content".to_string(),
JsonValue::String(note_content.trim().to_string()),
);
note_data.insert("bskyPosse".to_string(), JsonValue::Bool(!no_bsky_posse));
// add replyTo if we are replying to a previous message
if let Some(reply_msg) = msg.referenced_message.as_deref() {
if let Some(reply_note_id) = self
.notes
.read_async(&reply_msg.id, |_, v| v.to_owned())
.await
{
note_data.insert("replyTo".to_string(), JsonValue::String(reply_note_id));
}
}
let resp = self let resp = self
.http .http
.post("https://gaze.systems/log/create") .post("https://gaze.systems/log/create")
@ -52,10 +70,17 @@ impl EventHandler for Handler {
}; };
let note_resp = resp.json::<serenity::json::JsonMap>().await.unwrap(); let note_resp = resp.json::<serenity::json::JsonMap>().await.unwrap();
let created_note_id = note_resp["noteId"].as_str().expect("note id must be a string"); let created_note_id = note_resp["noteId"]
.as_str()
.expect("note id must be a string");
tracing::info!("succesfully created note with id {created_note_id}"); tracing::info!("succesfully created note with id {created_note_id}");
let mut reply_content = format!("created log at https://gaze.systems/log?id={created_note_id}"); self.notes
.upsert_async(msg.id, created_note_id.to_string())
.await;
let mut reply_content =
format!("created log at https://gaze.systems/log?id={created_note_id}");
let errors = note_resp["errors"].as_array().expect("must be array"); let errors = note_resp["errors"].as_array().expect("must be array");
if !errors.is_empty() { if !errors.is_empty() {
reply_content.push_str("\n\nerrors:"); reply_content.push_str("\n\nerrors:");
@ -90,6 +115,7 @@ async fn serenity(
.event_handler(Handler { .event_handler(Handler {
http: reqwest::Client::new(), http: reqwest::Client::new(),
secrets, secrets,
notes: ConcurrentHashMap::new(),
}) })
.await .await
.expect("Err creating client"); .expect("Err creating client");