Skip to content

Commit

Permalink
fix panic on start
Browse files Browse the repository at this point in the history
  • Loading branch information
tazz4843 committed Feb 2, 2024
1 parent eeda615 commit 1ffe6b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
24 changes: 10 additions & 14 deletions scripty_bot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)
Expand All @@ -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");
}
2 changes: 1 addition & 1 deletion scripty_bot_utils/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ use serenity::client::Cache;
use crate::{dm_support::DmSupportStatus, Data};

pub static CLIENT_CACHE: OnceCell<Arc<Cache>> = OnceCell::new();
pub static CLIENT_DATA: OnceCell<Data> = OnceCell::new();
pub static CLIENT_DATA: OnceCell<Arc<Data>> = OnceCell::new();
pub static DM_SUPPORT_GLOBAL: OnceCell<DmSupportStatus> = OnceCell::new();
4 changes: 2 additions & 2 deletions scripty_bot_utils/src/types/data.rs
Original file line number Diff line number Diff line change
@@ -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<ShardManager>,
pub shard_manager: OnceLock<Arc<ShardManager>>,
}

0 comments on commit 1ffe6b5

Please sign in to comment.