smol bot
file finder
| -rw-r--r-- | src/bot/mod.rs | 4 | ||||
| -rw-r--r-- | src/bot/search.rs | 64 |
2 files changed, 48 insertions, 20 deletions
diff --git a/src/bot/mod.rs b/src/bot/mod.rs index b1e672f..3a229be 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -159,7 +159,7 @@ impl Bot { std::env::var("TOKEN").unwrap_or_else(|_| read_to_string("token").expect("wher token")); let f: poise::Framework<Data, anyhow::Error> = poise::Framework::builder() .options(poise::FrameworkOptions { - commands: vec![logic::run(), help(), search::search(),search::find()], + commands: vec![logic::run(), help(), search::search() ,search::find(), search::file()], event_handler: |c, e, _, d| { Box::pin(async move { match e { @@ -315,7 +315,7 @@ impl Bot { .setup(|ctx, _ready, _| { Box::pin(async move { poise::builtins::register_globally(ctx, &[logic::run(), help()]).await?; - poise::builtins::register_in_guild(ctx, &[search::search(),search::find()], 925674713429184564.into()).await?; + poise::builtins::register_in_guild(ctx, &[search::search(), search::find(), search::file()], 925674713429184564.into()).await?; println!("registered"); let tracker = Arc::new(DashMap::new()); let tc = Arc::clone(&tracker); diff --git a/src/bot/search.rs b/src/bot/search.rs index 0a05dce..21a9f9b 100644 --- a/src/bot/search.rs +++ b/src/bot/search.rs @@ -4,7 +4,7 @@ use mindus::data::DataRead; use mindus::{Schematic, Serializable}; use poise::serenity_prelude::*; use std::mem::MaybeUninit; -use std::path::Path; +use std::path::{Path, PathBuf}; struct Dq<T, const N: usize> { arr: [MaybeUninit<T>; N], @@ -48,6 +48,29 @@ impl<T: Copy, const N: usize> Dq<T, N> { } #[poise::command(slash_command)] +/// Find a schematic by file +pub async fn file( + c: super::Context<'_>, + #[description = "schematic file name"] file: String, +) -> Result<()> { + let Some((file, ch)) = files().find(|(x, _)| x.file_name().unwrap().to_string_lossy() == file) + else { + return c + .say(format!("{CANCEL} not found")) + .await + .map(|_| ()) + .map_err(Into::into); + }; + c.say(format!( + "{RIGHT} https://discord.com/channels/925674713429184564/{ch}/{}", + flake(&file.file_name().unwrap().to_string_lossy()) + )) + .await + .map(|_| ()) + .map_err(Into::into) +} + +#[poise::command(slash_command)] /// Find a schematic in the repo pub async fn find( c: super::Context<'_>, @@ -97,7 +120,7 @@ pub struct Data { message: u64, } -pub fn schems() -> impl Iterator<Item = (Schematic, Data)> { +pub fn files() -> impl Iterator<Item = (PathBuf, u64)> { super::SPECIAL .entries() .filter_map(|(&ch, &dir)| { @@ -105,25 +128,30 @@ pub fn schems() -> impl Iterator<Item = (Schematic, Data)> { .ok() .map(|x| (x, ch)) }) - .map(|(fs, channel)| { - fs.filter_map(Result::ok).map(move |f| { - let dat = std::fs::read(f.path()).unwrap(); - let mut dat = DataRead::new(&dat); - let ts = Schematic::deserialize(&mut dat).unwrap(); - let p = f.path(); - let x = p.file_name().unwrap().to_string_lossy(); - ( - ts, - Data { - channel, - message: (u64::from_str_radix(&x[..x.len() - 5], 16).unwrap()), - }, - ) - }) - }) + .map(|(fs, channel)| fs.filter_map(Result::ok).map(move |f| (f.path(), channel))) .flatten() } +pub fn schems() -> impl Iterator<Item = (Schematic, Data)> { + files().map(|(f, channel)| { + let dat = std::fs::read(&f).unwrap(); + let mut dat = DataRead::new(&dat); + let ts = Schematic::deserialize(&mut dat).unwrap(); + let x = f.file_name().unwrap().to_string_lossy(); + ( + ts, + Data { + channel, + message: flake(&x), + }, + ) + }) +} + +pub fn flake(x: &str) -> u64 { + u64::from_str_radix(&x[..x.len() - 5], 16).unwrap() +} + #[poise::command(slash_command)] /// Search for a schematic in the repo pub async fn search( |