rewrite twitch client connection
This commit is contained in:
parent
c2fe4dc663
commit
7277cb8ede
4 changed files with 44 additions and 39 deletions
|
@ -1,8 +1,15 @@
|
|||
use std::env;
|
||||
|
||||
use dotenv::dotenv;
|
||||
use twitch_irc::login::StaticLoginCredentials;
|
||||
use twitch_irc::ClientConfig;
|
||||
use twitch_irc::{
|
||||
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> {
|
||||
dotenv().ok();
|
||||
|
@ -14,3 +21,15 @@ pub fn create_client() -> ClientConfig<StaticLoginCredentials> {
|
|||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,32 +1,29 @@
|
|||
use crate::client::create_client;
|
||||
use crate::client::TwitchClient;
|
||||
use sysinfo::System;
|
||||
|
||||
use twitch_irc::{
|
||||
login::StaticLoginCredentials, message::PrivmsgMessage, SecureTCPTransport, TwitchIRCClient,
|
||||
};
|
||||
use twitch_irc::message::PrivmsgMessage;
|
||||
|
||||
pub async fn ping_command(m: &PrivmsgMessage) {
|
||||
let client = create_client();
|
||||
|
||||
let (mut _incoming_messages, client) =
|
||||
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(client);
|
||||
pub async fn ping_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
||||
let mut sys = System::new_all();
|
||||
|
||||
sys.refresh_all();
|
||||
|
||||
let pid = sysinfo::get_current_pid().unwrap();
|
||||
|
||||
if let Some(process) = sys.process(pid) {
|
||||
let process_memory = process.memory();
|
||||
let total_memory = sys.total_memory();
|
||||
let mem = (process_memory as f64 / total_memory as f64) * 100.0;
|
||||
let mem = process_memory as f64 / (1024.0 * 1024.0);
|
||||
let cpu = process.cpu_usage().round();
|
||||
let uptime = process.run_time();
|
||||
|
||||
let host = System::name().unwrap();
|
||||
let s = format!(
|
||||
"Pong! | ↑: {}s | Host: {:?} | Mem: {:.2}% | CPU: {:?}%",
|
||||
"Pong! | ↑: {}s | Host: {:?} | Mem: {:.2} MB | 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,10 @@
|
|||
use crate::client::create_client;
|
||||
use sysinfo::System;
|
||||
use crate::client::TwitchClient;
|
||||
|
||||
use twitch_irc::{
|
||||
login::StaticLoginCredentials, message::PrivmsgMessage, SecureTCPTransport, TwitchIRCClient,
|
||||
};
|
||||
use twitch_irc::message::PrivmsgMessage;
|
||||
|
||||
pub async fn test_command(m: &PrivmsgMessage) {
|
||||
let client = create_client();
|
||||
|
||||
let (mut _incoming_messages, client) =
|
||||
TwitchIRCClient::<SecureTCPTransport, StaticLoginCredentials>::new(client);
|
||||
let _message = client
|
||||
pub async fn test_command(m: &PrivmsgMessage, c: &TwitchClient) {
|
||||
let _message = c
|
||||
.twitch_client
|
||||
.say(m.channel_login.to_owned(), "test".to_owned())
|
||||
.await;
|
||||
}
|
||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -1,37 +1,32 @@
|
|||
use client::create_client;
|
||||
use commands::ping::ping_command;
|
||||
use commands::test::test_command;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use twitch_irc::{
|
||||
login::StaticLoginCredentials, message::ServerMessage, SecureTCPTransport, TwitchIRCClient,
|
||||
};
|
||||
use client::client;
|
||||
use twitch_irc::message::ServerMessage;
|
||||
|
||||
mod client;
|
||||
mod commands;
|
||||
|
||||
#[tokio::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 client = client();
|
||||
|
||||
initial_channels.insert("notnotoh", ());
|
||||
initial_channels.insert("notohh", ());
|
||||
initial_channels.insert("daph", ());
|
||||
|
||||
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),
|
||||
Err(e) => eprintln!("Failed to join channels! {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
ServerMessage::Privmsg(msg) => {
|
||||
println!(
|
||||
|
@ -40,8 +35,8 @@ pub async fn main() {
|
|||
);
|
||||
if msg.sender.name == "notohh" {
|
||||
match msg.message_text.as_str() {
|
||||
"*ping" => ping_command(&msg).await,
|
||||
"*test" => test_command(&msg).await,
|
||||
"*ping" => ping_command(&msg, &client).await,
|
||||
"*test" => test_command(&msg, &client).await,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue