From 677f5aede710ab065917958c64b89a5fd8112b30 Mon Sep 17 00:00:00 2001 From: blackbeard420 Date: Thu, 28 Dec 2023 18:40:59 -0500 Subject: [PATCH] update to use tokio spawn --- src/main.rs | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index cda2313..ba53353 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,13 +37,15 @@ async fn main() -> Result<(), failure::Error> { let out = cmd.output().unwrap(); - let output = String::from_utf8_lossy(&out.stdout); + let output = String::from_utf8_lossy(&out.stdout).to_string(); - let lines = output.lines(); + let s = sender.clone(); - for l in lines { - sender.send_privmsg(target, l)?; - } + let t = target.to_string(); + + tokio::spawn(async move { + send_message(s, t, output).await + }); } if &msg[1..2] == "p" { @@ -57,30 +59,35 @@ async fn main() -> Result<(), failure::Error> { let out = cmd.output().unwrap(); - let outerr = String::from_utf8_lossy(&out.stderr); - let output = String::from_utf8_lossy(&out.stdout); + let outerr = String::from_utf8_lossy(&out.stderr).to_string(); + let output = String::from_utf8_lossy(&out.stdout).to_string(); - let lines = outerr.lines(); + let s = sender.clone(); - for l in lines { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; - sender.send_privmsg(target, l)?; - } + let t = target.to_string(); - let lines = output.lines(); + tokio::spawn(async move { + send_message(s, t, outerr).await + }); - for l in lines { - tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; - sender.send_privmsg(target, l)?; - } + let s = sender.clone(); + let t = target.to_string(); + + tokio::spawn(async move { + send_message(s.clone(), t, output).await + }); } } - - if msg.contains(client.current_nickname()) { - sender.send_privmsg(target, "Hi!")?; - } } } + Ok(()) +} + +async fn send_message(sender: irc::client::Sender, target: String, msg: String) -> Result<(), failure::Error> { + for l in msg.lines() { + sender.send_privmsg(target.clone(), l)?; + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + } Ok(()) } \ No newline at end of file