|
| 1 | +use crate::config::Config; |
| 2 | +use crate::errors::{ModmailResult, common}; |
| 3 | +use crate::i18n::get_translated_message; |
| 4 | +use crate::modules::message_recovery::recover_missing_messages; |
| 5 | +use crate::utils::command::defer_response::defer_response; |
| 6 | +use crate::utils::message::message_builder::MessageBuilder; |
| 7 | +use serenity::all::{CommandInteraction, Context, CreateCommand, ResolvedOption}; |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +pub async fn register(config: &Config) -> CreateCommand { |
| 11 | + let cmd_desc = get_translated_message( |
| 12 | + config, |
| 13 | + "slash_command.recover_command_description", |
| 14 | + None, |
| 15 | + None, |
| 16 | + None, |
| 17 | + None, |
| 18 | + ) |
| 19 | + .await; |
| 20 | + |
| 21 | + CreateCommand::new("recover").description(cmd_desc) |
| 22 | +} |
| 23 | + |
| 24 | +pub async fn run( |
| 25 | + ctx: &Context, |
| 26 | + command: &CommandInteraction, |
| 27 | + _options: &[ResolvedOption<'_>], |
| 28 | + config: &Config, |
| 29 | +) -> ModmailResult<()> { |
| 30 | + let _ = config |
| 31 | + .db_pool |
| 32 | + .as_ref() |
| 33 | + .ok_or_else(common::database_connection_failed)?; |
| 34 | + |
| 35 | + defer_response(&ctx, &command).await?; |
| 36 | + |
| 37 | + let mut params = HashMap::new(); |
| 38 | + params.insert("user".to_string(), command.user.name.clone()); |
| 39 | + |
| 40 | + let response = MessageBuilder::system_message(ctx, config) |
| 41 | + .translated_content( |
| 42 | + "recovery.started", |
| 43 | + Some(¶ms), |
| 44 | + Some(command.user.id), |
| 45 | + command.guild_id.map(|g| g.get()), |
| 46 | + ) |
| 47 | + .await |
| 48 | + .to_channel(command.channel_id) |
| 49 | + .build_interaction_message_followup() |
| 50 | + .await; |
| 51 | + |
| 52 | + let _ = command.create_followup(&ctx.http, response).await; |
| 53 | + |
| 54 | + let ctx_clone = ctx.clone(); |
| 55 | + let config_clone = config.clone(); |
| 56 | + let channel_id = command.channel_id; |
| 57 | + let command_clone = command.clone(); |
| 58 | + |
| 59 | + tokio::spawn(async move { |
| 60 | + let recovery_results = recover_missing_messages(&ctx_clone, &config_clone).await; |
| 61 | + |
| 62 | + let total_recovered: u32 = recovery_results.iter().map(|r| r.recovered_count).sum(); |
| 63 | + let successful_threads = recovery_results.iter().filter(|r| r.success).count(); |
| 64 | + let failed_threads = recovery_results.len() - successful_threads; |
| 65 | + |
| 66 | + let mut params = HashMap::new(); |
| 67 | + params.insert("total".to_string(), total_recovered.to_string()); |
| 68 | + params.insert("threads".to_string(), successful_threads.to_string()); |
| 69 | + params.insert("failed".to_string(), failed_threads.to_string()); |
| 70 | + |
| 71 | + let response = MessageBuilder::system_message(&ctx_clone, &config_clone) |
| 72 | + .translated_content("recovery.summary", Some(¶ms), None, None) |
| 73 | + .await |
| 74 | + .to_channel(channel_id) |
| 75 | + .build_interaction_message_followup() |
| 76 | + .await; |
| 77 | + |
| 78 | + let _ = command_clone |
| 79 | + .create_followup(&ctx_clone.http, response) |
| 80 | + .await; |
| 81 | + }); |
| 82 | + |
| 83 | + Ok(()) |
| 84 | +} |
0 commit comments