manifold/src/commands/admin.rs

59 lines
2.2 KiB
Rust
Raw Normal View History

2023-08-24 09:27:15 +00:00
use poise::serenity_prelude::Activity;
2023-08-24 09:27:15 +00:00
use crate::{ManifoldContext, ManifoldData, ManifoldResult};
use crate::error::ManifoldError;
#[poise::command(slash_command, prefix_command, owners_only)]
async fn dump_config(ctx: ManifoldContext<'_>) -> ManifoldResult<()> {
ctx.say(t!("commands.admin.config_dump", dump = format!("{:?}", &ctx.data().bot_config))).await?;
2023-08-24 09:27:15 +00:00
Ok(())
}
#[poise::command(slash_command, prefix_command, owners_only)]
async fn register_commands(ctx: ManifoldContext<'_>) -> ManifoldResult<()> {
poise::builtins::register_application_commands_buttons(ctx).await?;
Ok(())
}
#[poise::command(slash_command, prefix_command, track_edits, aliases("sa"), required_permissions = "MODERATE_MEMBERS")]
async fn set_activity(ctx: ManifoldContext<'_>, #[rest] #[description="Who to watch"] target: String) -> ManifoldResult<()> {
ctx.serenity_context().set_activity(Activity::watching(&target)).await;
ctx.say(t!("commands.admin.start_watching", target = target)).await?;
2023-08-24 09:27:15 +00:00
Ok(())
}
2023-08-24 22:19:48 +00:00
#[poise::command(slash_command, prefix_command, owners_only)]
async fn save_world(ctx: ManifoldContext<'_>) -> ManifoldResult<()> {
let my_message = ctx.send(|f| f.content(t!("commands.admin.saving_the_world")).reply(true)).await?;
2023-08-24 22:19:48 +00:00
let mut user_count = 0;
let db = &ctx.data().database;
let userinfo = ctx.data().user_info.lock().await;
for user in userinfo.iter() {
debug!("Saving user {:?}", user.0);
match user.1.insert(db) {
Ok(_) => debug!("User {:?} inserted successfully", user.0),
Err(e) => {
debug!("User {:?} was not inserted: {:?}, attempting to save instead", user.1, e);
match user.1.insert(db) {
2023-08-24 22:19:48 +00:00
Ok(_) => debug!("User {:?} saved successfully, user is saved in database.", user.0),
Err(e) => debug!("User {:?} could not be saved: {:?}", user.1, e),
};
}
}
user_count = userinfo.len();
}
my_message.edit(ctx, |f| f.content(t!("commands.admin.save_complete", user_count = user_count))).await?;
2023-08-24 22:19:48 +00:00
Ok(())
}
2023-08-30 15:53:24 +00:00
pub fn commands() -> [poise::Command<ManifoldData, ManifoldError>; 4] {
[dump_config(), register_commands(), set_activity(), save_world()]
2023-08-24 09:27:15 +00:00
}