Skip to content

Commit 00a1d6e

Browse files
committed
refactor(commands): put take/release command logic into tokio task for handle discord api limits
1 parent b6accb0 commit 00a1d6e

6 files changed

Lines changed: 95 additions & 87 deletions

File tree

rustmail/src/commands/release/slash_command/release.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,30 @@ impl RegistrableCommand for ReleaseCommand {
8888
if thread_name == thread.user_name {
8989
return Err(ModmailError::Command(CommandError::TicketAlreadyReleased));
9090
}
91-
92-
rename_channel_with_timeout(
93-
&ctx,
94-
&config,
95-
thread_id,
96-
thread.user_name.clone(),
97-
None,
98-
Some(&command),
99-
)
100-
.await?;
101-
102-
let mut params = std::collections::HashMap::new();
103-
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
104-
105-
let response = MessageBuilder::system_message(&ctx, &config)
106-
.translated_content("release.confirmation", Some(&params), None, None)
107-
.await
108-
.to_channel(command.channel_id)
109-
.build_interaction_message_followup()
110-
.await;
111-
112-
let _ = command.create_followup(ctx.clone(), response).await;
91+
92+
tokio::spawn(async move {
93+
let _ = rename_channel_with_timeout(
94+
&ctx,
95+
&config,
96+
thread_id,
97+
thread.user_name.clone(),
98+
None,
99+
Some(&command),
100+
)
101+
.await;
102+
103+
let mut params = std::collections::HashMap::new();
104+
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
105+
106+
let response = MessageBuilder::system_message(&ctx, &config)
107+
.translated_content("release.confirmation", Some(&params), None, None)
108+
.await
109+
.to_channel(command.channel_id)
110+
.build_interaction_message_followup()
111+
.await;
112+
113+
let _ = command.create_followup(ctx.clone(), response).await;
114+
});
113115

114116
Ok(())
115117
} else {

rustmail/src/commands/release/text_command/release.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,29 @@ pub async fn release(
4040
return Err(ModmailError::Command(CommandError::TicketAlreadyReleased));
4141
}
4242

43-
rename_channel_with_timeout(
44-
&ctx,
45-
&config,
46-
thread_id,
47-
thread.user_name.clone(),
48-
Some(&msg),
49-
None,
50-
)
51-
.await?;
43+
let config_clone = config.clone();
5244

53-
let mut params = std::collections::HashMap::new();
54-
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
45+
tokio::spawn(async move {
46+
let _ = rename_channel_with_timeout(
47+
&ctx,
48+
&config_clone,
49+
thread_id,
50+
thread.user_name.clone(),
51+
Some(&msg),
52+
None,
53+
)
54+
.await;
5555

56-
let _ = MessageBuilder::system_message(&ctx, config)
57-
.translated_content("release.confirmation", Some(&params), None, None)
58-
.await
59-
.to_channel(msg.channel_id)
60-
.send(true)
61-
.await?;
56+
let mut params = std::collections::HashMap::new();
57+
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
58+
59+
let _ = MessageBuilder::system_message(&ctx, &config_clone)
60+
.translated_content("release.confirmation", Some(&params), None, None)
61+
.await
62+
.to_channel(msg.channel_id)
63+
.send(true)
64+
.await;
65+
});
6266

6367
Ok(())
6468
} else {

rustmail/src/commands/take/common.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,13 @@ pub async fn rename_channel_with_timeout(
5757
None
5858
};
5959

60-
if let Err(e) = channel_id.edit(&ctx.http, EditChannel::new().name(new_name)).await {
61-
return Err(ModmailError::Discord(DiscordError::ApiError(e.to_string())));
62-
}
60+
let _ = channel_id.edit(&ctx.http, EditChannel::new().name(new_name)).await;
6361

64-
if let Some(msg) = message_response {
65-
let _ = msg.delete(&ctx.http).await;
62+
if let Some(m) = message_response {
63+
let _ = m.delete(&ctx.http).await;
6664
}
67-
68-
if let Some(command) = command_response {
69-
let _ = command.delete(&ctx.http).await;
65+
if let Some(m) = command_response {
66+
let _ = m.delete(&ctx.http).await;
7067
}
7168

7269
return Ok(());

rustmail/src/commands/take/slash_command/take.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,29 @@ impl RegistrableCommand for TakeCommand {
8989
return Err(ModmailError::Command(CommandError::TicketAlreadyTaken));
9090
}
9191

92-
rename_channel_with_timeout(
93-
&ctx,
94-
&config,
95-
thread_id,
96-
format!("🔵-{}", command.user.name.clone()),
97-
None,
98-
Some(&command),
99-
)
100-
.await?;
101-
102-
let mut params = std::collections::HashMap::new();
103-
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
104-
105-
let response = MessageBuilder::system_message(&ctx, &config)
106-
.translated_content("take.confirmation", Some(&params), None, None)
107-
.await
108-
.to_channel(command.channel_id)
109-
.build_interaction_message_followup()
110-
.await;
111-
112-
let _ = command.create_followup(ctx.clone(), response).await;
92+
tokio::spawn(async move {
93+
let _ = rename_channel_with_timeout(
94+
&ctx,
95+
&config,
96+
thread_id,
97+
format!("🔵-{}", command.user.name.clone()),
98+
None,
99+
Some(&command),
100+
)
101+
.await;
102+
103+
let mut params = std::collections::HashMap::new();
104+
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
105+
106+
let response = MessageBuilder::system_message(&ctx, &config)
107+
.translated_content("take.confirmation", Some(&params), None, None)
108+
.await
109+
.to_channel(command.channel_id)
110+
.build_interaction_message_followup()
111+
.await;
112+
113+
let _ = command.create_followup(ctx.clone(), response).await;
114+
});
113115

114116
Ok(())
115117
} else {

rustmail/src/commands/take/text_command/take.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,30 @@ pub async fn take(
3939
if thread_name == format!("🔵-{}", msg.author.name) {
4040
return Err(ModmailError::Command(CommandError::TicketAlreadyTaken));
4141
}
42+
43+
let config_clone = config.clone();
44+
45+
tokio::spawn(async move {
46+
let _ = rename_channel_with_timeout(
47+
&ctx,
48+
&config_clone,
49+
thread_id,
50+
format!("🔵-{}", msg.author.name.clone()),
51+
Some(&msg),
52+
None,
53+
)
54+
.await;
4255

43-
rename_channel_with_timeout(
44-
&ctx,
45-
config,
46-
thread_id,
47-
format!("🔵-{}", msg.author.name.clone()),
48-
Some(&msg),
49-
None,
50-
)
51-
.await?;
56+
let mut params = std::collections::HashMap::new();
57+
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
5258

53-
let mut params = std::collections::HashMap::new();
54-
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
55-
56-
let _ = MessageBuilder::system_message(&ctx, config)
57-
.translated_content("take.confirmation", Some(&params), None, None)
58-
.await
59-
.to_channel(msg.channel_id)
60-
.send(true)
61-
.await?;
59+
let _ = MessageBuilder::system_message(&ctx, &config_clone)
60+
.translated_content("take.confirmation", Some(&params), None, None)
61+
.await
62+
.to_channel(msg.channel_id)
63+
.send(true)
64+
.await;
65+
});
6266

6367
Ok(())
6468
} else {

rustmail/src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ where
6565
}
6666

6767
fn default_timezone() -> Tz {
68-
println!("yes");
6968
chrono_tz::UTC
7069
}
7170

0 commit comments

Comments
 (0)