html terminal
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/bot/logic.rs | 90 | ||||
| -rw-r--r-- | src/bot/mod.rs | 116 | ||||
| -rw-r--r-- | src/bot/schematic.rs | 107 | ||||
| -rw-r--r-- | src/bot/usage.md | 12 |
5 files changed, 11 insertions, 316 deletions
@@ -43,8 +43,6 @@ mindus = { version = "5.0.7", features = [], default-features = false } oxipng = { git = "https://github.com/shssoichiro/oxipng", branch = "master", default-features = false } flate2 = { version = "1.0", features = ["cloudflare_zlib"], default-features = false } strip-ansi-escapes = "0.2.0" -dashmap = "5.5.1" -lemu = { features = ["diagnose"], default-features = false, version = "0.2.0" } phf = { version = "0.11.2", features = ["macros"] } itertools = "0.11.0" diff --git a/src/bot/logic.rs b/src/bot/logic.rs deleted file mode 100644 index f31fdc9..0000000 --- a/src/bot/logic.rs +++ /dev/null @@ -1,90 +0,0 @@ -use super::{Context, Result}; -use lemu::Executor; -use poise::{serenity_prelude::AttachmentType, CodeBlock, KeyValueArgs}; -use std::borrow::Cow; - -#[poise::command(prefix_command, category = "Misc", track_edits, rename = "eval")] -pub async fn run( - ctx: Context<'_>, - #[description = "number of iterations"] kv: KeyValueArgs, - #[description = "Script"] block: CodeBlock, -) -> Result<()> { - let _ = ctx.channel_id().start_typing(&ctx.serenity_context().http); - let lemu::Output { - output: Some(output), - displays, - .. - } = (match tokio::task::spawn_blocking(move || { - Executor::with_output(vec![]) - .large_display() - .limit_iterations( - kv.get("iters") - .map_or(1, |v| v.parse::<usize>().unwrap_or(1).clamp(1, 50)), - ) - .limit_instructions(52789849) - .program(&block.code) - .map(|mut v| { - v.run(); - v.output() - }) - .map_err(|e| format!("{}", e.diagnose(&block.code)).replace("`", "\u{200b}`")) - }) - .await? - { - Ok(o) => o, - Err(e) => { - ctx.send(|c| { - c.allowed_mentions(|a| a.empty_parse()) - .content(format!("```ansi\n{e}\n```")) - }) - .await?; - return Ok(()); - } - }) - else { - unreachable!() - }; - let displays: Box<[_; 1]> = displays.try_into().unwrap(); - let [(display, _)] = *displays; - let display = if display.buffer().iter().any(|&n| n != 0) { - Some( - tokio::task::spawn_blocking(move || { - let p = oxipng::RawImage::new( - display.width(), - display.height(), - oxipng::ColorType::RGBA, - oxipng::BitDepth::Eight, - display.take_buffer(), - ) - .unwrap(); - p.create_optimized_png(&oxipng::Options::default()).unwrap() - }) - .await?, - ) - } else { - None - }; - - ctx.send(|c| { - if output.is_empty() && display.is_none() { - c.content("no output"); - } - if !output.is_empty() { - c.content(format!( - "```\n{}\n```", - String::from_utf8_lossy(&output).replace('`', "\u{200b}`") - )); - } - if let Some(display) = display { - c.attachment(AttachmentType::Bytes { - data: Cow::from(display), - filename: "display1.png".to_string(), - }) - .embed(|e| e.attachment("display1.png")); - } - c.allowed_mentions(|a| a.empty_parse()) - }) - .await?; - - Ok(()) -} diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 9d0fa5a..483d09b 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -2,16 +2,13 @@ mod admin; mod bans; mod config; mod js; -mod logic; pub mod maps; mod player; -mod schematic; mod status; mod voting; use crate::webhook::Webhook; use anyhow::Result; -use dashmap::DashMap; use maps::Maps; use poise::serenity_prelude::*; @@ -19,29 +16,18 @@ use serenity::http::Http; use serenity::model::channel::Message; use std::fmt::Write; use std::fs::read_to_string; -use std::ops::ControlFlow; use std::sync::{ atomic::{AtomicU8, Ordering}, Arc, OnceLock, }; -use std::time::Duration; use tokio::sync::broadcast; #[derive(Debug)] pub struct Data { - // message -> resp - tracker: Arc<DashMap<MessageId, Message>>, stdin: broadcast::Sender<String>, vote_data: voting::Votes, } -pub struct SMsg { - author: String, - content: String, - channel: ChannelId, - attachments: Vec<Attachment>, -} - static SKIPPING: OnceLock<(Arc<AtomicU8>, broadcast::Sender<String>)> = OnceLock::new(); #[macro_export] @@ -181,7 +167,6 @@ impl Bot { voting::list(), start(), end(), - logic::run(), help(), ], event_handler: |c, e, _, d| { @@ -198,65 +183,10 @@ impl Bot { { return Ok(()); } - if let ControlFlow::Break(m) = schematic::with( - SMsg { - author: new_message - .author_nick(c) - .await - .unwrap_or(new_message.author.name.clone()), - attachments: new_message.attachments.clone(), - content: new_message.content.clone(), - channel: new_message.channel_id, - }, - c, - ) - .await? - { - d.tracker.insert(new_message.id, m); - return Ok(()); - } if CHANNEL == new_message.channel_id.0 { say(c, new_message, d).await?; } } - poise::Event::MessageUpdate { event, .. } => { - let MessageUpdateEvent { - author: Some(author), - guild_id: Some(guild_id), - content: Some(content), - attachments: Some(attachments), - .. - } = event.clone() - else { - return Ok(()); - }; - if let Some((_, r)) = d.tracker.remove(&event.id) { - r.delete(c).await.unwrap(); - if let ControlFlow::Break(m) = schematic::with( - SMsg { - author: author - .nick_in(c, guild_id) - .await - .unwrap_or(author.name.clone()), - content, - attachments, - channel: event.channel_id, - }, - c, - ) - .await? - { - d.tracker.insert(event.id, m); - } - } - } - poise::Event::MessageDelete { - deleted_message_id, .. - } => { - if let Some((_, r)) = d.tracker.remove(deleted_message_id) { - r.delete(c).await.unwrap(); - } - } _ => {} }; Ok(()) @@ -276,30 +206,9 @@ impl Bot { .intents(GatewayIntents::all()) .setup(|ctx, _ready, framework| { Box::pin(async move { - poise::builtins::register_in_guild( - ctx, - &framework.options().commands[..18], - GuildId(GUILD), - ) - .await?; - poise::builtins::register_globally(ctx, &framework.options().commands[18..]) - .await?; + poise::builtins::register_globally(ctx, &framework.options().commands).await?; println!("registered"); - let tracker = Arc::new(DashMap::new()); - let tc = Arc::clone(&tracker); - tokio::spawn(async move { - loop { - // every 10 minutes - tokio::time::sleep(Duration::from_secs(60 * 10)).await; - tc.retain(|_, v: &mut Message| { - // prune messagees older than 3 hours - Timestamp::now().unix_timestamp() - v.timestamp.unix_timestamp() - < 60 * 60 * 3 - }); - } - }); Ok(Data { - tracker, stdin, vote_data: voting::Votes::new(vec![]), }) @@ -462,18 +371,15 @@ pub async fn help( #[autocomplete = "poise::builtins::autocomplete_command"] command: Option<String>, ) -> Result<()> { - if in_guild(ctx).await.unwrap() { - poise::builtins::help( - ctx, - command.as_deref(), - poise::builtins::HelpConfiguration { - extra_text_at_bottom: "Mindustry server management bot", - ..Default::default() - }, - ) - .await?; - } else { - ctx.say(include_str!("usage.md")).await?; - } + poise::builtins::help( + ctx, + command.as_deref(), + poise::builtins::HelpConfiguration { + extra_text_at_bottom: "Mindustry server management bot", + ..Default::default() + }, + ) + .await?; + Ok(()) } diff --git a/src/bot/schematic.rs b/src/bot/schematic.rs deleted file mode 100644 index 1c833ac..0000000 --- a/src/bot/schematic.rs +++ /dev/null @@ -1,107 +0,0 @@ -use anyhow::{anyhow, Result}; -use mindus::data::DataRead; -use mindus::*; -use oxipng::*; -use poise::serenity_prelude::*; -use regex::Regex; -use std::fmt::Write; -use std::sync::LazyLock; -use std::{borrow::Cow, ops::ControlFlow}; - -use super::{strip_colors, SMsg, 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 { - 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) else { - continue; - }; - return Ok(Some(s)); - } - } - Ok(None) -} - -pub async fn with(m: SMsg, c: &serenity::client::Context) -> Result<ControlFlow<Message, ()>> { - let author = m.author; - let send = |v: Schematic| async move { - let d = v.tags.get("description").map(|t| crate::conv::replace(t)); - let name = crate::conv::replace(&strip_colors(v.tags.get("name").unwrap())); - let cost = v.compute_total_cost().0; - let p = tokio::task::spawn_blocking(move || to_png(&v)).await?; - anyhow::Ok( - m.channel - .send_message(c, |m| { - m.add_file(AttachmentType::Bytes { - data: Cow::Owned(p), - filename: "image.png".to_string(), - }) - .embed(|e| { - e.attachment("image.png"); - d.map(|v| e.description(v)); - let mut s = String::new(); - for (i, n) in cost.iter() { - if n == 0 { - continue; - } - write!(s, "{} {n} ", crate::conv::item(i)).unwrap(); - } - e.field("req", s, true); - e.title(name) - .footer(|f| f.text(format!("requested by {author}"))) - .color(SUCCESS) - }) - }) - .await?, - ) - }; - - if let Ok(Some(v)) = from_attachments(&m.attachments).await { - println!("sent {}", v.tags.get("name").unwrap()); - return Ok(ControlFlow::Break(send(v).await?)); - } - if let Ok(v) = from_msg(&m.content) { - println!("sent {}", v.tags.get("name").unwrap()); - return Ok(ControlFlow::Break(send(v).await?)); - } - Ok(ControlFlow::Continue(())) -} - -pub fn to_png(s: &Schematic) -> Vec<u8> { - let p = s.render(); - let p = RawImage::new( - p.width(), - p.height(), - ColorType::RGB { - transparent_color: None, - }, - BitDepth::Eight, - p.take_buffer(), - ) - .unwrap(); - p.create_optimized_png(&oxipng::Options::default()).unwrap() -} - -fn from_msg(msg: &str) -> Result<Schematic> { - let schem_text = RE - .captures(msg) - .ok_or(anyhow!("couldnt find schematic"))? - .get(3) - .unwrap() - .as_str(); - Ok(Schematic::deserialize_base64(schem_text)?) -} diff --git a/src/bot/usage.md b/src/bot/usage.md deleted file mode 100644 index a036e7e..0000000 --- a/src/bot/usage.md +++ /dev/null @@ -1,12 +0,0 @@ -## Usage - -upload a file, with a msch extension (eg `24tpi_imp.msch`) and a schematic preview will be generated. -you may instead upload a message containing a base64 encoded schematic. - -## Eval - -type``` ->eval ```arm -print "xd" -``` -```for the bot to evaluate your MLOG
\ No newline at end of file |