Skip to content

Commit 2d7bef5

Browse files
committed
fix(reminder): correctly handle errors
1 parent 6b639de commit 2d7bef5

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

crates/rustmail/src/commands/add_reminder/text_command/add_reminder.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,18 @@ async fn handle_subscription(
241241
}
242242
};
243243

244-
let role_name_lower = role_name.to_lowercase();
245-
let role = guild
246-
.roles
247-
.values()
248-
.find(|r| r.name.to_lowercase() == role_name_lower);
244+
let mention_regex = Regex::new(r"<@&(\d+)>").unwrap();
245+
let role = if let Some(caps) = mention_regex.captures(role_name) {
246+
caps.get(1)
247+
.and_then(|m| m.as_str().parse::<u64>().ok())
248+
.and_then(|id| guild.roles.get(&RoleId::new(id)))
249+
} else {
250+
let role_name_lower = role_name.to_lowercase();
251+
guild
252+
.roles
253+
.values()
254+
.find(|r| r.name.to_lowercase() == role_name_lower)
255+
};
249256

250257
let role = match role {
251258
Some(r) => r,
@@ -282,9 +289,9 @@ async fn handle_subscription(
282289
.await?;
283290

284291
if !was_opted_out {
285-
return Err(ModmailError::Command(CommandError::ReminderAlreadySubscribed(
286-
role.name.clone(),
287-
)));
292+
return Err(ModmailError::Command(
293+
CommandError::ReminderAlreadySubscribed(role.name.clone()),
294+
));
288295
}
289296

290297
let _ = MessageBuilder::system_message(ctx, config)
@@ -308,9 +315,9 @@ async fn handle_subscription(
308315
.await?;
309316

310317
if is_already_opted_out {
311-
return Err(ModmailError::Command(CommandError::ReminderAlreadyUnsubscribed(
312-
role.name.clone(),
313-
)));
318+
return Err(ModmailError::Command(
319+
CommandError::ReminderAlreadyUnsubscribed(role.name.clone()),
320+
));
314321
}
315322

316323
insert_reminder_optout(

crates/rustmail/src/commands/reminder_subscription/slash_command/reminder_subscription.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ impl RegistrableCommand for ReminderSubscriptionCommand {
179179
let message_key = if was_opted_out {
180180
"reminder_subscription.subscribed"
181181
} else {
182-
"reminder_subscription.already_subscribed"
182+
return Err(ModmailError::Command(
183+
CommandError::ReminderAlreadySubscribed(role.name.clone()),
184+
));
183185
};
184186

185187
let _ = MessageBuilder::system_message(&ctx, &config)
@@ -203,17 +205,9 @@ impl RegistrableCommand for ReminderSubscriptionCommand {
203205
.await?;
204206

205207
if is_already_opted_out {
206-
let _ = MessageBuilder::system_message(&ctx, &config)
207-
.translated_content(
208-
"reminder_subscription.already_unsubscribed",
209-
Some(&params),
210-
Some(command.user.id),
211-
command.guild_id.map(|g| g.get()),
212-
)
213-
.await
214-
.to_channel(command.channel_id)
215-
.send_interaction_followup(&command, true)
216-
.await;
208+
return Err(ModmailError::Command(
209+
CommandError::ReminderAlreadyUnsubscribed(role.name.clone()),
210+
));
217211
} else {
218212
insert_reminder_optout(
219213
guild_id as i64,

0 commit comments

Comments
 (0)