commit daf09e376707b134a362f74f9a2815a88cc1dfee
parent 226f30cefaf3cf608a8453aa5581a33e1d084e82
Author: tomvig38@gmail.com <tomvig38@gmail.com>
Date: Sun, 17 Oct 2021 08:07:08 +0000
Aldonu traduk'komandon
Diffstat:
M | src/main.rs | | | 58 | +++++++++++++++++++++++++++++++++++++++++++++++++++++----- |
1 file changed, 53 insertions(+), 5 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -29,7 +29,7 @@ struct Traduko {
#[derive(Deserialize, Debug)]
struct Trovo {
malpreciza: Vec<String>,
- vortfarade: Vortfarado,
+ vortfarado: Vec<Vortfarado>,
preciza: Vec<String>,
tradukoj: Vec<Traduko>,
}
@@ -80,7 +80,7 @@ async fn main() -> irc::error::Result<()> {
if let Some(cmd) = msg.strip_prefix('!') {
if let Some(answer) = handle_command(cmd).await {
let r = match (target.starts_with('#'), message.source_nickname()) {
- (true, Some(n)) => format!("{}: {}", n, answer),
+ (true, Some(n)) => format!("{}:\r\n{}", n, answer),
_ => answer
};
sender.send_privmsg(target, r)?;
@@ -103,16 +103,26 @@ async fn handle_command(cmd: &str) -> Option<String> {
difinu [vorto]: difini vorton
"#,
)),
- Some("difini" | "d") => {
+ Some("difinu" | "d") => {
if let Some(w) = splitted.next() {
match define_word(w).await {
Ok(r) => Some(r),
Err(e) => Some(format!("Nenio trovata pri: {} ({})", w, e.to_string())),
}
} else {
- Some(String::from("Uzo: difini [vorto]"))
+ Some(String::from("Uzo: difinu [vorto]"))
}
- }
+ },
+ Some("traduku" | "trad" | "t") => {
+ if let Some(w) = splitted.next() {
+ match traduki(w, splitted.next()).await {
+ Ok(r) => Some(r),
+ Err(e) => Some(format!("Nenio trovata pri: {} ({})", w, e.to_string())),
+ }
+ } else {
+ Some(String::from("Uzo: traduku [vorto]"))
+ }
+ },
Some(u) => Some(format!("Mi ne scias kio respondi al: {}", u)),
None => None,
}
@@ -132,3 +142,41 @@ async fn define_word(word: &str) -> Result<String, String> {
Err(format!("{} ne havas difino.", word))
}
}
+
+async fn traduki(vorto: &str, fonto: Option<&str>) -> Result<String, String> {
+ let res = reqwest::get(format!("{}/{}", TROVI_URL, vorto))
+ .await
+ .map_err(|e| e.to_string())?
+ .json::<Trovo>()
+ .await
+ .map_err(|e| e.to_string())?;
+
+ if res.tradukoj.is_empty() {
+ return Err(format!("Nenio traduko trovita por {}", vorto));
+ }
+
+ let tradukoj = res.tradukoj.iter();
+ let tradukoj: Vec<String> = if let Some(f) = fonto {
+ tradukoj.filter_map(|t: &Traduko| {
+ if let Some(v) = &t.vorto {
+ if f == t.kodo {
+ Some(v.clone())
+ } else {
+ None
+ }
+ } else {
+ None
+ }
+ }).collect()
+ } else {
+ tradukoj.filter_map(|t: &Traduko| {
+ if let Some(v) = &t.vorto {
+ Some(format!("{} \"{}\": {}", t.lingvo, t.traduko, v))
+ } else {
+ None
+ }
+ }).collect()
+ };
+
+ Ok(tradukoj.join("\r\n"))
+}