From b9f9359ed79e12f9d34f0691fde18dcc1ade1db3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 21 Apr 2021 12:14:02 +0200 Subject: [PATCH] Add korone command Signed-off-by: trivernis --- src/commands/weeb/korone.rs | 14 ++++++++++++++ src/commands/weeb/matsuri.rs | 17 ++--------------- src/commands/weeb/mod.rs | 25 ++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 src/commands/weeb/korone.rs diff --git a/src/commands/weeb/korone.rs b/src/commands/weeb/korone.rs new file mode 100644 index 0000000..182f463 --- /dev/null +++ b/src/commands/weeb/korone.rs @@ -0,0 +1,14 @@ +use crate::commands::weeb::post_random_media; +use serenity::client::Context; +use serenity::framework::standard::macros::command; +use serenity::framework::standard::CommandResult; +use serenity::model::channel::Message; + +#[command] +#[description("Posts a random korone gif")] +#[usage("")] +#[aliases("yubi")] +#[bucket("general")] +async fn korone(ctx: &Context, msg: &Message) -> CommandResult { + post_random_media(ctx, msg, "korone").await +} diff --git a/src/commands/weeb/matsuri.rs b/src/commands/weeb/matsuri.rs index 430d6d4..1aeee57 100644 --- a/src/commands/weeb/matsuri.rs +++ b/src/commands/weeb/matsuri.rs @@ -1,26 +1,13 @@ -use crate::utils::context_data::get_database_from_context; -use crate::utils::error::BotError; -use rand::prelude::IteratorRandom; +use crate::commands::weeb::post_random_media; use serenity::client::Context; use serenity::framework::standard::macros::command; use serenity::framework::standard::CommandResult; use serenity::model::channel::Message; -static MEDIA_CATEGORY: &str = "matsuri"; - #[command] #[description("Posts a random matsuri gif")] #[usage("")] #[bucket("general")] async fn matsuri(ctx: &Context, msg: &Message) -> CommandResult { - let database = get_database_from_context(ctx).await; - let media = database.get_media_by_category(MEDIA_CATEGORY).await?; - let gif = media - .into_iter() - .choose(&mut rand::thread_rng()) - .ok_or(BotError::from("No gifs found."))?; - - msg.channel_id.say(ctx, gif.url).await?; - - Ok(()) + post_random_media(ctx, msg, "matsuri").await } diff --git a/src/commands/weeb/mod.rs b/src/commands/weeb/mod.rs index 59e6032..918ac95 100644 --- a/src/commands/weeb/mod.rs +++ b/src/commands/weeb/mod.rs @@ -1,13 +1,36 @@ use serenity::framework::standard::macros::group; +mod korone; mod matsuri; mod pekofy; mod sauce; +use crate::utils::context_data::get_database_from_context; +use crate::utils::error::BotError; +use rand::prelude::IteratorRandom; +use serenity::client::Context; +use serenity::framework::standard::CommandResult; +use serenity::model::channel::Message; + +use korone::KORONE_COMMAND; use matsuri::MATSURI_COMMAND; use pekofy::PEKOFY_COMMAND; use sauce::SAUCE_COMMAND; #[group] -#[commands(pekofy, sauce, matsuri)] +#[commands(pekofy, sauce, matsuri, korone)] pub struct Weeb; + +/// Posts a random media entry with the given category +async fn post_random_media(ctx: &Context, msg: &Message, category: &str) -> CommandResult { + let database = get_database_from_context(ctx).await; + let media = database.get_media_by_category(category).await?; + let gif = media + .into_iter() + .choose(&mut rand::thread_rng()) + .ok_or(BotError::from("No media found."))?; + + msg.channel_id.say(ctx, gif.url).await?; + + Ok(()) +}