Skip to content

Commit 56e3c68

Browse files
authored
Merge pull request #168 from Rustmail/159-extend-delete-command-to-work-with-message-context-menus
feat(commands): add message context to delete command
2 parents 40aaea5 + 7116756 commit 56e3c68

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

src/commands/delete/slash_command/delete.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use crate::commands::delete::common::{
44
};
55
use crate::commands::{BoxFuture, RegistrableCommand};
66
use crate::config::Config;
7+
use crate::db::messages::get_thread_message_by_message_id;
78
use crate::errors::{MessageError, ModmailError, ModmailResult, common};
89
use crate::i18n::get_translated_message;
910
use crate::utils::command::defer_response::defer_response_ephemeral;
1011
use crate::utils::message::message_builder::MessageBuilder;
1112
use serenity::all::{
12-
CommandDataOptionValue, CommandInteraction, CommandOptionType, Context, CreateCommand,
13-
CreateCommandOption, ResolvedOption,
13+
CommandDataOptionValue, CommandInteraction, CommandOptionType, CommandType, Context,
14+
CreateCommand, CreateCommandOption, ResolvedOption,
1415
};
1516
use std::collections::HashMap;
1617

@@ -56,6 +57,7 @@ impl RegistrableCommand for DeleteCommand {
5657
)
5758
.required(true),
5859
),
60+
CreateCommand::new("delete").kind(CommandType::Message),
5961
]
6062
})
6163
}
@@ -95,9 +97,32 @@ impl RegistrableCommand for DeleteCommand {
9597
let (user_id, thread) = get_thread_info(&command.channel_id.to_string(), pool).await?;
9698

9799
if message_number < 0 {
98-
return Err(ModmailError::Message(MessageError::MessageNotFound(
99-
"".to_string(),
100-
)));
100+
if let Some(message_id) = &command.data.target_id {
101+
let thread_message = match get_thread_message_by_message_id(
102+
&message_id.to_message_id().to_string(),
103+
pool,
104+
)
105+
.await
106+
{
107+
Ok(thread_message) => thread_message,
108+
Err(e) => {
109+
return Err(ModmailError::Message(MessageError::MessageNotFound(
110+
e.to_string(),
111+
)));
112+
}
113+
};
114+
if let Some(msg_number) = thread_message.message_number {
115+
message_number = msg_number;
116+
} else {
117+
return Err(ModmailError::Message(MessageError::MessageNotFound(
118+
"".to_string(),
119+
)));
120+
}
121+
} else {
122+
return Err(ModmailError::Message(MessageError::MessageNotFound(
123+
"".to_string(),
124+
)));
125+
}
101126
}
102127

103128
let message_ids = get_message_ids(user_id, &thread, message_number, pool).await?;

0 commit comments

Comments
 (0)