html terminal
better filtering
| -rw-r--r-- | src/bot/mod.rs | 38 | ||||
| -rw-r--r-- | src/bot/player.rs | 6 | ||||
| -rw-r--r-- | src/webhook.rs | 61 |
3 files changed, 72 insertions, 33 deletions
diff --git a/src/bot/mod.rs b/src/bot/mod.rs index c5cfc36..e85015f 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -12,10 +12,12 @@ use anyhow::Result; use maps::Maps; use poise::serenity_prelude::*; +use regex::Regex; use serenity::http::Http; use serenity::model::channel::Message; use std::fmt::Write; use std::fs::read_to_string; +use std::sync::LazyLock; use std::sync::{ atomic::{AtomicU8, Ordering}, Arc, OnceLock, @@ -95,7 +97,7 @@ pub async fn in_guild(ctx: Context<'_>) -> Result<bool> { Ok(ctx.guild_id().map_or(false, |i| i.0 == GUILD)) } -pub async fn safe(m: &Message, c: &serenity::client::Context) -> String { +pub async fn discord_to_mindustry(m: &Message, c: &serenity::client::Context) -> String { let mut result = m.content.clone(); for u in &m.mentions { @@ -119,7 +121,37 @@ pub async fn safe(m: &Message, c: &serenity::client::Context) -> String { result = result.replace(&mention, "@deleted-role"); } } - emoji::mindustry::to_discord(&result) + + pub fn parse(x: &[u8]) -> u64 { + let mut n = 0; + for &b in x { + n = n * 10 + (b - b'0') as u64 + } + n + } + + static CHANNEL: LazyLock<Regex> = LazyLock::new(|| Regex::new("<#([0-9]+)>").unwrap()); + let mut new = result.clone(); + for m in CHANNEL.captures_iter(&result) { + new = new.replace( + m.get(0).unwrap().as_str(), + &[ + "#", + c.http() + .get_channel(parse(m.get(1).unwrap().as_str().as_bytes())) + .await + .unwrap() + .guild() + .unwrap() + .name(), + ] + .concat(), + ); + } + + static EMOJI: LazyLock<Regex> = + LazyLock::new(|| Regex::new("<a?:([a-zA-Z_]+):[0-9]+>").unwrap()); + EMOJI.replace(&new, ":$1:").into_owned() } pub async fn say(c: &serenity::client::Context, m: &Message, d: &Data) -> Result<()> { @@ -127,7 +159,7 @@ pub async fn say(c: &serenity::client::Context, m: &Message, d: &Data) -> Result .author_nick(&c.http) .await .unwrap_or_else(|| m.author.name.replace("ggfenguin", "eris")); - for l in safe(m, c).await.lines() { + for l in discord_to_mindustry(m, c).await.lines() { if send!( d.stdin, "say [royal] [coral][[[scarlet]{n}[coral]]:[white] {l}" diff --git a/src/bot/player.rs b/src/bot/player.rs index 9bd931a..489a4ec 100644 --- a/src/bot/player.rs +++ b/src/bot/player.rs @@ -94,7 +94,11 @@ pub async fn list(ctx: Context<'_>) -> Result<()> { return e.title("no players online.").color(FAIL); } e.fields(players.into_iter().map(|p| { - let admins = if p.admin { "<:admin:1182128872435749005>" } else { "" }; + let admins = if p.admin { + "<:admin:1182128872435749005>" + } else { + "" + }; (p.name, admins, true) })) .description("currently online players.") diff --git a/src/webhook.rs b/src/webhook.rs index e19825a..6b04b78 100644 --- a/src/webhook.rs +++ b/src/webhook.rs @@ -9,6 +9,8 @@ use std::sync::{ use tokio::sync::broadcast::{self, error::TryRecvError}; use tokio::time::{sleep, Duration}; +use crate::bot::strip_colors; + pub struct Webhook<'a> { pub skipped: broadcast::Sender<String>, pub skip: Arc<AtomicU8>, @@ -33,8 +35,12 @@ impl<'a> Webhook<'a> { let mut execute_webhook = ExecuteWebhook::default(); execute_webhook.allowed_mentions(|m| { m.empty_parse() - .roles(vec![1169521140998357002, 1133416252791074877]) - .users(vec![696196765564534825, 600014432298598400]) + .roles(vec![1110088946374938715, 1133416252791074877]) + .users(vec![ + 696196765564534825, + 600014432298598400, + 1173213085553660034, + ]) }); block(&mut execute_webhook); @@ -119,6 +125,18 @@ pub enum Message { Load { map: String }, } +fn mention(line: &str) -> String { + const MODS: &str = "<@&1133416252791074877>"; + const ADMINS: &str = "<@&1110088946374938715>"; + line.replace("@Moderator", MODS) + .replace("@mods", MODS) + .replace("@Administrator", ADMINS) + .replace("@admin", ADMINS) + .replace("@bendn", "<@696196765564534825>") + .replace("@nile", "<@600014432298598400>") + .replace("@proto", "<@1173213085553660034>") +} + fn get(line: &str) -> Option<Message> { macro_rules! s { ($line: expr, $($e:expr),+ $(,)?) => { @@ -146,13 +164,13 @@ fn get(line: &str) -> Option<Message> { if !(u.is_empty() || c.is_empty() || HAS_UUID.is_match(c) || HAS_UUID.is_match(u)) { if c.starts_with("/a") { return Some(Message::AdminChat { - player: unify(u), - content: unify(&emoji::mindustry::to_discord(c)), + player: u.into(), + content: mindustry_to_discord(c), }); } return Some(Message::Chat { - player: unify(u), - content: unify(&emoji::mindustry::to_discord(c)), + player: u.into(), + content: mindustry_to_discord(c), }); } } @@ -161,7 +179,7 @@ fn get(line: &str) -> Option<Message> { Regex::new(r"(.+) has (dis)?connected. \[([a-zA-Z0-9+/]{22}==)\]").unwrap() }); if let Some(captures) = JOINAGE.captures(line) { - let player = unify(captures.get(1).unwrap().as_str()); + let player = captures.get(1).unwrap().as_str().into(); return Some(if captures.get(2).is_some() { Message::Left { player } } else { @@ -172,35 +190,20 @@ fn get(line: &str) -> Option<Message> { static MAP_LOAD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"Loading map (.+)").unwrap()); if let Some(captures) = MAP_LOAD.captures(line) { return Some(Message::Load { - map: captures.get(1).unwrap().as_str().to_string(), + map: crate::bot::strip_colors(captures.get(1).unwrap().as_str()), }); } None } -pub fn unify(s: &str) -> String { - s.chars().filter(|&c| c < 'џ').collect() -} - -trait Madd { - fn madd(&mut self, line: String); - fn madd_panic(&mut self, line: &str); +pub fn mindustry_to_discord(s: &str) -> String { + strip_colors(&mention(&emoji::mindustry::to_discord(&unify(s)))) } -// cant impl addassign because no impl for other types because -impl Madd for Option<String> { - fn madd(&mut self, line: String) { - match self.take() { - Some(x) => *self = Some(x + "\n" + &line), - None => *self = Some(line), - } - } - fn madd_panic(&mut self, line: &str) { - match self.take() { - Some(x) => *self = Some(x + "\n" + line), - None => unreachable!(), - } - } +pub fn unify(s: &str) -> String { + s.chars() + .filter(|&c| c < '\u{f80}' || c > '\u{107f}') + .collect() } #[test] |