From 91198ab35e48966f6869c7636439df20a512974b Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Thu, 17 Feb 2022 12:41:28 -0800 Subject: [PATCH] Add announcement command --- src/commands/owner.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 ++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/commands/owner.rs b/src/commands/owner.rs index 1624ef4..03af18f 100644 --- a/src/commands/owner.rs +++ b/src/commands/owner.rs @@ -122,4 +122,42 @@ async fn setAnnouncementRole(ctx: &Context, msg: &Message, mut args: Args) -> Co set_guild_data(guild_data); msg.reply(&ctx.http, "Announcement role set.").await?; Ok(()) +} + +async fn send(ctx: &Context, msg: &Message, message: &str, ping: bool, pin: bool) -> CommandResult { + let guild_data = get_guild_data(); + let mut announcements_count = 0; + for (_guild, data) in guild_data.iter() { + let data = data.as_object().unwrap(); + if !data.contains_key("submissionChannel") { + continue; + } + let channel = ChannelId(data["submissionChannel"].as_str().unwrap().parse::().unwrap()); + let mut message_to_send = String::from(""); + if ping && data.contains_key("announcementRole") { + message_to_send.push_str(&format!("<@&{}> ", data["announcementRole"].as_str().unwrap())); + } + message_to_send.push_str(message); + let sent_message = channel.send_message(&ctx.http, |e| { + e.content(message_to_send); + e + }).await.unwrap(); + if pin { + // No need to do anything on error, + // it just means we don't have pin permissions + match sent_message.pin(&ctx.http).await { + Ok(_) => (), + Err(_) => () + }; + } + announcements_count += 1; + } + msg.reply(&ctx.http, format!("Announced to {} server{}!", announcements_count, if announcements_count == 1 { "" } else { "s" })).await?; + Ok(()) +} + +#[command] +#[owners_only] +async fn announce(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + send(ctx, msg, args.rest(), true, true).await } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0d389aa..d11fe09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,8 @@ impl EventHandler for Handler { help, sleep, setSubmissionChannel, - setAnnouncementRole + setAnnouncementRole, + announce )] struct General;