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,101 +1094,122 @@ 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();
let content = captures.name("content").map(|mat| mat.as_str()).unwrap(); if let Ok(interval) = interval_parser {
let content = captures.name("content").map(|mat| mat.as_str()).unwrap();
let mut ok_locations = vec![]; let mut ok_locations = vec![];
let mut err_locations = vec![]; let mut err_locations = vec![];
let mut err_types = HashSet::new(); let mut err_types = HashSet::new();
for scope in scopes { for scope in scopes {
let res = create_reminder( let res = create_reminder(
&ctx, &ctx,
&pool, &pool,
msg.author.id, msg.author.id,
msg.guild_id, msg.guild_id,
&scope, &scope,
&time_parser, &time_parser,
interval_parser, interval,
content, content,
)
.await;
if let Err(e) = res {
err_locations.push(scope);
err_types.insert(e);
} else {
ok_locations.push(scope);
}
}
let success_part = match ok_locations.len() {
0 => "".to_string(),
1 => lm
.get(&user_data.language, "remind/success")
.replace("{location}", &ok_locations[0].mention())
.replace(
"{offset}",
&shorthand_displacement(time_parser.displacement().unwrap() as u64),
),
n => lm
.get(&user_data.language, "remind/success_bulk")
.replace("{number}", &n.to_string())
.replace(
"{location}",
&ok_locations
.iter()
.map(|l| l.mention())
.collect::<Vec<String>>()
.join(", "),
) )
.replace( .await;
"{offset}",
&shorthand_displacement(time_parser.displacement().unwrap() as u64),
),
};
let error_part = format!( if let Err(e) = res {
"{}\n{}", err_locations.push(scope);
match err_locations.len() { err_types.insert(e);
} else {
ok_locations.push(scope);
}
}
let success_part = match ok_locations.len() {
0 => "".to_string(), 0 => "".to_string(),
1 => lm 1 => lm
.get(&user_data.language, "remind/issue") .get(&user_data.language, "remind/success")
.replace("{location}", &err_locations[0].mention()), .replace("{location}", &ok_locations[0].mention())
.replace(
"{offset}",
&shorthand_displacement(time_parser.displacement().unwrap() as u64),
),
n => lm n => lm
.get(&user_data.language, "remind/issue_bulk") .get(&user_data.language, "remind/success_bulk")
.replace("{number}", &n.to_string()) .replace("{number}", &n.to_string())
.replace( .replace(
"{location}", "{location}",
&err_locations &ok_locations
.iter() .iter()
.map(|l| l.mention()) .map(|l| l.mention())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", "), .join(", "),
),
},
err_types
.iter()
.map(|err| lm.get(&user_data.language, err.to_response()))
.collect::<Vec<&str>>()
.join("\n")
);
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
e.title(
lm.get(&user_data.language, "remind/title")
.replace("{number}", &ok_locations.len().to_string()),
) )
.description(format!("{}\n\n{}", success_part, error_part)) .replace(
.color(*THEME_COLOR) "{offset}",
&shorthand_displacement(time_parser.displacement().unwrap() as u64),
),
};
let error_part = format!(
"{}\n{}",
match err_locations.len() {
0 => "".to_string(),
1 => lm
.get(&user_data.language, "remind/issue")
.replace("{location}", &err_locations[0].mention()),
n => lm
.get(&user_data.language, "remind/issue_bulk")
.replace("{number}", &n.to_string())
.replace(
"{location}",
&err_locations
.iter()
.map(|l| l.mention())
.collect::<Vec<String>>()
.join(", "),
),
},
err_types
.iter()
.map(|err| match err {
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")
);
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
e.title(
lm.get(&user_data.language, "remind/title")
.replace("{number}", &ok_locations.len().to_string()),
)
.description(format!("{}\n\n{}", success_part, error_part))
.color(*THEME_COLOR)
})
}) })
}) .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 {
channel_data.webhook_id = Some(webhook.id.as_u64().to_owned()); Ok(webhook) => {
channel_data.webhook_token = Some(webhook.token); channel_data.webhook_id = Some(webhook.id.as_u64().to_owned());
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()));
}
} }
} }
} }