From 6a71e3375beb4177f2090d0bbeaf02d76b21a0cf Mon Sep 17 00:00:00 2001 From: haruyuumei Date: Sat, 6 Apr 2024 19:36:24 -0300 Subject: [PATCH] Added butt command - still works to do Working on a "change the text" command/listener --- src/custom/text_mods.rs | 98 ++++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/src/custom/text_mods.rs b/src/custom/text_mods.rs index 9292db7..8960476 100644 --- a/src/custom/text_mods.rs +++ b/src/custom/text_mods.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; +use rand::{thread_rng, Rng}; + use crate::core::bot_actions::{actions_util, ExecBodyParams}; use crate::core::botinstance::Channel; use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, Listener, ModulesManager}; @@ -36,8 +38,66 @@ pub async fn init(mgr: Arc) { help:String::from("Forsen helper"), }; forsen_listener.add_to_modmgr(Arc::clone(&mgr)).await; + + + let butt = BotCommand{ + module:BotModule(String::from("ButtModule")), + command:String::from("butt"), + alias:vec![], + exec_body:actions_util::asyncbox(butt), + help:String::from("butt helper"), + required_roles:vec![ + BotAdmin, + Broadcaster, + Mod(OF_CMD_CHANNEL), + ] + }; + butt.add_to_modmgr(Arc::clone(&mgr)).await; + } + async fn butt(params : ExecBodyParams) + { + //getting User message + let mut userm = usermsg(¶ms); + + //choose a random word from the user phrase + let w = thread_rng().gen_range(1..userm.len()); + + //change the random word to butt :pepepains + userm.insert(w, String::from("Butt")); + + + let mut bot_reply = String::new(); + + for index in 1..userm.len() + { + if let Some(word) = userm.get(&index) + { + bot_reply.push_str(word); + bot_reply.push(' '); + } + } + + let bot = Arc::clone(¶ms.bot); + let botlock = bot.read().await; + + // uses chat.say_in_reply_to() for the bot controls for messages + botlock + .botmgrs + .chat + .say_in_reply_to( + ¶ms.msg, + bot_reply, + params.clone() + ).await; + + } + + + + + async fn forsenforsen(params : ExecBodyParams) { if params.msg.message_text == "forsen" || params.msg.message_text == "Forsen" @@ -59,27 +119,13 @@ pub async fn init(mgr: Arc) { async fn thereplyer(params : ExecBodyParams) { - //getting the user message - let sender_message = ¶ms.msg.message_text; - - //vector to store the words - let words: Vec<&str> = sender_message.split_whitespace().collect(); - - //using Hashmap to store the words - let mut index_words:HashMap = HashMap::new(); - - //adding to the hashmap each word - for(index,word) in words.iter().enumerate() - { - index_words.insert(index, String::from(*word)); - } - + let user: HashMap = usermsg(¶ms); //reply message let mut bot_reply = String::new(); - for index in 1..index_words.len() + for index in 1..user.len() { - if let Some(word) = index_words.get(&index) + if let Some(word) = user.get(&index) { bot_reply.push_str(word); bot_reply.push(' '); @@ -102,7 +148,25 @@ pub async fn init(mgr: Arc) { } +pub fn usermsg(params : &ExecBodyParams) -> HashMap +{ + //getting the user message + let sender_message = ¶ms.msg.message_text; + //vector to store the words + let words: Vec<&str> = sender_message.split_whitespace().collect(); + + //using Hashmap to store the words + let mut index_words:HashMap = HashMap::new(); + + //adding to the hashmap each word + for(index,word) in words.iter().enumerate() + { + index_words.insert(index, String::from(*word)); + } + index_words + +}