diff options
author | NatrixAeria <upezu@student.kit.edu> | 2020-09-19 00:01:30 +0200 |
---|---|---|
committer | NatrixAeria <upezu@student.kit.edu> | 2020-09-19 00:01:30 +0200 |
commit | 52376d0a993cab721c94ccc2a410aad213701dda (patch) | |
tree | d65d7caaab9bfff3f3295271b316472488311967 | |
parent | 2cafc2afbc27910db23c0b007d3bc2c93b8e8bce (diff) |
Add the create command
-rw-r--r-- | src/config.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 91 |
2 files changed, 89 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs index bb05d99..cd8a55b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ -use serenity::framework::standard::CommandGroup; - pub const TOKEN_ENV: &str = "DISCORD_TOKEN"; +pub const GLOBAL_COMMAND_PREFIX: &str = "!<3"; +pub const META_CHANNEL_NAME: &str = "Cupido Shuffler"; +pub const META_CHANNEL_DESCRIPTION: &str = "All channels related to cupido"; pub const HELP_TEXT: &str = "Cupido is a discord bot to get to know your people. Cupido is open-source <https://git.kobert.dev/lovefinderrz.git/>!"; diff --git a/src/main.rs b/src/main.rs index 79371f9..8ac2edd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,27 +4,108 @@ use serenity::framework::StandardFramework; use serenity::Client; fn configure(conf: &mut serenity::framework::standard::Configuration) { - conf.prefix("!<3").case_insensitivity(true); + conf.prefix(config::GLOBAL_COMMAND_PREFIX) + .with_whitespace((true, true, true)) + .case_insensitivity(true); } struct Handler; +#[derive(Debug, Clone)] +pub enum BotError { + MissingGuild, +} + +impl std::fmt::Display for BotError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Self::MissingGuild => write!(f, "this command is only usable in a guild"), + } + } +} + +impl std::error::Error for BotError {} + mod commands { + use crate::config; use serenity::client::Context; - use serenity::framework::standard::{macros::*, CommandResult}; - use serenity::model::channel::Message; + use serenity::framework::standard::{macros::*, CommandError, CommandResult}; + use serenity::model::{ + channel::{ChannelType, GuildChannel, Message}, + guild::Guild, + id::ChannelId, + }; #[group] #[description = "Commands for the cupido bot"] - #[default_command(help)] - #[commands(help)] + #[commands(help, create)] struct Group; + struct ChannelDescriptor<'n, 'd> { + name: &'n str, + description: &'d str, + kind: ChannelType, + parent: Option<ChannelId>, + } + + async fn get_guild(ctx: &Context, msg: &Message) -> Result<Guild, CommandError> { + msg.guild(&ctx.cache) + .await + .ok_or_else(|| Box::new(super::BotError::MissingGuild).into()) + } + + async fn create_channel<'n, 'd>( + ctx: &Context, + guild: &Guild, + desc: ChannelDescriptor<'n, 'd>, + ) -> Result<GuildChannel, CommandError> { + let channel = guild.channel_id_from_name(&ctx.cache, desc.name).await; + Ok( + match match channel { + Some(channel) => ctx.cache.guild_channel(channel).await, + None => None, + } { + Some(channel) => channel, + None => { + guild + .create_channel(ctx, |c| { + let c = c.name(desc.name).topic(desc.description).kind(desc.kind); + if let Some(parent) = desc.parent { + c.category(parent) + } else { + c + } + }) + .await? + } + }, + ) + } + #[command] #[aliases("hepl", "?", "h")] async fn help(ctx: &Context, msg: &Message) -> CommandResult { msg.reply(ctx, crate::config::HELP_TEXT).await?; Ok(()) } + + #[command] + #[aliases("craete", "+", "c", "init")] + async fn create(ctx: &Context, msg: &Message) -> CommandResult { + let guild = get_guild(ctx, msg).await?; + let meta_channel = create_channel( + ctx, + &guild, + ChannelDescriptor { + name: config::META_CHANNEL_NAME, + description: config::META_CHANNEL_DESCRIPTION, + kind: ChannelType::Category, + parent: None, + }, + ) + .await?; + + Ok(()) + } } impl serenity::client::EventHandler for Handler {} |