From 5c270db9cb07d001dfd740525f2e358dc01c5e1b Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Sun, 14 Jun 2020 20:36:15 -0400 Subject: [PATCH] Implement announcer tool --- youmubot-prelude/src/announcer.rs | 47 ++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/youmubot-prelude/src/announcer.rs b/youmubot-prelude/src/announcer.rs index 99847bb..ad5bfb9 100644 --- a/youmubot-prelude/src/announcer.rs +++ b/youmubot-prelude/src/announcer.rs @@ -168,6 +168,51 @@ impl AnnouncerHandler { } } +/// Gets the announcer of the given guild. +pub fn announcer_of( + ctx: &Context, + key: &'static str, + guild: GuildId, +) -> Result, Error> { + Ok(AnnouncerChannels::open(&*ctx.data.read()) + .borrow()? + .get(key) + .and_then(|channels| channels.get(&guild).cloned())) +} + +#[command("list")] +#[description = "List the registered announcers of this server"] +#[num_args(0)] +#[only_in(guilds)] +pub fn list_announcers(ctx: &mut Context, m: &Message, _: Args) -> CommandResult { + let guild_id = m.guild_id.unwrap(); + let announcers = AnnouncerChannels::open(&*ctx.data.read()); + let announcers = announcers.borrow()?; + + let channels = ctx + .data + .get_cloned::() + .into_iter() + .filter_map(|key| { + announcers + .get(key) + .and_then(|channels| channels.get(&guild_id)) + .map(|&ch| (key, ch)) + }) + .map(|(key, ch)| format!(" - `{}`: activated on channel {}", key, ch.mention())) + .collect::>(); + + m.reply( + &ctx, + format!( + "Activated announcers on this server:\n{}", + channels.join("\n") + ), + )?; + + Ok(()) +} + #[command("register")] #[description = "Register the current channel with an announcer"] #[usage = "[announcer key]"] @@ -253,5 +298,5 @@ pub fn remove_announcer(ctx: &mut Context, m: &Message, mut args: Args) -> Comma #[only_in(guilds)] #[required_permissions(MANAGE_CHANNELS)] #[description = "Manage the announcers in the server."] -#[commands(remove_announcer, register_announcer)] +#[commands(remove_announcer, register_announcer, list_announcers)] pub struct AnnouncerCommands;