Skip to content

Commit 6e7f76c

Browse files
authored
Merge pull request #147 from Rustmail/convert_to_slash_commands
feat(commands): convert all text commands to slash commands
2 parents cf07d27 + 4c6d52d commit 6e7f76c

98 files changed

Lines changed: 3297 additions & 1206 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.sqlx/query-b66e338803cb2416fc50708e8f76a62b6fa777fdaec8a293806b1fd0ba0dc179.json renamed to .sqlx/query-bfdec0459ceac78eb020cecca945937cb134188aef7633582c9da2dfb1f74111.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/add_staff/common.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::config::Config;
2+
use crate::errors::ModmailResult;
3+
use serenity::all::{
4+
ChannelId, Context, Message, PermissionOverwrite, PermissionOverwriteType, UserId,
5+
};
6+
use serenity::model::Permissions;
7+
8+
pub async fn add_user_to_channel(
9+
ctx: &Context,
10+
channel_id: ChannelId,
11+
user_id: UserId,
12+
) -> ModmailResult<()> {
13+
let allow = Permissions::VIEW_CHANNEL | Permissions::SEND_MESSAGES;
14+
15+
channel_id
16+
.create_permission(
17+
&ctx.http,
18+
PermissionOverwrite {
19+
allow,
20+
deny: Permissions::empty(),
21+
kind: PermissionOverwriteType::Member(user_id),
22+
},
23+
)
24+
.await?;
25+
26+
Ok(())
27+
}
28+
29+
pub async fn extract_user_id(msg: &Message, config: &Config) -> String {
30+
let content = msg.content.trim();
31+
let prefix = &config.command.prefix;
32+
let command_names = ["add_staff", "as"];
33+
34+
if command_names
35+
.iter()
36+
.any(|&name| content.starts_with(&format!("{}{}", prefix, name)))
37+
{
38+
let start = prefix.len() + command_names[0].len();
39+
content[start..].trim().to_string()
40+
} else {
41+
String::new()
42+
}
43+
}

src/commands/add_staff/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod common;
2+
pub mod slash_command;
3+
pub mod text_command;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use crate::commands::add_staff::common::add_user_to_channel;
2+
use crate::config::Config;
3+
use crate::db::thread_exists;
4+
use crate::errors::CommandError::InvalidFormat;
5+
use crate::errors::ThreadError::NotAThreadChannel;
6+
use crate::errors::{CommandError, ModmailError, ModmailResult, common};
7+
use crate::i18n::get_translated_message;
8+
use crate::utils::command::defer_response::defer_response;
9+
use crate::utils::message::message_builder::MessageBuilder;
10+
use serenity::all::{
11+
CommandDataOptionValue, CommandInteraction, CommandOptionType, Context, CreateCommand,
12+
CreateCommandOption, ResolvedOption,
13+
};
14+
use std::collections::HashMap;
15+
16+
pub async fn register(config: &Config) -> CreateCommand {
17+
let cmd_desc = get_translated_message(
18+
config,
19+
"slash_command.add_staff_command_description",
20+
None,
21+
None,
22+
None,
23+
None,
24+
)
25+
.await;
26+
let user_id_desc = get_translated_message(
27+
config,
28+
"slash_command.add_staff_user_id_argument",
29+
None,
30+
None,
31+
None,
32+
None,
33+
)
34+
.await;
35+
36+
CreateCommand::new("add_staff")
37+
.description(cmd_desc)
38+
.add_option(
39+
CreateCommandOption::new(CommandOptionType::User, "user_id", user_id_desc)
40+
.required(true),
41+
)
42+
}
43+
44+
pub async fn run(
45+
ctx: &Context,
46+
command: &CommandInteraction,
47+
_options: &[ResolvedOption<'_>],
48+
config: &Config,
49+
) -> ModmailResult<()> {
50+
let pool = config
51+
.db_pool
52+
.as_ref()
53+
.ok_or_else(common::database_connection_failed)?;
54+
55+
defer_response(&ctx, &command).await?;
56+
57+
let user_id = match command
58+
.data
59+
.options
60+
.iter()
61+
.find(|opt| opt.name == "user_id")
62+
{
63+
Some(opt) => match &opt.value {
64+
CommandDataOptionValue::User(user_id) => *user_id,
65+
_ => {
66+
return Err(ModmailError::Command(CommandError::InvalidArguments(
67+
"user_id".to_string(),
68+
)));
69+
}
70+
},
71+
None => return Err(ModmailError::Command(CommandError::MissingArguments)),
72+
};
73+
74+
if thread_exists(command.user.id, pool).await {
75+
match add_user_to_channel(ctx, command.channel_id, user_id).await {
76+
Ok(_) => {
77+
let mut params = HashMap::new();
78+
params.insert("user".to_string(), format!("<@{}>", user_id));
79+
80+
let response = MessageBuilder::system_message(ctx, config)
81+
.translated_content("add_staff.add_success", Some(&params), None, None)
82+
.await
83+
.to_channel(command.channel_id)
84+
.build_interaction_message_followup()
85+
.await;
86+
87+
let _ = command.create_followup(&ctx.http, response).await;
88+
89+
Ok(())
90+
}
91+
Err(..) => Err(ModmailError::Command(InvalidFormat)),
92+
}
93+
} else {
94+
Err(ModmailError::Thread(NotAThreadChannel))
95+
}
96+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod add_staff;

src/commands/add_staff.rs renamed to src/commands/add_staff/text_command/add_staff.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,14 @@
1+
use crate::commands::add_staff::common::add_user_to_channel;
2+
use crate::commands::add_staff::common::extract_user_id;
13
use crate::config::Config;
24
use crate::db::thread_exists;
35
use crate::errors::CommandError::InvalidFormat;
46
use crate::errors::ThreadError::NotAThreadChannel;
57
use crate::errors::{ModmailError, ModmailResult, common};
68
use crate::utils::message::message_builder::MessageBuilder;
7-
use serenity::all::{
8-
ChannelId, Context, Message, PermissionOverwrite, PermissionOverwriteType, UserId,
9-
};
10-
use serenity::model::Permissions;
9+
use serenity::all::{Context, Message, UserId};
1110
use std::collections::HashMap;
1211

13-
async fn add_user_to_channel(
14-
ctx: &Context,
15-
channel_id: ChannelId,
16-
user_id: UserId,
17-
) -> ModmailResult<()> {
18-
let allow = Permissions::VIEW_CHANNEL | Permissions::SEND_MESSAGES;
19-
20-
channel_id
21-
.create_permission(
22-
&ctx.http,
23-
PermissionOverwrite {
24-
allow,
25-
deny: Permissions::empty(),
26-
kind: PermissionOverwriteType::Member(user_id),
27-
},
28-
)
29-
.await?;
30-
31-
Ok(())
32-
}
33-
34-
async fn extract_user_id(msg: &Message, config: &Config) -> String {
35-
let content = msg.content.trim();
36-
let prefix = &config.command.prefix;
37-
let command_names = ["add_staff", "as"];
38-
39-
if command_names
40-
.iter()
41-
.any(|&name| content.starts_with(&format!("{}{}", prefix, name)))
42-
{
43-
let start = prefix.len() + command_names[0].len();
44-
content[start..].trim().to_string()
45-
} else {
46-
String::new()
47-
}
48-
}
49-
5012
pub async fn add_staff(ctx: &Context, msg: &Message, config: &Config) -> ModmailResult<()> {
5113
let pool = config
5214
.db_pool
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod add_staff;

src/commands/alert.rs

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)