feat: Rewrite de la function list

This commit is contained in:
Max batleforc 2024-05-26 23:51:01 +02:00
parent 45ddc2f2f7
commit 7711df3274
No known key found for this signature in database
GPG Key ID: 25D243AB4B6AC9E7
4 changed files with 44 additions and 3 deletions

View File

@ -0,0 +1,39 @@
use crate::botv2::init::{Context,Error};
use poise::{serenity_prelude::{CreateEmbed, CreateEmbedFooter, CreateMessage}, CreateReply};
/// List available meme keywords
#[poise::command(
slash_command,
prefix_command,
category = "meme"
)]
pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
let config_img = ctx.data().config_img.clone();
let list_value: Vec<(String, String, bool)> = config_img
.keyword
.iter()
.map(|x| (x.value.clone().join(", "), String::new(), true))
.collect();
if list_value.is_empty() {
let builder = CreateMessage::new().content("No meme keyword found");
if let Err(why) = ctx.channel_id().send_message(ctx.http(), builder).await {
println!("Error sending message: {:?}", why)
}
return Ok(());
}
let embed_vec = list_value.chunks(25).map(|chunks| {
let footer = CreateEmbedFooter::new("WeeboBot");
CreateEmbed::new()
.title("Meme List")
.fields(chunks.to_vec())
.footer(footer)
});
let mut reply = CreateReply::default();
reply.embeds = embed_vec.collect();
if let Err(why) = ctx.send(reply).await {
println!("Error sending message: {:?}", why)
}
Ok(())
}
// https://github.com/serenity-rs/poise/blob/current/examples/fluent_localization/main.rs

View File

@ -0,0 +1 @@
pub mod list;

View File

@ -1,2 +1,3 @@
pub mod ping; pub mod ping;
pub mod help; pub mod help;
pub mod meme;

View File

@ -1,7 +1,7 @@
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use std::fs; use std::fs;
use serenity::GatewayIntents; use serenity::GatewayIntents;
use crate::botv2::cmd::{ping::ping,help::help}; use crate::botv2::cmd::{ping::ping,help::help, meme::list::list};
use crate::{botv2::handler::event_handler, img::config_file::ConfigFile}; use crate::{botv2::handler::event_handler, img::config_file::ConfigFile};
use crate::config::Config; use crate::config::Config;
use tokio::sync::oneshot; use tokio::sync::oneshot;
@ -64,7 +64,7 @@ pub fn start_bot(config: Config, rx: oneshot::Receiver<()>){
let prefix = config.prefix.clone(); let prefix = config.prefix.clone();
let framework = poise::Framework::builder() let framework = poise::Framework::builder()
.options(poise::FrameworkOptions { .options(poise::FrameworkOptions {
commands: vec![age(), ping(),help()], commands: vec![age(), ping(),help(), list()],
prefix_options: poise::PrefixFrameworkOptions { prefix_options: poise::PrefixFrameworkOptions {
prefix: Some(prefix.into()), prefix: Some(prefix.into()),
..Default::default() ..Default::default()