|
| 1 | +use crate::commands::{BoxFuture, RegistrableCommand}; |
| 2 | +use crate::config::Config; |
| 3 | +use crate::db::threads::is_a_ticket_channel; |
| 4 | +use crate::db::get_thread_by_channel_id; |
| 5 | +use crate::errors::common::{database_connection_failed, thread_not_found}; |
| 6 | +use crate::errors::ThreadError::NotAThreadChannel; |
| 7 | +use crate::errors::{CommandError, ModmailError, ModmailResult}; |
| 8 | +use crate::handlers::guild_interaction_handler::InteractionHandler; |
| 9 | +use crate::i18n::get_translated_message; |
| 10 | +use crate::utils::command::defer_response::defer_response; |
| 11 | +use crate::utils::message::message_builder::MessageBuilder; |
| 12 | +use serenity::all::{ |
| 13 | + ChannelId, CommandInteraction, Context, CreateCommand, EditChannel, ResolvedOption, |
| 14 | +}; |
| 15 | +use serenity::FutureExt; |
| 16 | +use std::sync::Arc; |
| 17 | + |
| 18 | +pub struct TakeCommand; |
| 19 | + |
| 20 | +#[async_trait::async_trait] |
| 21 | +impl RegistrableCommand for TakeCommand { |
| 22 | + fn name(&self) -> &'static str { |
| 23 | + "take" |
| 24 | + } |
| 25 | + |
| 26 | + fn doc<'a>(&self, config: &'a Config) -> BoxFuture<'a, String> { |
| 27 | + async move { get_translated_message(config, "help.take", None, None, None, None).await } |
| 28 | + .boxed() |
| 29 | + } |
| 30 | + |
| 31 | + fn register(&self, config: &Config) -> BoxFuture<'_, Vec<CreateCommand>> { |
| 32 | + let config = config.clone(); |
| 33 | + |
| 34 | + Box::pin(async move { |
| 35 | + let cmd_desc = get_translated_message( |
| 36 | + &config, |
| 37 | + "slash_command.help_command_description", |
| 38 | + None, |
| 39 | + None, |
| 40 | + None, |
| 41 | + None, |
| 42 | + ) |
| 43 | + .await; |
| 44 | + |
| 45 | + vec![CreateCommand::new(self.name()).description(cmd_desc)] |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + fn run( |
| 50 | + &self, |
| 51 | + ctx: &Context, |
| 52 | + command: &CommandInteraction, |
| 53 | + _options: &[ResolvedOption<'_>], |
| 54 | + config: &Config, |
| 55 | + _handler: Arc<InteractionHandler>, |
| 56 | + ) -> BoxFuture<'_, ModmailResult<()>> { |
| 57 | + let ctx = ctx.clone(); |
| 58 | + let command = command.clone(); |
| 59 | + let config = config.clone(); |
| 60 | + |
| 61 | + Box::pin(async move { |
| 62 | + let db_pool = config |
| 63 | + .db_pool |
| 64 | + .as_ref() |
| 65 | + .ok_or_else(database_connection_failed)?; |
| 66 | + |
| 67 | + defer_response(&ctx, &command).await?; |
| 68 | + |
| 69 | + if is_a_ticket_channel(command.channel_id, &db_pool).await { |
| 70 | + let thread = match get_thread_by_channel_id( |
| 71 | + &command.channel_id.to_string(), |
| 72 | + db_pool, |
| 73 | + ) |
| 74 | + .await |
| 75 | + { |
| 76 | + Some(thread) => thread, |
| 77 | + None => return Err(thread_not_found()), |
| 78 | + }; |
| 79 | + |
| 80 | + let parse_thread_id = thread.channel_id.parse::<u64>().unwrap_or(0); |
| 81 | + |
| 82 | + let thread_id = ChannelId::new(parse_thread_id); |
| 83 | + |
| 84 | + let thread_name = thread_id |
| 85 | + .name(&ctx) |
| 86 | + .await |
| 87 | + .unwrap_or_else(|_| "Unknown".to_string()); |
| 88 | + |
| 89 | + if thread_name == format!("🔵-{}", command.user.name) { |
| 90 | + return Err(ModmailError::Command(CommandError::TicketAlreadyTaken)); |
| 91 | + } |
| 92 | + |
| 93 | + let _ = thread_id |
| 94 | + .edit( |
| 95 | + &ctx.http, |
| 96 | + EditChannel::new().name(format!("🔵-{}", command.user.name)), |
| 97 | + ) |
| 98 | + .await; |
| 99 | + |
| 100 | + let mut params = std::collections::HashMap::new(); |
| 101 | + params.insert("staff".to_string(), format!("<@{}>", command.user.id)); |
| 102 | + |
| 103 | + let response = MessageBuilder::system_message(&ctx, &config) |
| 104 | + .translated_content("take.confirmation", Some(¶ms), None, None) |
| 105 | + .await |
| 106 | + .to_channel(command.channel_id) |
| 107 | + .build_interaction_message_followup() |
| 108 | + .await; |
| 109 | + |
| 110 | + let _ = command.create_followup(ctx.clone(), response).await; |
| 111 | + |
| 112 | + Ok(()) |
| 113 | + } else { |
| 114 | + Err(ModmailError::Thread(NotAThreadChannel)) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments