diff --git a/scripty_bot/src/lib.rs b/scripty_bot/src/lib.rs index 09f7840a..67f88a6a 100644 --- a/scripty_bot/src/lib.rs +++ b/scripty_bot/src/lib.rs @@ -6,7 +6,7 @@ mod framework_opts; #[macro_use] extern crate tracing; -use std::sync::Arc; +use std::sync::{Arc, OnceLock}; use poise::FrameworkBuilder; use scripty_bot_utils::{globals::CLIENT_DATA, handler, Data}; @@ -26,24 +26,16 @@ pub async fn entrypoint() { let framework = FrameworkBuilder::default() .options(framework_opts::get_framework_opts()) .build(); + let data = Arc::new(Data { + shard_manager: OnceLock::new(), + }); CLIENT_DATA - .set(Data { - shard_manager: framework.shard_manager().clone(), - }) + .set(data.clone()) .expect("user data setup called more than once: bug?"); - let sm = framework.shard_manager().clone(); - tokio::spawn(async move { - tokio::signal::ctrl_c() - .await - .expect("failed to listen for ctrl+c"); - sm.shutdown_all().await; - }); let mut client = serenity::Client::builder(&cfg.tokens.discord, framework_opts::get_gateway_intents()) - .data(Arc::new(Data { - shard_manager: framework.shard_manager().clone(), - })) + .data(data.clone()) .framework(framework) .event_handler(handler::BotEventHandler) .raw_event_handler(handler::RawEventHandler) @@ -52,5 +44,9 @@ pub async fn entrypoint() { .await .expect("failed to create serenity client"); + data.shard_manager + .set(client.shard_manager.clone()) + .expect("no other task should set shard manager"); + client.start_autosharded().await.expect("failed to run bot"); } diff --git a/scripty_bot_utils/src/globals.rs b/scripty_bot_utils/src/globals.rs index 2fea8639..bad95ff7 100644 --- a/scripty_bot_utils/src/globals.rs +++ b/scripty_bot_utils/src/globals.rs @@ -6,5 +6,5 @@ use serenity::client::Cache; use crate::{dm_support::DmSupportStatus, Data}; pub static CLIENT_CACHE: OnceCell> = OnceCell::new(); -pub static CLIENT_DATA: OnceCell = OnceCell::new(); +pub static CLIENT_DATA: OnceCell> = OnceCell::new(); pub static DM_SUPPORT_GLOBAL: OnceCell = OnceCell::new(); diff --git a/scripty_bot_utils/src/types/data.rs b/scripty_bot_utils/src/types/data.rs index 4c72311d..38e4bff6 100644 --- a/scripty_bot_utils/src/types/data.rs +++ b/scripty_bot_utils/src/types/data.rs @@ -1,8 +1,8 @@ -use std::sync::Arc; +use std::sync::{Arc, OnceLock}; use serenity::all::ShardManager; #[derive(Debug)] pub struct Data { - pub shard_manager: Arc, + pub shard_manager: OnceLock>, }