|
| 1 | +use crate::commands::{BoxFuture, RegistrableCommand}; |
| 2 | +use crate::config::Config; |
| 3 | +use crate::db::reminders::{get_reminder_by_id, update_reminder_status}; |
| 4 | +use crate::errors::{CommandError, DatabaseError, ModmailError, ModmailResult, common}; |
| 5 | +use crate::i18n::get_translated_message; |
| 6 | +use crate::utils::command::defer_response::defer_response; |
| 7 | +use crate::utils::message::message_builder::MessageBuilder; |
| 8 | +use serenity::all::{ |
| 9 | + CommandDataOptionValue, CommandInteraction, CommandOptionType, Context, CreateCommand, |
| 10 | + CreateCommandOption, ResolvedOption, |
| 11 | +}; |
| 12 | +use std::collections::HashMap; |
| 13 | + |
| 14 | +pub struct RemoveReminderCommand; |
| 15 | + |
| 16 | +impl RegistrableCommand for RemoveReminderCommand { |
| 17 | + fn name(&self) -> &'static str { |
| 18 | + "remove_reminder" |
| 19 | + } |
| 20 | + |
| 21 | + fn register(&self, config: &Config) -> BoxFuture<Vec<CreateCommand>> { |
| 22 | + let config = config.clone(); |
| 23 | + let name = self.name(); |
| 24 | + |
| 25 | + Box::pin(async move { |
| 26 | + let cmd_desc = get_translated_message( |
| 27 | + &config, |
| 28 | + "slash_command.remove_reminder_command_description", |
| 29 | + None, |
| 30 | + None, |
| 31 | + None, |
| 32 | + None, |
| 33 | + ) |
| 34 | + .await; |
| 35 | + let id_desc = get_translated_message( |
| 36 | + &config, |
| 37 | + "slash_command.remove_reminder_id_argument", |
| 38 | + None, |
| 39 | + None, |
| 40 | + None, |
| 41 | + None, |
| 42 | + ) |
| 43 | + .await; |
| 44 | + |
| 45 | + vec![CreateCommand::new(name).description(cmd_desc).add_option( |
| 46 | + CreateCommandOption::new(CommandOptionType::Number, "id", id_desc).required(true), |
| 47 | + )] |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + fn run( |
| 52 | + &self, |
| 53 | + ctx: &Context, |
| 54 | + command: &CommandInteraction, |
| 55 | + options: &[ResolvedOption<'_>], |
| 56 | + config: &Config, |
| 57 | + ) -> BoxFuture<ModmailResult<()>> { |
| 58 | + let ctx = ctx.clone(); |
| 59 | + let command = command.clone(); |
| 60 | + let config = config.clone(); |
| 61 | + |
| 62 | + Box::pin(async move { |
| 63 | + let pool = config |
| 64 | + .db_pool |
| 65 | + .as_ref() |
| 66 | + .ok_or_else(common::database_connection_failed)?; |
| 67 | + |
| 68 | + let _ = defer_response(&ctx, &command).await; |
| 69 | + |
| 70 | + let mut reminder_id: Option<i64> = None; |
| 71 | + |
| 72 | + for option in &command.data.options { |
| 73 | + match option.name.as_str() { |
| 74 | + "id" => { |
| 75 | + if let CommandDataOptionValue::Number(val) = &option.value { |
| 76 | + reminder_id.replace(*val as i64); |
| 77 | + } |
| 78 | + } |
| 79 | + _ => {} |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + let reminder_id = match reminder_id { |
| 84 | + Some(id) => id, |
| 85 | + None => { |
| 86 | + return Err(ModmailError::Command(CommandError::InvalidArguments( |
| 87 | + "reminder ID".to_string(), |
| 88 | + ))); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + let reminder = match get_reminder_by_id(reminder_id, pool).await { |
| 93 | + Ok(Some(r)) => r, |
| 94 | + Ok(None) => { |
| 95 | + return Err(ModmailError::Database(DatabaseError::NotFound( |
| 96 | + "".to_string(), |
| 97 | + ))); |
| 98 | + } |
| 99 | + Err(e) => { |
| 100 | + return Err(ModmailError::Database(DatabaseError::QueryFailed( |
| 101 | + e.to_string(), |
| 102 | + ))); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + match update_reminder_status(&reminder, true, pool).await { |
| 107 | + Ok(_) => { |
| 108 | + let mut params = HashMap::new(); |
| 109 | + params.insert("id".to_string(), reminder_id.to_string()); |
| 110 | + |
| 111 | + let response = MessageBuilder::system_message(&ctx, &config) |
| 112 | + .translated_content( |
| 113 | + "remove_reminder.confirmation", |
| 114 | + Some(¶ms), |
| 115 | + None, |
| 116 | + None, |
| 117 | + ) |
| 118 | + .await |
| 119 | + .to_channel(command.channel_id) |
| 120 | + .build_interaction_message_followup() |
| 121 | + .await; |
| 122 | + |
| 123 | + let _ = command.create_followup(&ctx.http, response).await; |
| 124 | + } |
| 125 | + Err(e) => { |
| 126 | + return Err(ModmailError::Database(DatabaseError::QueryFailed( |
| 127 | + e.to_string(), |
| 128 | + ))); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + Ok(()) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments