smol bot
logging
bendn 8 months ago
parent 456949a · commit 9be0774
-rw-r--r--Cargo.toml1
-rw-r--r--src/bot/data.rs23
-rw-r--r--src/bot/logic.rs2
-rw-r--r--src/bot/map.rs9
-rw-r--r--src/bot/mod.rs26
-rw-r--r--src/bot/schematic.rs8
-rw-r--r--src/bot/sorter.rs2
7 files changed, 65 insertions, 6 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e4d4f50..2722fe8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -61,6 +61,7 @@ car = "0.1.1"
kv = "0.24.0"
sled = { version = "0.34.7", features = ["compression"] }
remapper = { version = "0.1.0", path = "../remapper" }
+implicit-fn = "0.1.0"
[features]
server = ["axum"]
diff --git a/src/bot/data.rs b/src/bot/data.rs
new file mode 100644
index 0000000..4f3e205
--- /dev/null
+++ b/src/bot/data.rs
@@ -0,0 +1,23 @@
+use serde_json::json;
+use serenity::json::Value;
+
+use crate::bot::Context;
+
+#[implicit_fn::implicit_fn]
+pub fn log(c: &Context<'_>) {
+ let v = json! {{
+ "locale": c.author().locale.as_deref().unwrap_or("unknown".into()),
+ "name": c.author().name.clone(),
+ "id": c.author().id,
+ "cname": &*c.command().name,
+ "guild": c.guild().map_or(0, |x|x.id.get()),
+ "channel": c.channel_id()
+ }};
+ push_j(v);
+}
+
+pub fn push_j(j: Value) {
+ let mut f = std::fs::File::options().append(true).open("data").unwrap();
+ use std::io::Write;
+ writeln!(f, "{}", serde_json::to_string(&j).unwrap()).unwrap();
+}
diff --git a/src/bot/logic.rs b/src/bot/logic.rs
index e299c08..ad8880a 100644
--- a/src/bot/logic.rs
+++ b/src/bot/logic.rs
@@ -12,6 +12,7 @@ pub async fn run_file(
#[description = "logic, txt"] mlog: Attachment,
#[description = "number of iterations (0–50)"] iterations: Option<u8>,
) -> Result<()> {
+ super::log(&ctx);
ctx.defer().await?;
let bytes = mlog.download().await?;
let Ok(code) = String::from_utf8(bytes) else {
@@ -41,6 +42,7 @@ pub async fn run(
#[description = "number of iterations"] kv: KeyValueArgs,
#[description = "Script"] block: CodeBlock,
) -> Result<()> {
+ super::log(&ctx);
match exec(
block.code,
kv.get("iters")
diff --git a/src/bot/map.rs b/src/bot/map.rs
index 6642860..76023e0 100644
--- a/src/bot/map.rs
+++ b/src/bot/map.rs
@@ -115,6 +115,14 @@ pub async fn with(msg: &Message, c: &serenity::client::Context) -> Result<()> {
let t = msg.channel_id.start_typing(&c.http);
let (png, embed) = embed(m, deser_took).await;
t.stop();
+ super::data::push_j(serde_json::json! {{
+ "locale": msg.author.locale.as_deref().unwrap_or("no locale"),
+ "name": msg.author.name,
+ "id": msg.author.id,
+ "cname": "map message input",
+ "guild": msg.guild_id.map_or(0,|x|x.get()),
+ "channel": msg.channel_id.get(),
+ }});
msg.channel_id
.send_message(c, CreateMessage::new().add_file(png).embed(embed))
.await?;
@@ -151,6 +159,7 @@ async fn embed(m: Map, deser_took: Duration) -> (CreateAttachment, CreateEmbed)
)]
/// Renders map inside a message.
pub async fn render_message(c: super::Context<'_>, m: Message) -> Result<()> {
+ super::log(&c);
let Some((_auth, m, deser_took)) = find(&m, c.serenity_context()).await? else {
poise::say_reply(c, "no map").await?;
return Ok(());
diff --git a/src/bot/mod.rs b/src/bot/mod.rs
index 4a05ee0..1044e87 100644
--- a/src/bot/mod.rs
+++ b/src/bot/mod.rs
@@ -6,6 +6,9 @@ mod schematic;
pub mod search;
mod sorter;
mod db;
+mod data;
+pub use data::log;
+
use crate::emoji;
use anyhow::Result;
@@ -56,6 +59,9 @@ pub struct Data {
pub struct Msg {
avatar: String,
author: String,
+ locale: String,
+ author_id: u64,
+ guild: u64,
content: String,
channel: ChannelId,
attachments: Vec<Attachment>,
@@ -222,12 +228,12 @@ async fn handle_message(
.await
.unwrap_or(new_message.author.name.clone());
let post = EXTRA.get(&new_message.channel_id.get()).map(|x| x.clone());
- if post.is_some() {
- println!("recv message on thread")
- }
let (dir, l, repo) = sep(SPECIAL.get(&new_message.channel_id.get()).or(post.as_ref()));
let m = Msg {
author: who.clone(),
+ locale: new_message.author.locale.clone().unwrap_or("unknown locale".to_string()),
+ author_id: new_message.author.id.get(),
+ guild: new_message.guild_id.map_or(0,Into::into),
avatar: new_message.author.avatar_url().unwrap_or(CAT.to_string()),
attachments: new_message.attachments.clone(),
content: new_message.content.clone(),
@@ -412,9 +418,12 @@ impl Bot {
let (dir, l, repo) = sep(SPECIAL.get(&r.channel_id.get()));
if let ControlFlow::Break((m,_,s)) = schematic::with(
Msg {
+ locale:author.locale.clone().unwrap_or("unknown locale".to_string()),
+ author_id: author.id.get(),
avatar: author.avatar_url().unwrap_or(CAT.to_string()),
author: who.clone(),
content:content.clone(),
+ guild: r.guild_id.map_or(0,Into::into),
attachments:attachments.clone(),
channel: *channel_id,
},
@@ -803,6 +812,7 @@ pub async fn help(
#[autocomplete = "poise::builtins::autocomplete_command"]
command: Option<String>,
) -> Result<()> {
+ log(&ctx);
macro_rules! pick {
($e:literal, $u:literal) => {
if matches!(
@@ -858,6 +868,7 @@ pub fn png(p: fimg::Image<Vec<u8>, 3>) -> Vec<u8> {
)]
/// Pong!
pub async fn ping(c: Context<'_>) -> Result<()> {
+ log(&c);
use emoji::named::*;
let m = memory_stats::memory_stats().unwrap().physical_mem as f32 / (1 << 20) as f32;
@@ -894,6 +905,7 @@ pub async fn ping(c: Context<'_>) -> Result<()> {
)]
/// Renders base64 schematic.
pub async fn render(c: Context<'_>, #[description = "schematic, base64"] s: String) -> Result<()> {
+ log(&c);
poise::send_reply(c,
match schematic::from_b64(&s) {
Ok(s) =>
@@ -921,6 +933,7 @@ pub async fn render_file(
c: Context<'_>,
#[description = "map / schematic, msch"] s: Attachment,
) -> Result<()> {
+ log(&c);
_ = c.defer().await;
let Some(s) = schematic::from_attachments(std::slice::from_ref(&s)).await? else {
@@ -956,6 +969,7 @@ pub async fn render_file(
#[poise::command(slash_command)]
/// Rename a schematic.
async fn rename_file(c: Context<'_>, #[description = "schematic, msch"] s: Attachment, #[description = "new name"] name:String) -> Result<()> {
+ log(&c);
let Some(schematic::Schem{schem: mut s}) = schematic::from_attachments(std::slice::from_ref(&s)).await? else {
c.reply("no schem!").await?;
return Ok(());
@@ -973,7 +987,7 @@ async fn rename_file(c: Context<'_>, #[description = "schematic, msch"] s: Attac
#[poise::command(slash_command)]
/// Rename a schematic.
async fn rename(c: Context<'_>, #[description = "schematic, base64"] s: String, #[description = "new name"] name:String) -> Result<()> {
- let Ok(schematic::Schem{schem: mut s}) = schematic::from_b64(&*s) else {
+ log(&c);let Ok(schematic::Schem{schem: mut s}) = schematic::from_b64(&*s) else {
c.reply("no schem!").await?;
return Ok(());
};
@@ -995,7 +1009,7 @@ async fn rename(c: Context<'_>, #[description = "schematic, base64"] s: String,
)]
/// Renders schematic inside a message.
pub async fn render_message(c: Context<'_>, m: Message) -> Result<()> {
- poise::send_reply(
+ log(&c);poise::send_reply(
c, match schematic::from((&m.content, &m.attachments)).await {
Ok(Some(s)) =>
schematic::reply(
@@ -1026,7 +1040,7 @@ pub async fn render_message(c: Context<'_>, m: Message) -> Result<()> {
)]
/// Instructions on adding a schematic repository to YOUR server!
pub async fn schembrowser_instructions(c: Context<'_>) -> Result<()> {
- poise::send_reply(
+ log(&c);poise::send_reply(
c,
poise::CreateReply::default()
.content(include_str!("repo.md"))
diff --git a/src/bot/schematic.rs b/src/bot/schematic.rs
index 7fc4023..fd9bd04 100644
--- a/src/bot/schematic.rs
+++ b/src/bot/schematic.rs
@@ -145,6 +145,14 @@ pub async fn with(
labels: Option<String>,
) -> Result<ControlFlow<(Message, String, Schem), ()>> {
if let Ok(Some(mut v)) = from((&m.content, &m.attachments)).await {
+ super::data::push_j(serde_json::json! {{
+ "locale": m.locale,
+ "name": m.author,
+ "id": m.author,
+ "cname": "schematic message input",
+ "guild": m.guild,
+ "channel": m.channel.get(),
+ }});
if let Some(x) = labels {
v.schem.tags.insert("labels".into(), x);
};
diff --git a/src/bot/sorter.rs b/src/bot/sorter.rs
index b0a68f2..8826b97 100644
--- a/src/bot/sorter.rs
+++ b/src/bot/sorter.rs
@@ -142,6 +142,7 @@ pub async fn sorter(
#[description = "scaling algorithm, defaults to nearest"] algorithm: Option<Scaling>,
#[description = "dithering algorithm, defaults to none"] dithered: Option<Dithering>,
) -> Result<()> {
+ super::log(&c);
c.defer().await?;
let image = i.download().await?;
match image::load_from_memory(&image) {
@@ -195,6 +196,7 @@ pub async fn mapper(
#[description = "dithering algorithm, defaults to none (if you want the map to be playable, go with ordered)"]
dithered: Option<Dithering>,
) -> Result<()> {
+ super::log(&c);
c.defer().await?;
let image = i.download().await?;
match image::load_from_memory(&image) {