text-mods Module: re-writen code

the module now works with the actual command and not with the listener
This commit is contained in:
haruyuumei 2024-04-03 09:14:37 -03:00
parent b561d24c65
commit 6fd5f7b7fb

View file

@ -1,48 +1,39 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use crate::core::{bot_actions::*, botlog}; use crate::core::bot_actions::{actions_util, ExecBodyParams};
use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, Listener, ModulesManager}; use crate::core::botinstance::Channel;
use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule,ModulesManager};
use crate::core::identity::UserRole::*; use crate::core::identity::UserRole::*;
const OF_CMD_CHANNEL:Channel = Channel(String::new());
//use rand::{thread_rng, Rng}; //use rand::{thread_rng, Rng};
use tokio::time::{sleep, Duration};
pub async fn init(mgr: Arc<ModulesManager>) { pub async fn init(mgr: Arc<ModulesManager>) {
// Example Working BotCommand Add //DEFINING BOT COMMAND
BotCommand { let replyer = BotCommand {
module: BotModule(String::from("TextMods")), module: BotModule(String::from("TextMods")),
command: String::from("Reply"), command: String::from("reply"),
alias: vec![], alias: vec![
exec_body: actions_util::asyncbox(fors), String::from("reply1")
],
exec_body: actions_util::asyncbox(thereplyer),
help: String::from("txt mods help"), help: String::from("txt mods help"),
required_roles: vec![BotAdmin], required_roles: vec![
BotAdmin,
//I had to add Broadcaster, just the BotAdmin didn't work, it
//said I had no permission, even if being the broadcaster of the channel
Broadcaster,
Mod(OF_CMD_CHANNEL),
],
};
//ADDINNG BOT ACTION TO MODULE MANAGER
replyer.add_to_modmgr(Arc::clone(&mgr)).await;
} }
.add_to_modmgr(Arc::clone(&mgr))
.await;
Listener {
module: BotModule(String::from("TextMods")), async fn thereplyer(params : ExecBodyParams)
name: String::from("This Guy Listener"),
exec_body: actions_util::asyncbox(mods),
help: String::from(""),
}
.add_to_modmgr(Arc::clone(&mgr))
.await;
}
async fn fors(params: ExecBodyParams)
{ {
println!("Test triggered!"); // NOTE : This test function intends to print (e.g., to stdout) at fn call //getting the user message
botlog::debug(
"test triggered!",
Some("modules > tesst()".to_string()),
Some(&params.msg),
);
}
async fn mods(params: ExecBodyParams)
{
// //getting the user message
let sender_message = &params.msg.message_text; let sender_message = &params.msg.message_text;
//vector to store the words //vector to store the words
@ -57,103 +48,51 @@ async fn mods(params: ExecBodyParams)
index_words.insert(index, String::from(*word)); index_words.insert(index, String::from(*word));
} }
//if command is rw/Rw //reply message
if index_words.get(&0).unwrap().to_string() == "+reply" let mut bot_reply = String::new();
|| index_words.get(&0).unwrap().to_string() == "+Reply"
{
//do what i want with the message
//new message
let mut message:String = String::new();
//adding all other words in index words to the reply phrase
//for index starting at 1 til max indexed words
for index in 1..index_words.len() for index in 1..index_words.len()
{ {
//if the word = the word on the indexed words if let Some(word) = index_words.get(&index)
if let Some(words) = index_words.get(&index)
{ {
//add message bot_reply.push_str(word);
message.push_str(words); bot_reply.push(' ');
//add space
message.push_str(" ");
//do agane
} }
} }
//bot typing the rest of the message
let bot = Arc::clone(&params.bot); let bot = Arc::clone(&params.bot);
let botlock = bot.read().await; let botlock = bot.read().await;
// uses chat.say_in_reply_to() for the bot controls for messages // uses chat.say_in_reply_to() for the bot controls for messages
botlock botlock
.botmgrs .botmgrs
.chat .chat
.say_in_reply_to(&params.msg, .say_in_reply_to(
message, &params.msg,
params.clone()) bot_reply.clone(),
.await; params.clone()
sleep(Duration::from_secs_f64(0.5)).await; ).await;
} }
/*
TESTING AREA, WAITING FOR THREAD_RNG FIX :D
*/
// if index_words.get(&0).unwrap().to_string() == "+shuffle"
// || index_words.get(&0).unwrap().to_string() == "+Shuffle"{
// //do what i want with the message
// //new message
// let mut message:String = String::new();
// let mut rnd = thread_rng();
// let random_number = rnd.gen_range(1..=100); // //getting the user message
// //adding all other words in index words to the reply phrase // let sender_message = &params.msg.message_text;
// //for index starting at 1 til max indexed words
// for index in 1..index_words.len() // //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<usize,String> = HashMap::new();
// //adding to the hashmap each word
// for(index,word) in words.iter().enumerate()
// { // {
// //if the word = the word on the indexed words // index_words.insert(index, String::from(*word));
// if let Some(words) = index_words.get(&index)
// {
// //add message
// message.push_str(words);
// //add space
// message.push_str(" ");
// //do agane
// } // }
// }
// //bot typing the rest of the message
// let bot = Arc::clone(&params.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(&params.msg,
// message,
// params.clone())
// .await;
// sleep(Duration::from_secs_f64(0.5)).await;
// }
if words.get(0).unwrap().to_string() == "forsen"
{
//bot typing the rest of the message
let bot = Arc::clone(&params.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(&params.msg, String::from("Forsen!"), params.clone())
.await;
sleep(Duration::from_secs_f64(0.5)).await;
}
}