I'm trying to use this API for interacting with twitch, but I've also never used Tokio before, so I don't know a way around this (probably) common issue.
Inside the tokio::spawn task is where all of the messages get parsed, but I need a reference to client to be able to send messages, and (I think) it gets moved in the thread if I put it in there.
This code is pretty much a copy of the example code given in the crate's docs: https://docs.rs/twitch-irc/5.0.1/twitch_irc/#getting-started
use twitch_irc::{
login::StaticLoginCredentials, message::ServerMessage, ClientConfig, SecureTCPTransport,
TwitchIRCClient,
};
const CONFIG_FILE_NAME: &str = "config.txt";
#[tokio::main]
pub async fn main() {
let user_config = parse_config(CONFIG_FILE_NAME).unwrap();
let (mut incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new((&user_config).into());
let streamer_username = user_config.streamer_account_name;
let join_handle = tokio::spawn(async move {
while let Some(message) = incoming_messages.recv().await {
if let ServerMessage::Privmsg(message) = message {
println!("{:#?}\n", message);
// I need `client` here to send messages that use data from chat messages
client.say_in_reply_to(&message, "Hello!".to_owned()).await.unwrap();
}
}
});
// I also need `client` here to send general messages and to join the channel
client.say(
streamer_username.clone(),
"Bot Connected!".to_owned(),
).await.unwrap();
client.join(streamer_username.clone()).unwrap();
join_handle.await.unwrap();
}```