diff --git a/Cargo.lock b/Cargo.lock index eeb57f7..4541c11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -314,6 +314,7 @@ dependencies = [ "sqlx", "tokio", "unicode-segmentation", + "url", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c8e6d70..26fb6f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ rand = "0.7" unicode-segmentation = "1.6" log = "0.4" env_logger = "0.7" -lazy_static = "1.4" \ No newline at end of file +lazy_static = "1.4" +url = "2.1" \ No newline at end of file diff --git a/src/desu.jpg b/res/desu.jpg similarity index 100% rename from src/desu.jpg rename to res/desu.jpg diff --git a/res/mocking-spongebob.jpg b/res/mocking-spongebob.jpg new file mode 100644 index 0000000..a3d0a8c Binary files /dev/null and b/res/mocking-spongebob.jpg differ diff --git a/src/commands/crosspost.rs b/src/commands/crosspost.rs index 6521171..4cb301b 100644 --- a/src/commands/crosspost.rs +++ b/src/commands/crosspost.rs @@ -42,6 +42,7 @@ async fn crosspost(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul msg.author, args.rest() ); + for channel in channels { debug_channel_say(channel, msg.author.id, ctx, &rest).await?; } diff --git a/src/commands/mock.rs b/src/commands/mock.rs new file mode 100644 index 0000000..0a817cd --- /dev/null +++ b/src/commands/mock.rs @@ -0,0 +1,50 @@ +use crate::util::{debug_say, is_bot_owner}; +use rand::thread_rng; +use rand::Rng; +use serenity::framework::standard::{macros::command, CommandResult}; +use serenity::model::channel::Message; +use serenity::prelude::Context; +use url::Url; + +#[command] +async fn mock(ctx: &Context, msg: &Message) -> CommandResult { + if is_bot_owner(msg.author.id) { + let prev_msg = msg + .channel_id + .messages(ctx, |retriever| retriever.before(msg.id).limit(1)) + .await? + .pop(); + let mut content: String = prev_msg.unwrap().content_safe(ctx).await; + for word in content.clone().split_whitespace() { + if let Err(_) = Url::parse(word) { + content = (&content).replacen(word, &random_uppercase(word), 1); + } + } + // debug_say(msg, ctx, content).await?; + msg.channel_id + .send_message(ctx, |m| { + m.content(content); + m.add_file(serenity::http::AttachmentType::Bytes { + data: std::borrow::Cow::from( + &include_bytes!("../../res/mocking-spongebob.jpg")[..], + ), + filename: "spongemock.jpg".to_string(), + }) + }) + .await?; + } + Ok(()) +} + +fn random_uppercase(word: &str) -> String { + let mut rng = thread_rng(); + word.chars() + .map(|c| { + if rng.gen::() { + c.to_uppercase().collect::() + } else { + c.to_lowercase().collect() + } + }) + .collect() +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e1c0d0e..aac05a0 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,6 @@ use crate::commands::{ clap::CLAP_COMMAND, crosspost::CROSSPOST_COMMAND, cube::CUBE_COMMAND, heck::HECK_COMMAND, - source::SOURCE_COMMAND, + mock::MOCK_COMMAND, source::SOURCE_COMMAND, }; use serenity::framework::standard::macros::group; @@ -8,8 +8,9 @@ mod clap; mod crosspost; mod cube; mod heck; +mod mock; mod source; #[group] -#[commands(heck, clap, cube, source, crosspost)] +#[commands(heck, clap, cube, source, crosspost, mock)] pub(crate) struct General; diff --git a/src/passive/desu.rs b/src/passive/desu.rs index bf09215..b4e2cd8 100644 --- a/src/passive/desu.rs +++ b/src/passive/desu.rs @@ -72,7 +72,7 @@ const DESU_ACTIONS: &[DesuAction>>] = &|ctx, msg| { Box::pin(msg.channel_id.send_message(ctx, |m| { m.add_file(serenity::http::AttachmentType::Bytes { - data: std::borrow::Cow::from(&include_bytes!("../desu.jpg")[..]), + data: std::borrow::Cow::from(&include_bytes!("../../res/desu.jpg")[..]), filename: "desu.jpg".to_string(), }) })) diff --git a/src/util/mod.rs b/src/util/mod.rs index 217fd32..ea46071 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,5 +1,6 @@ use lazy_static::lazy_static; use serenity::{ + builder::CreateMessage, http::Http, model::{channel::Message, id::ChannelId}, }; @@ -45,3 +46,35 @@ pub async fn debug_channel_say( Ok(None) } } + +// pub async fn debug_send_message<'a, F>( +// channel: impl Into, +// author_id: impl Into, +// http: impl AsRef, +// f: F, +// ) -> Result, serenity::Error> +// where +// for<'b> F: FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a>, +// { +// let channel: ChannelId = channel.into(); +// if cfg!(debug_assertions) && author_id.into() == *BOT_OWNER_ID { +// Ok(Some( +// channel +// .send_message(http, |mut m| { +// let resp = f(m); +// // let cur_content = resp.0.get("content"); + +// f(m) +// }) +// .await?, +// )) +// } else if !cfg!(debug_assertions) { +// Ok(Some(channel.send_message(http, f).await?)) +// } else { +// Ok(None) +// } +// } + +pub fn is_bot_owner(id: impl Into) -> bool { + id.into() == *BOT_OWNER_ID +}