smol bot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use anyhow::{anyhow, Result};
use mindus::data::DataRead;
use mindus::*;
use poise::serenity_prelude::*;
use regex::Regex;
use std::fmt::Write;
use std::ops::ControlFlow;
use std::sync::LazyLock;

use super::{strip_colors, Msg, SUCCESS};

static RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(```)?(\n)?([^`]+)(\n)?(```)?").unwrap());

async fn from_attachments(attchments: &[Attachment]) -> Result<Option<Schematic>> {
    for a in attchments {
        if a.filename.ends_with("msch") {
            let s = a.download().await?;
            let mut s = DataRead::new(&s);
            let Ok(s) = Schematic::deserialize(&mut s) else {
                println!("failed to read {}", a.filename);
                continue;
            };
            return Ok(Some(s));
        // discord uploads base64 as a file when its too long
        } else if a.filename == "message.txt" {
            let Ok(s) = String::from_utf8(a.download().await?) else {
                continue;
            };
            let Ok(s) = Schematic::deserialize_base64(s.trim()) else {
                println!("failed to read msg.txt");
                continue;
            };
            return Ok(Some(s));
        }
    }
    Ok(None)
}

pub async fn with(
    m: Msg,
    c: &serenity::client::Context,
) -> Result<ControlFlow<(Message, String), ()>> {
    let author = m.author;
    let send = |v: Schematic| async move {
        let d = v
            .tags
            .get("description")
            .map(|t| emoji::mindustry::to_discord(&strip_colors(t)));
        let name = emoji::mindustry::to_discord(&strip_colors(v.tags.get("name").unwrap()));
        let cost = v.compute_total_cost().0;
        println!("deser {name}");
        let p = tokio::task::spawn_blocking(move || to_png(&v)).await?;
        println!("rend {name}");
        anyhow::Ok((
            m.channel
                .send_message(
                    c,
                    CreateMessage::new()
                        .add_file(CreateAttachment::bytes(p, "image.png"))
                        .embed({
                            let mut e = CreateEmbed::new().attachment("image.png");
                            if let Some(v) = d {
                                e = e.description(v);
                            }
                            let mut s = String::new();
                            for (i, n) in cost.iter() {
                                if n == 0 {
                                    continue;
                                }
                                write!(s, "{} {n} ", emoji::mindustry::item(i)).unwrap();
                            }
                            e.field("req", s, true)
                                .title(name.clone())
                                .footer(CreateEmbedFooter::new(format!("requested by {author}")))
                                .color(SUCCESS)
                        }),
                )
                .await?,
            name,
        ))
    };

    if let Ok(Some(v)) = from_attachments(&m.attachments).await {
        return Ok(ControlFlow::Break(send(v).await?));
    }
    if let Ok(v) = from_msg(&m.content) {
        return Ok(ControlFlow::Break(send(v).await?));
    }
    Ok(ControlFlow::Continue(()))
}

pub fn to_png(s: &Schematic) -> Vec<u8> {
    super::png(s.render())
}

fn from_msg(msg: &str) -> Result<Schematic> {
    let schem_text = RE
        .captures(msg)
        .ok_or(anyhow!("couldnt find schematic"))?
        .get(3)
        .unwrap()
        .as_str()
        .trim();
    Ok(Schematic::deserialize_base64(schem_text)?)
}