Hello,
some time ago I shared a Guess game that I made from scratch and this time I thought to do something different so I decided to make a Guess game over IRC.
It’s pretty basic but I learned a lot about destructing Enum, unwrap and if let syntax.
I would appreciate any suggestion :)
here is the source code:
use futures::prelude::*;
use irc::client::prelude::*;
use rand::Rng;
#[tokio::main]
async fn main() -> irc::error::Result<()> {
let config = Config {
nickname: Some("beep_boop".to_owned()),
server: Some("irc.libera.chat".to_owned()),
channels: vec!["#test".to_owned()],
..Default::default()
};
let mut client = Client::from_config(config).await?;
client.identify()?;
let mut stream = client.stream()?;
let mut secret_number = rand::thread_rng().gen_range(0..101);
let mut attempts = 0;
while let Some(message) = stream.next().await.transpose()? {
print!("{}", message);
match message.command {
Command::PRIVMSG(channel, message) => {
if let Some(command) = message.get(0..6) {
if command == "!guess" {
let parts = message.split(' ').collect::<Vec<_>>();
if let Some(part2) = parts.get(1) {
if let Ok(num) = part2.to_string().parse::<u32>() {
println!("{:?}", num);
if num == secret_number {
client.send_privmsg(&channel, format!("You won with {attempts} attempts! generating next number")).unwrap();
secret_number = rand::thread_rng().gen_range(0..101);
attempts = 0; // reseting the number of attempts
} else if num > secret_number {
client.send_privmsg(&channel, "too high").unwrap();
attempts += 1;
} else {
client.send_privmsg(&channel, "too low").unwrap();
attempts += 1;
}
}
}
}
}
else {
continue;
}
if message.contains(client.current_nickname()) {
client.send_privmsg(channel, "beep boop").unwrap();
}
},
_ => println!("IRC command not implemented: {message}"),
}
}
Ok(())
}
Thank you for the in depth suggestion :)