diff --git a/src/commands/crosspost.rs b/src/commands/crosspost.rs new file mode 100644 index 0000000..1e261ce --- /dev/null +++ b/src/commands/crosspost.rs @@ -0,0 +1,51 @@ +use crate::util::debug_say; +use lazy_static::lazy_static; +use log::error; +use regex::Regex; +use serenity::framework::standard::CommandError; +use serenity::framework::standard::{macros::command, Args, CommandResult}; +use serenity::model::{channel::Message, id::ChannelId}; +use serenity::prelude::Context; +use std::collections::HashMap; +use std::str::FromStr; + +lazy_static! { + static ref CHANNEL_REGEX: Regex = Regex::new(r"<#\d+>").unwrap(); +} + +#[command] +async fn crosspost(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { + let guild_id = msg.guild_id; + let guild_id = match guild_id { + Some(guild_id) => guild_id, + None => return Err(CommandError::from(String::from("Not in guild"))), + }; + let mut avail_channels: HashMap = guild_id.channels(ctx).await?; + avail_channels.remove(&msg.channel_id); // Don't crosspost to current channel. + let mut channels = vec![]; + while args.current().is_some() { + let arg = args.current().unwrap(); + if CHANNEL_REGEX.is_match(&arg) { + match avail_channels.get(&ChannelId::from_str(arg.as_ref())?) { + Some(channel) => channels.push(channel), + None => {} + } + args.advance(); + } else { + // Stop early if we stop getting channel ids + break; + } + } + + let rest = format!( + "Crossposted from <#{}> by {}:\n{}", + msg.channel_id, + msg.author, + args.rest() + ); + for channel in channels { + channel.say(ctx, &rest).await?; + } + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0590124..e1c0d0e 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,13 +1,15 @@ use crate::commands::{ - clap::CLAP_COMMAND, cube::CUBE_COMMAND, heck::HECK_COMMAND, source::SOURCE_COMMAND, + clap::CLAP_COMMAND, crosspost::CROSSPOST_COMMAND, cube::CUBE_COMMAND, heck::HECK_COMMAND, + source::SOURCE_COMMAND, }; use serenity::framework::standard::macros::group; mod clap; +mod crosspost; mod cube; mod heck; mod source; #[group] -#[commands(heck, clap, cube, source)] +#[commands(heck, clap, cube, source, crosspost)] pub(crate) struct General;