ported all changes to natural command also

This commit is contained in:
jellywx 2020-12-16 22:35:51 +00:00
parent e002984986
commit aebb8529b6

View File

@ -983,6 +983,16 @@ enum ContentError {
AttachmentDownloadFailed, AttachmentDownloadFailed,
} }
impl ContentError {
fn to_response(&self) -> &'static str {
match self {
ContentError::TooManyAttachments => "remind/too_many_attachments",
ContentError::AttachmentTooLarge => "remind/attachment_too_large",
ContentError::AttachmentDownloadFailed => "remind/attachment_download_failed",
}
}
}
impl std::fmt::Display for ContentError { impl std::fmt::Display for ContentError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self) write!(f, "{:?}", self)
@ -1134,7 +1144,8 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
) )
.await; .await;
if let Ok(mut content) = content_res { match content_res {
Ok(mut content) => {
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();
@ -1167,7 +1178,9 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
.replace("{location}", &ok_locations[0].mention()) .replace("{location}", &ok_locations[0].mention())
.replace( .replace(
"{offset}", "{offset}",
&shorthand_displacement(time_parser.displacement().unwrap() as u64), &shorthand_displacement(
time_parser.displacement().unwrap() as u64
),
), ),
n => lm n => lm
.get(&user_data.language, "remind/success_bulk") .get(&user_data.language, "remind/success_bulk")
@ -1182,7 +1195,9 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
) )
.replace( .replace(
"{offset}", "{offset}",
&shorthand_displacement(time_parser.displacement().unwrap() as u64), &shorthand_displacement(
time_parser.displacement().unwrap() as u64
),
), ),
}; };
@ -1232,15 +1247,35 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
}) })
.await; .await;
} }
Err(content_error) => {
let _ = msg
.channel_id
.send_message(ctx, |m| {
m.embed(move |e| {
e.title(
lm.get(&user_data.language, "remind/title")
.replace("{number}", "0"),
)
.description(
lm.get(&user_data.language, content_error.to_response()),
)
.color(*THEME_COLOR)
})
})
.await;
}
}
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.send_message(ctx, |m| { .send_message(ctx, |m| {
m.embed(move |e| { m.embed(move |e| {
e.title("0 Reminders Set") e.title(
.description( lm.get(&user_data.language, "remind/title")
lm.get(&user_data.language, "interval/invalid_interval"), .replace("{number}", "0"),
) )
.description(lm.get(&user_data.language, "interval/invalid_interval"))
.color(*THEME_COLOR) .color(*THEME_COLOR)
}) })
}) })
@ -1403,64 +1438,123 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
} }
} }
// todo remove this unwrap let content_res = Content::build(&content, msg).await;
let mut content = Content::build(&content, msg).await.unwrap();
if location_ids.len() == 1 {
let location_id = location_ids.get(0).unwrap();
let res = create_reminder(
&ctx,
&pool,
msg.author.id,
msg.guild_id,
&location_id,
timestamp,
interval,
&mut content,
)
.await;
match content_res {
Ok(mut content) => {
let offset = timestamp as u64 - since_epoch.as_secs(); let offset = timestamp as u64 - since_epoch.as_secs();
let str_response = lm let mut ok_locations = vec![];
.get(&user_data.language, &res.to_response_natural()) let mut err_locations = vec![];
.replace( let mut err_types = HashSet::new();
"{prefix}",
&GuildData::prefix_from_id(msg.guild_id, &pool).await,
)
.replacen("{location}", &location_id.mention(), 1)
.replacen("{offset}", &shorthand_displacement(offset), 1)
.replacen("{min_interval}", &MIN_INTERVAL.to_string(), 1)
.replacen("{max_time}", &MAX_TIME.to_string(), 1);
let _ = msg.channel_id.say(&ctx, &str_response).await; for scope in location_ids {
} else {
let mut ok_count = 0_u8;
for location in location_ids {
let res = create_reminder( let res = create_reminder(
&ctx, &ctx,
&pool, &pool,
msg.author.id, msg.author.id,
msg.guild_id, msg.guild_id,
&location, &scope,
timestamp, timestamp,
interval, interval,
&mut content, &mut content,
) )
.await; .await;
if res.is_ok() { if let Err(e) = res {
ok_count += 1; err_locations.push(scope);
err_types.insert(e);
} else {
ok_locations.push(scope);
} }
} }
let content = lm let success_part = match ok_locations.len() {
.get(&user_data.language, "natural/bulk_set") 0 => "".to_string(),
.replace("{}", &ok_count.to_string()); 1 => lm
.get(&user_data.language, "remind/success")
.replace("{location}", &ok_locations[0].mention())
.replace("{offset}", &shorthand_displacement(offset)),
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("{offset}", &shorthand_displacement(offset)),
};
let _ = msg.channel_id.say(&ctx, content).await; 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_natural())
.replace("{error}", &s),
_ => lm
.get(&user_data.language, err.to_response_natural())
.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;
}
Err(content_error) => {
let _ = msg
.channel_id
.send_message(ctx, |m| {
m.embed(move |e| {
e.title(
lm.get(&user_data.language, "remind/title")
.replace("{number}", "0"),
)
.description(
lm.get(&user_data.language, content_error.to_response()),
)
.color(*THEME_COLOR)
})
})
.await;
}
} }
} else { } else {
let _ = msg let _ = msg