Skip to content

Commit

Permalink
Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
tazz4843 committed Feb 2, 2024
1 parent 1ffe6b5 commit 1b9a6c9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
24 changes: 10 additions & 14 deletions scripty_bot_utils/src/background_tasks/tasks/status_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@ use serenity::{
small_fixed_array::FixedString,
};

use crate::{background_tasks::core::BackgroundTask, globals::CLIENT_DATA, Error};
use crate::{background_tasks::core::BackgroundTask, globals::CLIENT_DATA, Data, Error};

Check warning on line 10 in scripty_bot_utils/src/background_tasks/tasks/status_update.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

unused import: `globals::CLIENT_DATA`

warning: unused import: `globals::CLIENT_DATA` --> scripty_bot_utils/src/background_tasks/tasks/status_update.rs:10:53 | 10 | use crate::{background_tasks::core::BackgroundTask, globals::CLIENT_DATA, Data, Error}; | ^^^^^^^^^^^^^^^^^^^^

/// Updates the bot status every minute.
pub struct StatusUpdater {
ctx: SerenityContext,
shard_manager: Arc<ShardManager>,
run_number: u32,
ctx: SerenityContext,
run_number: u32,
}

#[async_trait]
impl BackgroundTask for StatusUpdater {
async fn init(ctx: SerenityContext) -> Result<Self, Error> {
Ok(Self {
ctx,
shard_manager: CLIENT_DATA
.get()
.expect("client data not initialized")
.shard_manager
.clone(),
run_number: 0,
})
Ok(Self { ctx, run_number: 0 })
}

fn interval(&mut self) -> Duration {
Expand All @@ -42,11 +33,16 @@ impl BackgroundTask for StatusUpdater {
return;
}

let ctx_data = self.ctx.data::<Data>();
let Some(shard_manager) = ctx_data.shard_manager.get().clone() else {

Check warning on line 37 in scripty_bot_utils/src/background_tasks/tasks/status_update.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

using `clone` on type `Option<&Arc<ShardManager>>` which implements the `Copy` trait

warning: using `clone` on type `Option<&Arc<ShardManager>>` which implements the `Copy` trait --> scripty_bot_utils/src/background_tasks/tasks/status_update.rs:37:29 | 37 | let Some(shard_manager) = ctx_data.shard_manager.get().clone() else { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `ctx_data.shard_manager.get()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy = note: `#[warn(clippy::clone_on_copy)]` on by default
return;
};

let guild_count = self.ctx.cache.guild_count();
let mut guild_count_fmt = num_format::Buffer::new();
guild_count_fmt.write_formatted(&guild_count, &num_format::Locale::en);

let runners = self.shard_manager.runners.lock().await;
let runners = shard_manager.runners.lock().await;
for (shard_id, shard_info) in runners.iter() {
let shard_latency = shard_info
.latency
Expand Down
6 changes: 4 additions & 2 deletions scripty_bot_utils/src/extern_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ pub async fn get_shard_info() -> Result<HashMap<u16, ShardInfo>, CacheNotInitial
}
};

let shard_manager = data.shard_manager.get().ok_or(CacheNotInitializedError)?;

if should_update {
let shard_count = data.shard_manager.runners.lock().await.len() as u64;
let shard_count = shard_manager.runners.lock().await.len() as u64;

let mut shard_guild_count = HashMap::new();
for guild in cache.guilds() {
Expand Down Expand Up @@ -178,7 +180,7 @@ pub async fn get_shard_info() -> Result<HashMap<u16, ShardInfo>, CacheNotInitial
data.0.clone()
};

let runners = data.shard_manager.runners.lock().await;
let runners = shard_manager.runners.lock().await;
let mut shard_list = HashMap::new();

for (shard_id, shard_info) in runners.iter() {
Expand Down
18 changes: 6 additions & 12 deletions scripty_commands/src/cmds/admin/guild_check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{collections::HashMap, fmt::Write, ops::Range};

use poise::CreateReply;
use scripty_bot_utils::globals::CLIENT_DATA;
use serenity::{all::ShardId, gateway::ChunkGuildFilter};

use crate::{Context, Error};
Expand Down Expand Up @@ -42,11 +41,11 @@ pub async fn check_guilds(ctx: Context<'_>, specified_ratio: f64) -> Result<(),
// fields of the tuple are, respectively, the guild name, the guild id, and the ratio (bot count / user count)
let mut guild_warnings: Vec<(String, u64, f64)> = Vec::new();

let shard_manager = CLIENT_DATA
.get()
.expect("client data not initialized")
let ctx_data = ctx.data();
let shard_manager = ctx_data
.shard_manager
.clone();
.get()
.ok_or(Error::custom("shard manager not initialized".to_string()))?;
let shard_count = shard_manager.runners.lock().await.len() as u16;

for guild in ctx.serenity_context().cache.guilds() {
Expand All @@ -64,17 +63,12 @@ pub async fn check_guilds(ctx: Context<'_>, specified_ratio: f64) -> Result<(),
if (member_count as usize) < g.members.len() {
debug!(?g.id, "chunking guild");
let guild_id = g.id;
let shard_manager2 = shard_manager.clone();
tokio::spawn(async move {
// calculate the shard this guild is on
let shard_id = serenity::utils::shard_id(guild_id, shard_count);

let shard_manager = CLIENT_DATA
.get()
.expect("client data not initialized")
.shard_manager
.clone();

let runner_guard = shard_manager.runners.lock().await;
let runner_guard = shard_manager2.runners.lock().await;
runner_guard
.get(&ShardId(shard_id))
.expect("shard should exist")
Expand Down
13 changes: 9 additions & 4 deletions scripty_commands/src/cmds/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
scripty_i18n::get_resolved_language(ctx.author().id.get(), ctx.guild_id().map(|g| g.get()))
.await;

let ctx_data = ctx.data();
let shard_manager = ctx_data
.shard_manager
.get()
.ok_or(Error::custom("shard manager not initialized".to_string()))?;

// all latency methods return a latency in nanoseconds

// get WebSocket latency in nanoseconds and milliseconds, defaulting to 0 if it fails
let ws_latency_ns =
get_ws_latency(&ctx.data().shard_manager, ctx.serenity_context().shard_id.0)
.await
.unwrap_or(0);
let ws_latency_ns = get_ws_latency(&shard_manager, ctx.serenity_context().shard_id.0)

Check warning on line 23 in scripty_commands/src/cmds/ping.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> scripty_commands/src/cmds/ping.rs:23:37 | 23 | let ws_latency_ns = get_ws_latency(&shard_manager, ctx.serenity_context().shard_id.0) | ^^^^^^^^^^^^^^ help: change this to: `shard_manager` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
.await
.unwrap_or(0);
let ws_latency_ms = (ws_latency_ns as f64 / 1_000_000.0).round();
// get HTTP latency in nanoseconds and milliseconds
let http_latency_ns = get_http_latency(ctx.serenity_context(), ctx.channel_id()).await;
Expand Down

0 comments on commit 1b9a6c9

Please sign in to comment.