This commit is contained in:
nora 2021-05-04 18:07:56 +02:00
parent 456584c596
commit 65c6f3eedc
3 changed files with 42 additions and 5 deletions

0
src/commands.rs Normal file
View file

View file

@ -8,6 +8,8 @@ use serenity::model::channel::{Message, ReactionType};
use serenity::model::id::EmojiId; use serenity::model::id::EmojiId;
use toml::Value; use toml::Value;
use fancy_regex::Regex; use fancy_regex::Regex;
use crate::LastMessageInChannel;
pub static CONFIG_ERR: &'static str = "Invalid config file"; pub static CONFIG_ERR: &'static str = "Invalid config file";
@ -46,6 +48,11 @@ lazy_static! {
#[hook] #[hook]
pub async fn normal_message(ctx: &Context, msg: &Message) { pub async fn normal_message(ctx: &Context, msg: &Message) {
let mut data = ctx.data.write().await;
let map = data.get_mut::<LastMessageInChannel>().expect("LastMessageInChannel not found");
map.insert(msg.channel_id.clone(), msg.content.clone());
lazy_static! { lazy_static! {
static ref TOM_REGEX: Regex = Regex::new(r"(?<=^|\D)(\d{6})(?=\D|$)").unwrap(); static ref TOM_REGEX: Regex = Regex::new(r"(?<=^|\D)(\d{6})(?=\D|$)").unwrap();
} }

View file

@ -1,4 +1,7 @@
use std::collections::HashSet; mod general;
mod commands;
use std::collections::{HashSet, HashMap};
use std::fs; use std::fs;
use serenity::{ 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::standard::{Args, CommandGroup, CommandResult, help_commands, HelpOptions, macros::{command, group, help}};
use serenity::framework::StandardFramework; use serenity::framework::StandardFramework;
use serenity::http::Http; use serenity::http::Http;
use serenity::model::id::UserId; use serenity::model::id::{UserId, ChannelId};
use serenity::utils::{content_safe, ContentSafeOptions}; use serenity::utils::{content_safe, ContentSafeOptions};
use uwuifier::uwuify_str_sse; use uwuifier::uwuify_str_sse;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use widertom::{normal_message, reply, CONFIG, CONFIG_ERR};
use rand::Rng; use rand::Rng;
use toml::Value; use toml::Value;
use crate::general::{normal_message, CONFIG_ERR, reply, CONFIG};
pub struct LastMessageInChannel;
impl TypeMapKey for LastMessageInChannel {
type Value = HashMap<ChannelId, String>;
}
#[group] #[group]
#[commands(say)] #[commands(say)]
@ -85,6 +94,11 @@ async fn main() {
.await .await
.expect("Err creating client"); .expect("Err creating client");
{
let mut data = client.data.write().await;
data.insert::<LastMessageInChannel>(HashMap::default());
}
if let Err(why) = client.start().await { if let Err(why) = client.start().await {
println!("Client error: {:?}", why); println!("Client error: {:?}", why);
} }
@ -108,9 +122,25 @@ async fn say(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
} }
#[command] #[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 { async fn uwuify(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let uwu = uwuify_str_sse(args.rest()); if args.is_empty() {
msg.channel_id.say(&ctx.http, uwu).await?; let mut data = ctx.data.write().await;
let map = data.get_mut::<LastMessageInChannel>().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(()) Ok(())
} }