From a9c4d1a65cc346d537fa6867cb7d528bc5d91679 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Mon, 12 Aug 2024 15:42:19 -0700 Subject: [PATCH] Add emoji counter --- crates/chatbot/src/lib.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/chatbot/src/lib.rs b/crates/chatbot/src/lib.rs index 7ec2afc..8f90b82 100644 --- a/crates/chatbot/src/lib.rs +++ b/crates/chatbot/src/lib.rs @@ -20,27 +20,30 @@ pub async fn gen_random_number() -> usize { /// A chatbot that responds to inputs. pub struct Chatbot { - emoji: String, + emojis: Vec, + emoji_counter: usize, } impl Chatbot { /// Creates a new chatbot that uses the provided emoji in its responses. - pub fn new(emoji: String) -> Self { - Chatbot { emoji } + pub fn new(emojis: Vec) -> Self { + Chatbot { + emojis, + emoji_counter: 0, + } } /// Generates a list of possible responses given the current chat. /// /// Warning: may take a few seconds! - pub async fn query_chat(&self, messages: &[String]) -> Vec { + pub async fn query_chat(&mut self, messages: &[String]) -> Vec { std::thread::sleep(Duration::from_secs(2)); let most_recent = messages.last().unwrap(); + let emoji = &self.emojis[self.emoji_counter]; + self.emoji_counter = (self.emoji_counter + 1) % self.emojis.len(); vec![ - format!( - "\"{most_recent}\"? And how does that make you feel? {}", - self.emoji - ), - format!("\"{most_recent}\"! Interesting! Go on... {}", self.emoji), + format!("\"{most_recent}\"? And how does that make you feel? {emoji}",), + format!("\"{most_recent}\"! Interesting! Go on... {emoji}"), ] } }