From 087310e94f4b5c88e6e787ae41e143aacdf9e8a0 Mon Sep 17 00:00:00 2001 From: syui Date: Tue, 13 Feb 2024 19:31:08 +0900 Subject: [PATCH] fix bot --- src/bot.rs | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 8567dbe..924722b 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,9 +1,11 @@ use seahorse::Context; +use std::process::Command; use crate::refresh; use crate::reply; use crate::reply_link; use crate::notify; +use crate::openai; use crate::notify_read; use crate::data::log_file; @@ -49,7 +51,7 @@ pub fn c_bot(c: &Context) { text = &n[i].record.text.as_ref().unwrap(); } let vec: Vec<&str> = text.split_whitespace().collect(); - let rep_com = &vec[0..].join(" "); + let com = vec[1].trim().to_string(); let handlev: Vec<&str> = handle.split('.').collect(); let mut handlev = handlev[0].trim().to_string(); @@ -57,10 +59,20 @@ pub fn c_bot(c: &Context) { let s = 0; let mut e = link.chars().count(); - use std::process::Command; + let mut prompt = "none".to_string(); + if reason == "mention" { + prompt = vec[2..].join(" "); + } else if reason == "reply" { + prompt = vec[1..].join(" "); + } + if prompt.is_empty() == false { + println!("{}", prompt); + } + if check_cid == false && { reason == "mention" || reason == "reply" } || check_cid_run == false && { reason == "mention" || reason == "reply" } { + w_cid(cid.to_string(), log_file(&"n2"), true); - if rep_com == "/did" || rep_com == "did" { + if com == "/did" || com == "did" { let link = "https://plc.directory/".to_owned() + &did + &"/log"; let s = 0; let e = link.chars().count(); @@ -71,12 +83,11 @@ pub fn c_bot(c: &Context) { w_cid(cid.to_string(), log_file(&"n1"), true); println!("{}", str_rep); } - } else if rep_com == "/diffusers" || rep_com == "diffusers" { - let prompt = &vec[2..].join(" "); + } else if com == "/diffusers" || com == "diffusers" { let _output = Command::new(data_scpt(&"diffusers")).arg(&did).arg(&prompt).output().expect("zsh"); let _output = Command::new(data_scpt(&"at_img")).arg(&cid).arg(&uri).output().expect("zsh"); w_cid(cid.to_string(), log_file(&"n1"), true); - } else if rep_com.contains("占") == true || rep_com.contains("うらない") == true || rep_com.contains("うらなって") == true { + } else if prompt.contains("占") == true || prompt.contains("うらない") == true || prompt.contains("うらなって") == true { let output = Command::new(data_scpt(&"card_fortune")).arg(&handle).arg(&did).arg(&cid).arg(&uri).output().expect("zsh"); let d = String::from_utf8_lossy(&output.stdout); let d = d.to_string(); @@ -85,8 +96,7 @@ pub fn c_bot(c: &Context) { println!("{}", text_limit); w_cid(cid.to_string(), log_file(&"n1"), true); } - } else if rep_com == "/card" || rep_com == "card" { - let prompt = &vec[2..].join(" "); + } else if com == "/card" || com == "card" { let output = Command::new(data_scpt(&"api_card.zsh")).arg(&handle).arg(&did).arg(&prompt).output().expect("zsh"); let d = String::from_utf8_lossy(&output.stdout); let dd = "\n".to_owned() + &d.to_string(); @@ -104,8 +114,7 @@ pub fn c_bot(c: &Context) { } w_cid(cid.to_string(), log_file(&"n1"), true); } - } else if rep_com == "/fav" || rep_com == "fav" { - let prompt = &vec[2..].join(" "); + } else if com == "/fav" || com == "fav" { let output = Command::new(data_scpt(&"api_fav")).arg(&handle).arg(&did).arg(&prompt).output().expect("zsh"); let d = String::from_utf8_lossy(&output.stdout); let dd = "\n".to_owned() + &d.to_string(); @@ -115,8 +124,7 @@ pub fn c_bot(c: &Context) { println!("{}", str_rep); w_cid(cid.to_string(), log_file(&"n1"), true); } - } else if rep_com == "/egg" || rep_com == "egg" { - let prompt = &vec[2..].join(" "); + } else if com == "/egg" || com == "egg" { let output = Command::new(data_scpt(&"api_egg")).arg(&handle).arg(&did).arg(&prompt).output().expect("zsh"); let d = String::from_utf8_lossy(&output.stdout); let dd = "\n".to_owned() + &d.to_string(); @@ -126,8 +134,7 @@ pub fn c_bot(c: &Context) { println!("{}", str_rep); w_cid(cid.to_string(), log_file(&"n1"), true); } - } else if rep_com == "/nyan" || rep_com == "nyan" { - let prompt = &vec[2..].join(" "); + } else if com == "/nyan" || com == "nyan" { let output = Command::new(data_scpt(&"nyancat")).arg(&handle).arg(&did).arg(&cid).arg(&uri).arg(&prompt).output().expect("zsh"); let d = String::from_utf8_lossy(&output.stdout); let dd = "\n".to_owned() + &d.to_string(); @@ -138,7 +145,7 @@ pub fn c_bot(c: &Context) { println!("{}", str_rep); w_cid(cid.to_string(), log_file(&"n1"), true); } - } else if rep_com == "/ten" || rep_com == "ten" { + } else if com == "/ten" || com == "ten" { let option = &vec[2..].join(" "); let sub_option = &vec[3..].join(" "); let output = Command::new(data_scpt(&"api_ten")).arg(&handle).arg(&did).arg(&cid).arg(&uri).arg(&option).arg(&sub_option).output().expect("zsh"); @@ -150,7 +157,14 @@ pub fn c_bot(c: &Context) { println!("{}", str_rep); w_cid(cid.to_string(), log_file(&"n1"), true); } - } + } else { + // openai + let str_openai = openai::post_request(prompt.to_string()).await; + let text_limit = c_char(str_openai); + let str_rep = reply::post_request(text_limit.to_string(), cid.to_string(), uri.to_string(), cid_root.to_string(), uri_root.to_string()).await; + println!("{}", str_rep); + w_cid(cid.to_string(), log_file(&"n1"), true); + } let str_notify = notify_read::post_request(time.to_string()).await; println!("{}", str_notify); println!("---");