From a54f77766246f4ce418447cc4d37295c15065b39 Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Tue, 2 Jul 2024 21:28:21 -0600 Subject: multithreading and message filtering from multiple clients --- src/main.rs | 58 +++++++++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 37 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 02fba86..a63c4bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,44 +1,28 @@ mod config; -use config::Config; -use twilight_gateway::{Intents, Shard, ShardId}; +mod system; -#[tokio::main(flavor = "current_thread")] -async fn main() { - println!("Hello, world!"); +use std::thread::{self, JoinHandle}; +use tokio::runtime; - let config_str = include_str!("../config.toml"); - let config = Config::load(config_str.to_string()); - - let token = config.systems.get("ashe-test").unwrap().members - .iter().find(|member| member.name == "test").unwrap() - .discord_token.clone(); - - println!("Token: {}", token); - - let intents = Intents::GUILD_MEMBERS | Intents::GUILD_PRESENCES | Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT; - - let mut shard = Shard::new(ShardId::ONE, token, intents); +use system::System; - loop { - let event = match shard.next_event().await { - Ok(event) => event, - Err(source) => { - println!("error receiving event"); - - if source.is_fatal() { - break; - } - - continue; - } - }; - - match event { - twilight_gateway::Event::MessageCreate(message) => println!("Message: {:?}", message), - twilight_gateway::Event::MessageUpdate(_) => println!("Message updated"), - twilight_gateway::Event::Ready(_) => println!("Bot ready!"), - _ => (), - } +fn main() { + let config_str = include_str!("../config.toml"); + let config = config::Config::load(config_str.to_string()); + + let handles : Vec<_> = config.systems.into_iter().map(|(system_name, system_config)| -> JoinHandle<()> { + println!("Starting thread for system {}", system_name); + thread::spawn(move || { + let runtime = runtime::Builder::new_current_thread().enable_all().build().expect("Could not construct Tokio runtime"); + runtime.block_on(async { + let mut system = System::new(system_config); + system.start_clients().await; + }) + }) + }).collect(); + + for thread_handle in handles.into_iter() { + thread_handle.join().expect("Child thread has panicked"); } } -- cgit 1.4.1