From 65c6f3eedc7cfaff05c26eb285f464f55ad10acf Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Tue, 4 May 2021 18:07:56 +0200 Subject: [PATCH] uwu --- src/commands.rs | 0 src/{lib.rs => general.rs} | 7 +++++++ src/main.rs | 40 +++++++++++++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/commands.rs rename src/{lib.rs => general.rs} (91%) diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/general.rs similarity index 91% rename from src/lib.rs rename to src/general.rs index 5750971..731d373 100644 --- a/src/lib.rs +++ b/src/general.rs @@ -8,6 +8,8 @@ use serenity::model::channel::{Message, ReactionType}; use serenity::model::id::EmojiId; use toml::Value; use fancy_regex::Regex; +use crate::LastMessageInChannel; + pub static CONFIG_ERR: &'static str = "Invalid config file"; @@ -46,6 +48,11 @@ lazy_static! { #[hook] pub async fn normal_message(ctx: &Context, msg: &Message) { + + let mut data = ctx.data.write().await; + let map = data.get_mut::().expect("LastMessageInChannel not found"); + map.insert(msg.channel_id.clone(), msg.content.clone()); + lazy_static! { static ref TOM_REGEX: Regex = Regex::new(r"(?<=^|\D)(\d{6})(?=\D|$)").unwrap(); } diff --git a/src/main.rs b/src/main.rs index 7d10e8c..bb807fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ -use std::collections::HashSet; +mod general; +mod commands; + +use std::collections::{HashSet, HashMap}; use std::fs; use serenity::{ @@ -10,14 +13,20 @@ use serenity::client::Context; use serenity::framework::standard::{Args, CommandGroup, CommandResult, help_commands, HelpOptions, macros::{command, group, help}}; use serenity::framework::StandardFramework; use serenity::http::Http; -use serenity::model::id::UserId; +use serenity::model::id::{UserId, ChannelId}; use serenity::utils::{content_safe, ContentSafeOptions}; use uwuifier::uwuify_str_sse; use lazy_static::lazy_static; -use widertom::{normal_message, reply, CONFIG, CONFIG_ERR}; use rand::Rng; use toml::Value; +use crate::general::{normal_message, CONFIG_ERR, reply, CONFIG}; + +pub struct LastMessageInChannel; + +impl TypeMapKey for LastMessageInChannel { + type Value = HashMap; +} #[group] #[commands(say)] @@ -85,6 +94,11 @@ async fn main() { .await .expect("Err creating client"); + { + let mut data = client.data.write().await; + data.insert::(HashMap::default()); + } + if let Err(why) = client.start().await { println!("Client error: {:?}", why); } @@ -108,9 +122,25 @@ async fn say(ctx: &Context, msg: &Message, args: Args) -> CommandResult { } #[command] +#[description("uwuifies the arguments, or the last message in the channel if no args are supplied")] async fn uwuify(ctx: &Context, msg: &Message, args: Args) -> CommandResult { - let uwu = uwuify_str_sse(args.rest()); - msg.channel_id.say(&ctx.http, uwu).await?; + if args.is_empty() { + let mut data = ctx.data.write().await; + let map = data.get_mut::().expect("No LastMessageInChannel in TypeMap"); + let old_message = map.get(&msg.channel_id); + match old_message { + Some(s) => { + let uwu = uwuify_str_sse(s); + msg.channel_id.say(&ctx.http, uwu).await?; + } + None => { + msg.channel_id.say(&ctx.http, "Could not find last message.").await?; + } + } + } else { + let uwu = uwuify_str_sse(args.rest()); + msg.channel_id.say(&ctx.http, uwu).await?; + } Ok(()) }