Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offload application command creation to new task #140

Merged
merged 1 commit into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ blocklist.json
Cargo.lock
#Added by cargo
/target
/.idea
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/discord-compiler-bot.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

40 changes: 23 additions & 17 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,26 +242,32 @@ impl EventHandler for Handler {
async fn ready(&self, ctx: Context, ready: Ready) {
info!("[Shard {}] Ready", ctx.shard_id);

let data = ctx.data.read().await;
let mut stats = data.get::<StatsManagerCache>().unwrap().lock().await;
let cmd_mgr = data.get::<CommandCache>().unwrap().read().await;
cmd_mgr.register_commands(&ctx).await;
info!("Registering commands for shard {}", ctx.shard_id);

// occasionally we can have a ready event fire well after execution
// this check prevents us from double calling all_shards_ready
let total_shards_to_spawn = ready.shard.unwrap()[1];
if stats.shard_count()+1 > total_shards_to_spawn {
info!("Skipping duplicate ready event...");
return;
}
{
let data = ctx.data.read().await;
let mut stats = data.get::<StatsManagerCache>().unwrap().lock().await;
// occasionally we can have a ready event fire well after execution
// this check prevents us from double calling all_shards_ready
let total_shards_to_spawn = ready.shard.unwrap()[1];
if stats.shard_count() + 1 > total_shards_to_spawn {
info!("Skipping duplicate ready event...");
return;
}

let guild_count = ready.guilds.len() as u64;
stats.add_shard(guild_count);
let guild_count = ready.guilds.len() as u64;
stats.add_shard(guild_count);

if stats.shard_count() == total_shards_to_spawn {
self.all_shards_ready(&ctx, & mut stats, &ready).await;
if stats.shard_count() == total_shards_to_spawn {
self.all_shards_ready(&ctx, &mut stats, &ready).await;
}
}

tokio::task::spawn(async move {
let ctx = ctx.clone();
let data = ctx.data.read().await;
let cmd_mgr = data.get::<CommandCache>().unwrap().read().await;
cmd_mgr.register_commands(&ctx).await;
info!("[Shard {}] Registered commands", ctx.shard_id);
});
}

async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
Expand Down