Random and true equal

This commit is contained in:
max 2024-02-07 22:47:37 +00:00
parent 358fe2a9ce
commit 3fc7b0d2dc
2 changed files with 20 additions and 7 deletions

View File

@ -7,7 +7,6 @@ use serenity::{
client::Context, client::Context,
}; };
use tokio::fs::File; use tokio::fs::File;
use walkdir::{DirEntry, WalkDir};
use crate::{ use crate::{
config::ConfigGlobal, config::ConfigGlobal,
@ -19,7 +18,12 @@ pub struct Handler;
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message) {
if msg.author.bot || msg.content.starts_with("!") { let chan_id: u64 = 675046560219791378;
if msg.author.bot
|| msg.content.starts_with("!")
|| !msg.channel_id.eq(&chan_id)
|| msg.content.len() == 0
{
return; return;
} }
if msg.content == "&ping" { if msg.content == "&ping" {
@ -40,7 +44,10 @@ impl EventHandler for Handler {
.clone(); .clone();
(config_img, config) (config_img, config)
}; };
let mut rng = rand::thread_rng();
if config_img.keyword.len() == 0 || msg.content.len() > 50 {
return;
}
let folder_container = match config_img let folder_container = match config_img
.keyword .keyword
.iter() .iter()
@ -51,7 +58,10 @@ impl EventHandler for Handler {
let keyword_path = match keyword_matching.path.len() { let keyword_path = match keyword_matching.path.len() {
0 => keyword_matching.path[0].clone(), 0 => keyword_matching.path[0].clone(),
_ => { _ => {
let id = rng.gen_range(0..keyword_matching.path.len()); let id: usize = {
let mut rng = rand::thread_rng();
rng.gen_range(0..keyword_matching.path.len())
};
keyword_matching.path[id].clone() keyword_matching.path[id].clone()
} }
}; };
@ -62,12 +72,15 @@ impl EventHandler for Handler {
}; };
let path = format!("{}/{}", config.image.path.clone(), folder_container); let path = format!("{}/{}", config.image.path.clone(), folder_container);
let file_folder = KeyWordItem::output_folder_content(path.clone()); let file_folder = KeyWordItem::output_folder_content(path.clone());
let id_rand: usize = rng.gen_range(0..file_folder.len()); let id_rand: usize = {
let mut rng = rand::thread_rng();
rng.gen_range(0..file_folder.len())
};
let filename = match file_folder.get(id_rand) { let filename = match file_folder.get(id_rand) {
Some(file) => file.file_name().to_str().unwrap(), Some(file) => file.file_name().to_str().unwrap(),
None => return, None => return,
}; };
let file_path = format!("{}/{}", path, filename.clone()); let file_path = format!("{}/{}", path, filename);
let file = match File::open(file_path).await { let file = match File::open(file_path).await {
Ok(file) => file, Ok(file) => file,
Err(why) => { Err(why) => {

View File

@ -14,7 +14,7 @@ impl KeyWordItem {
pub fn does_value_match(&self, haystack: String) -> bool { pub fn does_value_match(&self, haystack: String) -> bool {
self.value.clone().into_iter().any(|val| { self.value.clone().into_iter().any(|val| {
let re = Regex::new(&val).unwrap(); let re = Regex::new(&val).unwrap();
re.is_match(&haystack) re.replace_all(&haystack, "") == ""
}) })
} }
pub fn output_folder_content(path: String) -> Vec<DirEntry> { pub fn output_folder_content(path: String) -> Vec<DirEntry> {