show the discord error in place of the webhook error. removed an unwrap from interval

This commit is contained in:
jellywx 2020-12-12 18:56:50 +00:00
parent 20a3c134c0
commit 9bb969c642

View File

@ -946,7 +946,7 @@ enum ReminderError {
NotEnoughArgs, NotEnoughArgs,
InvalidTime, InvalidTime,
NeedSubscription, NeedSubscription,
DiscordError, DiscordError(String),
} }
impl std::fmt::Display for ReminderError { impl std::fmt::Display for ReminderError {
@ -974,7 +974,7 @@ impl ToResponse for ReminderError {
Self::NotEnoughArgs => "remind/no_argument", Self::NotEnoughArgs => "remind/no_argument",
Self::InvalidTime => "remind/invalid_time", Self::InvalidTime => "remind/invalid_time",
Self::NeedSubscription => "interval/donor", Self::NeedSubscription => "interval/donor",
Self::DiscordError => "remind/no_webhook", Self::DiscordError(_) => "remind/generic_error",
} }
} }
@ -1094,9 +1094,10 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
let interval_parser = captures let interval_parser = captures
.name("interval") .name("interval")
.map(|mat| TimeParser::new(mat.as_str(), user_data.timezone())) .map(|mat| TimeParser::new(mat.as_str(), user_data.timezone()))
// todo remove unwrap below .map(|parser| parser.displacement())
.map(|parser| parser.displacement().unwrap()); .transpose();
if let Ok(interval) = interval_parser {
let content = captures.name("content").map(|mat| mat.as_str()).unwrap(); let content = captures.name("content").map(|mat| mat.as_str()).unwrap();
let mut ok_locations = vec![]; let mut ok_locations = vec![];
@ -1111,7 +1112,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
msg.guild_id, msg.guild_id,
&scope, &scope,
&time_parser, &time_parser,
interval_parser, interval,
content, content,
) )
.await; .await;
@ -1171,8 +1172,14 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
}, },
err_types err_types
.iter() .iter()
.map(|err| lm.get(&user_data.language, err.to_response())) .map(|err| match err {
.collect::<Vec<&str>>() ReminderError::DiscordError(s) => lm
.get(&user_data.language, err.to_response())
.replace("{error}", &s),
_ => lm.get(&user_data.language, err.to_response()).to_string(),
})
.collect::<Vec<String>>()
.join("\n") .join("\n")
); );
@ -1189,6 +1196,20 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
}) })
}) })
.await; .await;
} else {
let _ = msg
.channel_id
.send_message(ctx, |m| {
m.embed(move |e| {
e.title("0 Reminders Set")
.description(
lm.get(&user_data.language, "interval/invalid_interval"),
)
.color(*THEME_COLOR)
})
})
.await;
}
} }
None => { None => {
@ -1472,13 +1493,17 @@ async fn create_reminder<
if let Some(guild_channel) = channel.guild() { if let Some(guild_channel) = channel.guild() {
if channel_data.webhook_token.is_none() || channel_data.webhook_id.is_none() { if channel_data.webhook_token.is_none() || channel_data.webhook_id.is_none() {
if let Ok(webhook) = create_webhook(&ctx, guild_channel, "Reminder").await { match create_webhook(&ctx, guild_channel, "Reminder").await {
Ok(webhook) => {
channel_data.webhook_id = Some(webhook.id.as_u64().to_owned()); channel_data.webhook_id = Some(webhook.id.as_u64().to_owned());
channel_data.webhook_token = Some(webhook.token); channel_data.webhook_token = Some(webhook.token);
channel_data.commit_changes(&pool).await; channel_data.commit_changes(&pool).await;
} else { }
return Err(ReminderError::DiscordError);
Err(e) => {
return Err(ReminderError::DiscordError(e.to_string()));
}
} }
} }
} }