discord-kurante/src/commands/crosspost.rs

51 lines
1.6 KiB
Rust

use crate::util::debug_channel_say;
use lazy_static::lazy_static;
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<ChannelId, _> = 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 {
debug_channel_say(&channel.into(), *msg.author.id.as_u64(), ctx, &rest).await?;
}
Ok(())
}