diff --git a/youmubot-prelude/src/announcer.rs b/youmubot-prelude/src/announcer.rs index ecd195a..62b50ab 100644 --- a/youmubot-prelude/src/announcer.rs +++ b/youmubot-prelude/src/announcer.rs @@ -1,13 +1,17 @@ -use crate::AppData; +use crate::{AppData, GetCloned}; use rayon::prelude::*; use serenity::{ - framework::standard::{macros::command, Args, CommandError as Error, CommandResult}, + framework::standard::{ + macros::{command, group}, + Args, CommandError as Error, CommandResult, + }, http::CacheHttp, model::{ channel::Message, id::{ChannelId, GuildId, UserId}, }, prelude::*, + utils::MessageBuilder, CacheAndHttp, }; use std::{ @@ -162,6 +166,87 @@ impl AnnouncerHandler { #[command("register")] #[description = "Register the current channel with an announcer"] #[usage = "[announcer key]"] +#[required_permissions(MANAGE_CHANNELS)] +#[only_in(guilds)] +#[num_args(1)] pub fn register_announcer(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult { - unimplemented!() + let key = args.single::()?; + let keys = ctx.data.get_cloned::(); + if !keys.contains(&key.as_str()) { + m.reply( + &ctx, + format!( + "Key not found. Available announcer keys are: `{}`", + keys.join(", ") + ), + )?; + return Ok(()); + } + let guild = m.guild(&ctx).expect("Guild-only command"); + let guild = guild.read(); + let channel = m.channel_id.to_channel(&ctx)?; + AnnouncerChannels::open(&*ctx.data.read()) + .borrow_mut()? + .entry(key.clone()) + .or_default() + .insert(guild.id, m.channel_id); + m.reply( + &ctx, + MessageBuilder::new() + .push("Announcer ") + .push_mono_safe(key) + .push(" has been activated for server ") + .push_bold_safe(&guild.name) + .push(" on channel ") + .push_bold_safe(channel) + .build(), + )?; + Ok(()) } + +#[command("remove")] +#[description = "Remove an announcer from the server"] +#[usage = "[announcer key]"] +#[required_permissions(MANAGE_CHANNELS)] +#[only_in(guilds)] +#[num_args(1)] +pub fn remove_announcer(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult { + let key = args.single::()?; + let keys = ctx.data.get_cloned::(); + if !keys.contains(&key.as_str()) { + m.reply( + &ctx, + format!( + "Key not found. Available announcer keys are: `{}`", + keys.join(", ") + ), + )?; + return Ok(()); + } + let guild = m.guild(&ctx).expect("Guild-only command"); + let guild = guild.read(); + AnnouncerChannels::open(&*ctx.data.read()) + .borrow_mut()? + .entry(key.clone()) + .and_modify(|m| { + m.remove(&guild.id); + }); + m.reply( + &ctx, + MessageBuilder::new() + .push("Announcer ") + .push_mono_safe(key) + .push(" has been de-activated for server ") + .push_bold_safe(&guild.name) + .build(), + )?; + Ok(()) +} + +#[group("announcer")] +#[prefix("announcer")] +#[only_in(guilds)] +#[required_permissions(MANAGE_CHANNELS)] +#[description = "Manage the announcers in the server."] +#[commands(remove_announcer, register_announcer)] +pub struct AnnouncerCommands; diff --git a/youmubot-prelude/src/lib.rs b/youmubot-prelude/src/lib.rs index 29f9704..8a9669a 100644 --- a/youmubot-prelude/src/lib.rs +++ b/youmubot-prelude/src/lib.rs @@ -50,3 +50,28 @@ impl GetCloned for AppData { self.read().get::().cloned().expect("Should be there") } } + +pub mod prelude_commands { + use crate::announcer::ANNOUNCERCOMMANDS_GROUP; + use serenity::{ + framework::standard::{ + macros::{command, group}, + CommandResult, + }, + model::channel::Message, + prelude::Context, + }; + + #[group("Prelude")] + #[description = "All the commands that makes the base of Youmu"] + #[commands(ping)] + #[sub_groups(AnnouncerCommands)] + pub struct Prelude; + + #[command] + #[description = "pong!"] + fn ping(ctx: &mut Context, m: &Message) -> CommandResult { + m.reply(&ctx, "Pong!")?; + Ok(()) + } +} diff --git a/youmubot/src/main.rs b/youmubot/src/main.rs index 1c79fd3..13c6cde 100644 --- a/youmubot/src/main.rs +++ b/youmubot/src/main.rs @@ -165,7 +165,8 @@ fn setup_framework(client: &Client) -> StandardFramework { .bucket("images", |c| c.time_span(60).limit(2)) .bucket("community", |c| { c.delay(30).time_span(30).limit(1) - }); + }) + .group(&prelude_commands::PRELUDE_GROUP); // groups here #[cfg(feature = "core")] let fw = fw