diff --git a/src/core_commands.rs b/src/core_commands.rs new file mode 100644 index 0000000..95b9ef7 --- /dev/null +++ b/src/core_commands.rs @@ -0,0 +1,109 @@ +use serenity::{ + prelude::*, + framework::standard::{ + macros::command, + CommandResult, + Args, + }, + model::prelude::* +}; +use crate::config::{Config, ManifoldConfig}; +use crate::built_info; + +#[command] +async fn ping(ctx: &Context, msg: &Message) -> CommandResult { + msg.reply_ping(ctx, "Pong!").await?; + + Ok(()) +} + +#[command] +#[aliases("sa")] +async fn set_activity(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + + let activity_name = args.raw().collect::>().join(" "); + + msg.reply_ping(&ctx, "OK: I'm going to start playing that.").await.unwrap(); + + ctx.set_activity(Activity::playing(&activity_name)).await; + + Ok(()) +} + +#[command] +#[aliases("v")] +async fn version(ctx: &Context, msg: &Message) -> CommandResult { + + let git_info: String = built_info::GIT_VERSION.unwrap_or("unknown").to_string(); + + let version_string: String = format!("Version {} built at {} revision {}", built_info::PKG_VERSION, built_info::BUILT_TIME_UTC, git_info); + + msg.reply_ping(&ctx, &version_string).await.unwrap(); + + Ok(()) +} + +#[command] +async fn get_config(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { + let data = &ctx.data.read().await; + let config = match data.get::() { + Some(c) => c.lock().await, + None => Err("Could not lock database!".to_string())? + }; + + let key = match args.single::() { + Ok(k) => k, + Err(e) => { msg.reply_ping(&ctx, format!("Failed to process your input: {:?}", e.to_string())).await?; Err("ArgParse")? } + }; + + let value = match config.get_value(&key) { + Some(v) => v.clone(), + None => "not found, sorry!".to_string() + }; + + msg.reply_ping(&ctx, format!("Value for key {} was {}", &key, &value)).await?; + + Ok(()) +} + +#[command] +async fn set_config(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { + let mut data = ctx.data.write().await; + let mut config = match data.get_mut::() { + Some(c) => c.lock().await, + None => Err("Could not lock database!".to_string())? + }; + + let key = match args.single::() { + Ok(k) => k, + Err(e) => { msg.reply_ping(&ctx, format!("Error parsing your message: {:?}", e)).await?; Err("ArgParse")? } + }; + + let value = match args.single::() { + Ok(v) => v, + Err(e) => { msg.reply_ping(&ctx, format!("Error parsing your message: {:?}", e)).await?; Err("ArgParse")? } + }; + + match config.set_value(&key, &value) { + Some(_) => msg.reply_ping(&ctx, format!("Value for key {} set to {}", &key, &value)).await?, + None => msg.reply_ping(&ctx, format!("Error setting config value")).await?, + }; + + config.save_config()?; + + Ok(()) +} + + +#[command] +async fn get_environment(ctx: &Context, msg: &Message, _: Args) -> CommandResult { + let data = ctx.data.read().await; + let config = match data.get::() { + Some(c) => c.lock().await, + None => Err("Could not lock config!".to_string())? + }; + + msg.reply_ping(&ctx, format!("Currently running under the {} environment", config.get_environment())).await?; + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index c566779..82c955c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,14 @@ pub mod config; pub mod error; pub mod responses; pub mod events; +pub mod core_commands; + +// Retrieve build info from output file +pub mod built_info { + include!(concat!(env!("OUT_DIR"), "/built.rs")); +} + +use crate::core_commands::*; type Pool = diesel::r2d2::Pool>; @@ -50,7 +58,7 @@ impl TypeMapKey for ManifoldConfig { } #[group] -#[commands(ping)] +#[commands(ping, set_config, get_config, version, set_activity, get_environment)] struct Core; pub async fn prepare_client(arguments: ArgMatches<'_>, mut framework: StandardFramework) -> ManifoldResult { @@ -137,13 +145,6 @@ fn load_config(config_file: &String, bot_environment: &String) -> ManifoldConfig config } -#[command] -async fn ping(ctx: &Context, msg: &Message) -> CommandResult { - msg.reply_ping(ctx, "Pong!").await?; - - Ok(()) -} - #[help] async fn manifold_help( ctx: &Context,