feat: mise en place commande Answer
This commit is contained in:
parent
9c0b011ef2
commit
fe75d33566
@ -1,25 +1,131 @@
|
||||
use rand::Rng;
|
||||
use serenity::{
|
||||
all::Message,
|
||||
builder::{CreateAttachment, CreateMessage},
|
||||
client::Context,
|
||||
framework::standard::{macros::command, Args, CommandResult},
|
||||
};
|
||||
use tokio::fs::File;
|
||||
|
||||
use crate::{
|
||||
config::ConfigGlobal,
|
||||
img::config_file::{ConfigImgGlobal, KeyWordItem},
|
||||
};
|
||||
|
||||
#[command]
|
||||
#[description = "Answer your message with a meme"]
|
||||
#[description = "Answer your message with a meme, the keyword need to be put under quote"]
|
||||
pub async fn answer(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
if let Ok(keyword) = args.single::<String>() {
|
||||
if let Err(why) = msg
|
||||
.channel_id
|
||||
.say(&ctx.http, format!("Slow mode rate: {} seconds", keyword))
|
||||
.await
|
||||
if let Ok(keyword) = args.single_quoted::<String>() {
|
||||
println!("Keyword: {}", keyword);
|
||||
|
||||
let (config_img, config) = {
|
||||
let data_read = ctx.data.read().await;
|
||||
let config_img = data_read
|
||||
.get::<ConfigImgGlobal>()
|
||||
.expect("Config img not found")
|
||||
.clone();
|
||||
let config = data_read
|
||||
.get::<ConfigGlobal>()
|
||||
.expect("Main config not found")
|
||||
.clone();
|
||||
(config_img, config)
|
||||
};
|
||||
let folder_container = match config_img
|
||||
.keyword
|
||||
.iter()
|
||||
.find(|keyword_under_search| keyword_under_search.does_value_match(keyword.clone()))
|
||||
{
|
||||
println!("Error sending message: {:?}", why)
|
||||
Some(keyword_matching) => {
|
||||
println!("{} match {:?}", keyword, keyword_matching);
|
||||
let keyword_path = match keyword_matching.path.len() {
|
||||
0 => keyword_matching.path[0].clone(),
|
||||
_ => {
|
||||
let id: usize = {
|
||||
let mut rng = rand::thread_rng();
|
||||
rng.gen_range(0..keyword_matching.path.len())
|
||||
};
|
||||
keyword_matching.path[id].clone()
|
||||
}
|
||||
};
|
||||
|
||||
keyword_path.clone()
|
||||
}
|
||||
None => {
|
||||
if let Err(why) = msg
|
||||
.channel_id
|
||||
.say(&ctx.http, format!("No match for {}", keyword))
|
||||
.await
|
||||
{
|
||||
println!("Error sending message: {:?}", why)
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let path = format!("{}/{}", config.image.path.clone(), folder_container);
|
||||
let file_folder = KeyWordItem::output_folder_content(path.clone());
|
||||
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) {
|
||||
Some(file) => file.file_name().to_str().unwrap(),
|
||||
None => {
|
||||
if let Err(why) = msg
|
||||
.channel_id
|
||||
.say(
|
||||
&ctx.http,
|
||||
format!("Couldn't find file in folder for {}", keyword),
|
||||
)
|
||||
.await
|
||||
{
|
||||
println!("Error sending message: {:?}", why)
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_path = format!("{}/{}", path, filename);
|
||||
let file = match File::open(file_path).await {
|
||||
Ok(file) => file,
|
||||
Err(why) => {
|
||||
if let Err(why) = msg
|
||||
.channel_id
|
||||
.say(
|
||||
&ctx.http,
|
||||
format!("Error opening file {} => {:?}", filename, why),
|
||||
)
|
||||
.await
|
||||
{
|
||||
println!("Error sending message: {:?}", why)
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let attachment = match CreateAttachment::file(&file, filename).await {
|
||||
Ok(attachment) => attachment,
|
||||
Err(why) => {
|
||||
if let Err(why) = msg
|
||||
.channel_id
|
||||
.say(&ctx.http, format!("Error creating attachment: {:?}", why))
|
||||
.await
|
||||
{
|
||||
println!("Error sending message: {:?}", why)
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let builder = CreateMessage::new().add_file(attachment);
|
||||
if let Err(why) = msg.channel_id.send_message(&ctx.http, builder).await {
|
||||
println!("Error sending message: {:?}", why);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if let Err(why) = msg
|
||||
.channel_id
|
||||
.say(&ctx.http, "Answer from framework!")
|
||||
.say(
|
||||
&ctx.http,
|
||||
"Please add keyword if you want to use this command. Example: `!meme answer \"keyword\"`",
|
||||
)
|
||||
.await
|
||||
{
|
||||
println!("Error sending message: {:?}", why)
|
||||
|
Loading…
Reference in New Issue
Block a user