From 96e244835277a46baa9176a887768d85809109b2 Mon Sep 17 00:00:00 2001 From: Xyon Date: Thu, 24 Aug 2023 23:19:48 +0100 Subject: [PATCH] Add save command --- src/commands/admin.rs | 32 ++++++++++++++++++++++++++++++-- src/commands/core.rs | 2 +- src/events.rs | 3 ++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/commands/admin.rs b/src/commands/admin.rs index ed4f9c3..5c32c64 100644 --- a/src/commands/admin.rs +++ b/src/commands/admin.rs @@ -49,6 +49,34 @@ async fn set_activity(ctx: ManifoldContext<'_>, #[rest] #[description="Who to wa Ok(()) } -pub fn commands() -> [poise::Command; 5] { - [dump_config(), get_environment(), get_config(), register_commands(), set_activity()] +#[poise::command(slash_command, prefix_command, owners_only)] +async fn save_world(ctx: ManifoldContext<'_>) -> ManifoldResult<()> { + let my_message = ctx.send(|f| f.content("Save call received, saving the world...").reply(true)).await?; + + 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.save(db) { + 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(format!("Save call complete; Saved {} users.", user_count))).await?; + + Ok(()) +} + +pub fn commands() -> [poise::Command; 6] { + [dump_config(), get_environment(), get_config(), register_commands(), set_activity(), save_world()] } diff --git a/src/commands/core.rs b/src/commands/core.rs index 77f0a36..c2c2789 100644 --- a/src/commands/core.rs +++ b/src/commands/core.rs @@ -85,7 +85,7 @@ pub async fn time(ctx: ManifoldContext<'_>, target: u64) -> ManifoldResult<()> { let user = match userinfo.get(&target) { Some(u) => match u.timezone.to_owned() { - Some(t) => u, + Some(_) => u, None => {ctx.send(|f| f.content(format!("This user hasn't given me their timezone yet. Shame on them!")).reply(true)).await?; Err("NoTZ")?} }, None => { ctx.send(|f| f.content(format!("Who's that? I don't know them.")).reply(true)).await?; Err("User not found")?} diff --git a/src/events.rs b/src/events.rs index 194416b..6b30181 100644 --- a/src/events.rs +++ b/src/events.rs @@ -25,7 +25,7 @@ impl Handler { match event { Event::Ready { data_about_bot} => Handler::standard_startup(&ctx, &framework_ctx, data_about_bot).await, Event::Message { new_message } => Handler::message(&ctx, &framework_ctx, &new_message).await, - Event::MessageUpdate { old_if_available, new, event } => Handler::message_edited(&ctx, &framework_ctx, old_if_available, new).await, + Event::MessageUpdate { old_if_available, new, event: _event } => Handler::message_edited(&ctx, &framework_ctx, old_if_available, new).await, _ => Ok(()) } } @@ -34,6 +34,7 @@ impl Handler { let config = &framework_ctx.user_data().await.bot_config; let responses = &framework_ctx.user_data().await.responses; + let greeting = match responses.get_response(&"bot startup".to_string()) { Some(g) => g.to_owned(), None => "Manifold bot connected to discord and ready to begin broadcast operations.".to_string(),