rewrite twitch client connection

This commit is contained in:
notohh 2024-06-16 01:55:33 -04:00
parent c2fe4dc663
commit 7277cb8ede
Signed by: notohh
GPG key ID: BD47506D475EE86D
4 changed files with 44 additions and 39 deletions

View file

@ -1,8 +1,15 @@
use std::env; use std::env;
use dotenv::dotenv; use dotenv::dotenv;
use twitch_irc::login::StaticLoginCredentials; use twitch_irc::{
use twitch_irc::ClientConfig; login::StaticLoginCredentials, message::ServerMessage, ClientConfig, SecureTCPTransport,
TwitchIRCClient,
};
pub struct TwitchClient {
pub incoming_messages: tokio::sync::mpsc::UnboundedReceiver<ServerMessage>,
pub twitch_client: TwitchIRCClient<SecureTCPTransport, StaticLoginCredentials>,
}
pub fn create_client() -> ClientConfig<StaticLoginCredentials> { pub fn create_client() -> ClientConfig<StaticLoginCredentials> {
dotenv().ok(); dotenv().ok();
@ -14,3 +21,15 @@ pub fn create_client() -> ClientConfig<StaticLoginCredentials> {
ClientConfig::new_simple(login_creds) ClientConfig::new_simple(login_creds)
} }
pub fn client() -> TwitchClient {
let config = create_client();
let (incoming_messages, twitch_client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(config);
TwitchClient {
twitch_client,
incoming_messages,
}
}

View file

@ -1,32 +1,29 @@
use crate::client::create_client; use crate::client::TwitchClient;
use sysinfo::System; use sysinfo::System;
use twitch_irc::{ use twitch_irc::message::PrivmsgMessage;
login::StaticLoginCredentials, message::PrivmsgMessage, SecureTCPTransport, TwitchIRCClient,
};
pub async fn ping_command(m: &PrivmsgMessage) { pub async fn ping_command(m: &PrivmsgMessage, c: &TwitchClient) {
let client = create_client();
let (mut _incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(client);
let mut sys = System::new_all(); let mut sys = System::new_all();
sys.refresh_all(); sys.refresh_all();
let pid = sysinfo::get_current_pid().unwrap(); let pid = sysinfo::get_current_pid().unwrap();
if let Some(process) = sys.process(pid) { if let Some(process) = sys.process(pid) {
let process_memory = process.memory(); let process_memory = process.memory();
let total_memory = sys.total_memory(); let mem = process_memory as f64 / (1024.0 * 1024.0);
let mem = (process_memory as f64 / total_memory as f64) * 100.0;
let cpu = process.cpu_usage().round(); let cpu = process.cpu_usage().round();
let uptime = process.run_time(); let uptime = process.run_time();
let host = System::name().unwrap(); let host = System::name().unwrap();
let s = format!( let s = format!(
"Pong! | ↑: {}s | Host: {:?} | Mem: {:.2}% | CPU: {:?}%", "Pong! | ↑: {}s | Host: {:?} | Mem: {:.2} MB | CPU: {:?}%",
uptime, host, mem, cpu uptime, host, mem, cpu
); );
let _message = client.say(m.channel_login.to_owned(), s.to_owned()).await; let _message = c
.twitch_client
.say(m.channel_login.to_owned(), s.to_owned())
.await;
} }
} }

View file

@ -1,16 +1,10 @@
use crate::client::create_client; use crate::client::TwitchClient;
use sysinfo::System;
use twitch_irc::{ use twitch_irc::message::PrivmsgMessage;
login::StaticLoginCredentials, message::PrivmsgMessage, SecureTCPTransport, TwitchIRCClient,
};
pub async fn test_command(m: &PrivmsgMessage) { pub async fn test_command(m: &PrivmsgMessage, c: &TwitchClient) {
let client = create_client(); let _message = c
.twitch_client
let (mut _incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(client);
let _message = client
.say(m.channel_login.to_owned(), "test".to_owned()) .say(m.channel_login.to_owned(), "test".to_owned())
.await; .await;
} }

View file

@ -1,37 +1,32 @@
use client::create_client;
use commands::ping::ping_command; use commands::ping::ping_command;
use commands::test::test_command; use commands::test::test_command;
use std::collections::HashMap; use std::collections::HashMap;
use twitch_irc::{ use client::client;
login::StaticLoginCredentials, message::ServerMessage, SecureTCPTransport, TwitchIRCClient, use twitch_irc::message::ServerMessage;
};
mod client; mod client;
mod commands; mod commands;
#[tokio::main] #[tokio::main]
pub async fn main() { pub async fn main() {
let client = create_client();
let (mut incoming_messages, client) =
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(client);
let mut initial_channels = HashMap::new(); let mut initial_channels = HashMap::new();
let mut client = client();
initial_channels.insert("notnotoh", ()); initial_channels.insert("notnotoh", ());
initial_channels.insert("notohh", ()); initial_channels.insert("notohh", ());
initial_channels.insert("daph", ()); initial_channels.insert("daph", ());
for (channels, _) in initial_channels.iter() { for (channels, _) in initial_channels.iter() {
match client.join(channels.to_owned().to_string()) { match client.twitch_client.join(channels.to_owned().to_string()) {
Ok(_) => println!("Joined channel {}", channels), Ok(_) => println!("Joined channel {}", channels),
Err(e) => eprintln!("Failed to join channels! {}", e), Err(e) => eprintln!("Failed to join channels! {}", e),
} }
} }
let message_handler = tokio::spawn(async move { let message_handler = tokio::spawn(async move {
while let Some(message) = incoming_messages.recv().await { while let Some(message) = client.incoming_messages.recv().await {
match message { match message {
ServerMessage::Privmsg(msg) => { ServerMessage::Privmsg(msg) => {
println!( println!(
@ -40,8 +35,8 @@ pub async fn main() {
); );
if msg.sender.name == "notohh" { if msg.sender.name == "notohh" {
match msg.message_text.as_str() { match msg.message_text.as_str() {
"*ping" => ping_command(&msg).await, "*ping" => ping_command(&msg, &client).await,
"*test" => test_command(&msg).await, "*test" => test_command(&msg, &client).await,
_ => {} _ => {}
} }
} }