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
}
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 {
fn process_message(&self, msg: &Message, prefix: &str) -> Result<(), Error> {
if msg.content == "<@392763365409292298> has big gay" {
@ -171,16 +183,14 @@ impl Handler {
return Ok(());
}
if msg.content.contains("<@392763365409292298>") {
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)?;
*(*CS).lock() = Some(resp.cs);
msg.channel_id.say(format!("{} {}", msg.author.mention(), resp.output))?;
return Ok(());
if let Ok(cs) = cleverbot(msg) {
*(*CS).lock() = Some(cs);
return Ok(());
} else {
*(*CS).lock() = None;
self.process_message(msg, prefix)?;
return Ok(());
}
}
if !msg.content.starts_with(&prefix) {
return Ok(());