summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAshelyn Dawn <git@ashen.earth>2024-07-02 21:28:21 -0600
committerAshelyn Rose <git@ashen.earth>2024-07-02 22:11:54 -0600
commita54f77766246f4ce418447cc4d37295c15065b39 (patch)
treeaa9b73592cf36ff56cb25e7dc91f7d1099d1106b /src/main.rs
parent5dc71ca03b5402c0a284e25492e63c696f7bdec6 (diff)
multithreading and message filtering from multiple clients
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs58
1 files changed, 21 insertions, 37 deletions
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");
     }
 }