crosspost command

This commit is contained in:
Edward Shen 2020-05-02 15:41:06 -04:00
parent 6090db30bc
commit de5cd63e9c
Signed by: edward
GPG key ID: 19182661E818369F
2 changed files with 55 additions and 2 deletions

51
src/commands/crosspost.rs Normal file
View file

@ -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<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 {
channel.say(ctx, &rest).await?;
}
Ok(())
}

View file

@ -1,13 +1,15 @@
use crate::commands::{ 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; use serenity::framework::standard::macros::group;
mod clap; mod clap;
mod crosspost;
mod cube; mod cube;
mod heck; mod heck;
mod source; mod source;
#[group] #[group]
#[commands(heck, clap, cube, source)] #[commands(heck, clap, cube, source, crosspost)]
pub(crate) struct General; pub(crate) struct General;