Add more central commands to the core

This commit is contained in:
Lucy Bladen 2021-11-12 13:23:14 +00:00
parent 459bd8068e
commit c6f1add119
2 changed files with 118 additions and 8 deletions

109
src/core_commands.rs Normal file
View File

@ -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::<Vec<&str>>().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::<ManifoldConfig>() {
Some(c) => c.lock().await,
None => Err("Could not lock database!".to_string())?
};
let key = match args.single::<String>() {
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::<ManifoldConfig>() {
Some(c) => c.lock().await,
None => Err("Could not lock database!".to_string())?
};
let key = match args.single::<String>() {
Ok(k) => k,
Err(e) => { msg.reply_ping(&ctx, format!("Error parsing your message: {:?}", e)).await?; Err("ArgParse")? }
};
let value = match args.single::<String>() {
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::<ManifoldConfig>() {
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(())
}

View File

@ -22,6 +22,14 @@ pub mod config;
pub mod error; pub mod error;
pub mod responses; pub mod responses;
pub mod events; 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<ConnectionManager<SqliteConnection>>; type Pool = diesel::r2d2::Pool<ConnectionManager<SqliteConnection>>;
@ -50,7 +58,7 @@ impl TypeMapKey for ManifoldConfig {
} }
#[group] #[group]
#[commands(ping)] #[commands(ping, set_config, get_config, version, set_activity, get_environment)]
struct Core; struct Core;
pub async fn prepare_client(arguments: ArgMatches<'_>, mut framework: StandardFramework) -> ManifoldResult<Client> { pub async fn prepare_client(arguments: ArgMatches<'_>, mut framework: StandardFramework) -> ManifoldResult<Client> {
@ -137,13 +145,6 @@ fn load_config(config_file: &String, bot_environment: &String) -> ManifoldConfig
config config
} }
#[command]
async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply_ping(ctx, "Pong!").await?;
Ok(())
}
#[help] #[help]
async fn manifold_help( async fn manifold_help(
ctx: &Context, ctx: &Context,