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,114 +1144,139 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
) )
.await; .await;
if let Ok(mut content) = content_res { match content_res {
let mut ok_locations = vec![]; Ok(mut content) => {
let mut err_locations = vec![]; let mut ok_locations = vec![];
let mut err_types = HashSet::new(); let mut err_locations = vec![];
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, interval,
&mut content, &mut 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| 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)) .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; }
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"),
) )
.color(*THEME_COLOR) .description(lm.get(&user_data.language, "interval/invalid_interval"))
.color(*THEME_COLOR)
}) })
}) })
.await; .await;
@ -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 { match content_res {
let location_id = location_ids.get(0).unwrap(); Ok(mut content) => {
let offset = timestamp as u64 - since_epoch.as_secs();
let res = create_reminder( let mut ok_locations = vec![];
&ctx, let mut err_locations = vec![];
&pool, let mut err_types = HashSet::new();
msg.author.id,
msg.guild_id,
&location_id,
timestamp,
interval,
&mut content,
)
.await;
let offset = timestamp as u64 - since_epoch.as_secs(); for scope in location_ids {
let res = create_reminder(
&ctx,
&pool,
msg.author.id,
msg.guild_id,
&scope,
timestamp,
interval,
&mut content,
)
.await;
let str_response = lm if let Err(e) = res {
.get(&user_data.language, &res.to_response_natural()) err_locations.push(scope);
.replace( err_types.insert(e);
"{prefix}", } else {
&GuildData::prefix_from_id(msg.guild_id, &pool).await, ok_locations.push(scope);
) }
.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;
} else {
let mut ok_count = 0_u8;
for location in location_ids {
let res = create_reminder(
&ctx,
&pool,
msg.author.id,
msg.guild_id,
&location,
timestamp,
interval,
&mut content,
)
.await;
if res.is_ok() {
ok_count += 1;
} }
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(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 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;
} }
let content = lm Err(content_error) => {
.get(&user_data.language, "natural/bulk_set") let _ = msg
.replace("{}", &ok_count.to_string()); .channel_id
.send_message(ctx, |m| {
let _ = msg.channel_id.say(&ctx, content).await; 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