Fix cleverbot error handling

This commit is contained in:
Sakuhl 2018-03-07 14:53:31 +01:00
parent d6c59bb1d1
commit 4669d37a71

View File

@ -156,6 +156,18 @@ struct CBResponse {
output: String output: String
} }
fn cleverbot(msg: &Message) -> Result<String, Error> {
let _ = msg.channel_id.broadcast_typing();
let resp = reqwest::get(&format!("http://www.cleverbot.com/getreply?key=CC7wgO9gQl7yPjB54eDLHmM5Jmg&input={}&cb_settings_tweak1=0&cb_settings_tweak2=100&cb_settings_tweak3=100{}",
msg.content.replace("<@392763365409292298>", ""), (*CS).lock().clone().map(|x| format!("&cs={}", x)).unwrap_or_else(String::new)))?
.error_for_status()?
.text()?;
thread::sleep_ms(500);
let resp: CBResponse = serde_json::from_str(&resp)?;
msg.channel_id.say(format!("{} {}", msg.author.mention(), resp.output))?;
Ok(resp.cs)
}
impl Handler { impl Handler {
fn process_message(&self, msg: &Message, prefix: &str) -> Result<(), Error> { fn process_message(&self, msg: &Message, prefix: &str) -> Result<(), Error> {
if msg.content == "<@392763365409292298> has big gay" { if msg.content == "<@392763365409292298> has big gay" {
@ -171,16 +183,14 @@ impl Handler {
return Ok(()); return Ok(());
} }
if msg.content.contains("<@392763365409292298>") { if msg.content.contains("<@392763365409292298>") {
let _ = msg.channel_id.broadcast_typing(); if let Ok(cs) = cleverbot(msg) {
let resp = reqwest::get(&format!("http://www.cleverbot.com/getreply?key=CC7wgO9gQl7yPjB54eDLHmM5Jmg&input={}&cb_settings_tweak1=0&cb_settings_tweak2=100&cb_settings_tweak3=100{}", *(*CS).lock() = Some(cs);
msg.content.replace("<@392763365409292298>", ""), (*CS).lock().clone().map(|x| format!("&cs={}", x)).unwrap_or_else(String::new)))? return Ok(());
.error_for_status()? } else {
.text()?; *(*CS).lock() = None;
thread::sleep_ms(500); self.process_message(msg, prefix)?;
let resp: CBResponse = serde_json::from_str(&resp)?; return Ok(());
*(*CS).lock() = Some(resp.cs); }
msg.channel_id.say(format!("{} {}", msg.author.mention(), resp.output))?;
return Ok(());
} }
if !msg.content.starts_with(&prefix) { if !msg.content.starts_with(&prefix) {
return Ok(()); return Ok(());