From 786925d8e4c27c591785f8d5aa68d006f2c82e04 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 13 Apr 2021 11:32:51 +0200 Subject: [PATCH] Switch to stable toolchain and add more info to stats command Signed-off-by: trivernis --- .github/workflows/build-and-release.yml | 2 +- .github/workflows/build.yml | 2 +- Cargo.lock | 37 +++++++++++++++++++++++ Cargo.toml | 1 + bot-coreutils/src/lib.rs | 2 ++ bot-database/src/lib.rs | 1 + bot-serenityutils/src/lib.rs | 2 ++ rust-toolchain.toml | 2 +- src/client.rs | 2 +- src/commands/misc/stats.rs | 40 ++++++++++++++++++++++--- src/commands/music/leave.rs | 16 ++++++---- src/main.rs | 2 ++ 12 files changed, 95 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build-and-release.yml b/.github/workflows/build-and-release.yml index 96ff4a4..71aac77 100644 --- a/.github/workflows/build-and-release.yml +++ b/.github/workflows/build-and-release.yml @@ -14,7 +14,7 @@ jobs: - name: Set up toolchain uses: actions-rs/toolchain@v1 with: - toolchain: nightly + toolchain: stable override: true - name: Cache cargo builds uses: actions/cache@v2 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b80fe46..d9809d2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,7 +20,7 @@ jobs: - name: Set up toolchain uses: actions-rs/toolchain@v1 with: - toolchain: nightly + toolchain: stable override: true - name: Cache build data uses: actions/cache@v2 diff --git a/Cargo.lock b/Cargo.lock index 2ddf6e3..131b5a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "adler" version = "1.0.2" @@ -1734,6 +1736,25 @@ version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "rustc_version_runtime" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4076135c155c07b05e4ab54ba0834aca3fc6a5a01610a2339f5ef3223cf8dee5" +dependencies = [ + "rustc_version", + "semver", +] + [[package]] name = "rustls" version = "0.19.0" @@ -1860,6 +1881,21 @@ dependencies = [ "markup5ever_rcdom", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "serde" version = "1.0.125" @@ -2300,6 +2336,7 @@ dependencies = [ "rand 0.8.3", "regex", "reqwest", + "rustc_version_runtime", "sauce-api", "serde", "serde_derive", diff --git a/Cargo.toml b/Cargo.toml index a4ba1c9..f80aac4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ sysinfo = "0.16.5" reqwest = "0.11.2" chrono-tz = "0.5.3" sauce-api = "0.7.1" +rustc_version_runtime = "0.2.0" \ No newline at end of file diff --git a/bot-coreutils/src/lib.rs b/bot-coreutils/src/lib.rs index 96d0007..588b5b3 100644 --- a/bot-coreutils/src/lib.rs +++ b/bot-coreutils/src/lib.rs @@ -6,3 +6,5 @@ pub mod shuffle; pub mod string; /// Utilities to quickly check strings that represent urls pub mod url; + +pub static VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/bot-database/src/lib.rs b/bot-database/src/lib.rs index 0b6af1f..831a1c2 100644 --- a/bot-database/src/lib.rs +++ b/bot-database/src/lib.rs @@ -14,6 +14,7 @@ pub mod error; pub mod models; pub mod schema; +pub static VERSION: &str = env!("CARGO_PKG_VERSION"); pub use database::Database; type PoolConnection = Pool>; diff --git a/bot-serenityutils/src/lib.rs b/bot-serenityutils/src/lib.rs index ed1e686..72861ec 100644 --- a/bot-serenityutils/src/lib.rs +++ b/bot-serenityutils/src/lib.rs @@ -3,3 +3,5 @@ pub mod ephemeral_message; pub mod error; pub mod macros; pub mod menu; + +pub static VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 9de0495..54e5d89 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,5 +1,5 @@ [toolchain] -channel = "nightly" +channel = "stable" targets = [ "x86_64-unknown-linux-gnu", ] \ No newline at end of file diff --git a/src/client.rs b/src/client.rs index 5b4fef9..64b239a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -99,7 +99,7 @@ async fn after_hook(ctx: &Context, msg: &Message, cmd_name: &str, error: Command let database = get_database_from_context(ctx).await; let _ = database .add_statistic( - env!("CARGO_PKG_VERSION"), + crate::VERSION, cmd_name, SystemTime::now(), error_msg.is_none(), diff --git a/src/commands/misc/stats.rs b/src/commands/misc/stats.rs index 5674cf1..ae83a26 100644 --- a/src/commands/misc/stats.rs +++ b/src/commands/misc/stats.rs @@ -9,9 +9,9 @@ use serenity::prelude::*; use sysinfo::{ProcessExt, SystemExt}; use crate::commands::common::handle_autodelete; -use crate::utils::context_data::get_database_from_context; - -const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +use crate::providers::music::queue::MusicQueue; +use crate::utils::context_data::{get_database_from_context, Store}; +use std::sync::Arc; #[command] #[description("Shows some statistics about the bot")] @@ -22,6 +22,7 @@ async fn stats(ctx: &Context, msg: &Message) -> CommandResult { let database = get_database_from_context(ctx).await; let mut system = sysinfo::System::new_all(); system.refresh_all(); + let kernel_version = system.get_kernel_version().unwrap_or("n/a".to_string()); let own_process = system.get_process(process::id() as i32).unwrap(); let memory_usage = own_process.memory(); @@ -39,11 +40,18 @@ async fn stats(ctx: &Context, msg: &Message) -> CommandResult { let discord_info = format!( r#" Version: {} + Compiled with: rustc {} Owner: <@{}> Guilds: {} + Voice Connections: {} Times Used: {} "#, - VERSION, bot_info.owner.id, guild_count, total_commands_executed + crate::VERSION, + rustc_version_runtime::version(), + bot_info.owner.id, + guild_count, + get_queue_count(ctx).await, + total_commands_executed ); log::trace!("Discord info {}", discord_info); @@ -79,3 +87,27 @@ async fn stats(ctx: &Context, msg: &Message) -> CommandResult { Ok(()) } + +/// Returns the total number of queues that are not +/// flagged to leave +async fn get_queue_count(ctx: &Context) -> usize { + let queues: Vec>> = { + let data = ctx.data.read().await; + let store = data.get::().unwrap(); + + store + .music_queues + .iter() + .map(|(_, q)| Arc::clone(q)) + .collect() + }; + let mut count = 0; + for queue in queues { + let queue = queue.lock().await; + if !queue.leave_flag { + count += 1; + } + } + + count +} diff --git a/src/commands/music/leave.rs b/src/commands/music/leave.rs index 37e0542..a4af16f 100644 --- a/src/commands/music/leave.rs +++ b/src/commands/music/leave.rs @@ -4,7 +4,8 @@ use serenity::framework::standard::CommandResult; use serenity::model::channel::Message; use crate::commands::common::handle_autodelete; -use crate::commands::music::{get_queue_for_guild, get_voice_manager, is_dj}; +use crate::commands::music::{get_voice_manager, is_dj}; +use crate::utils::context_data::Store; use bot_serenityutils::core::SHORT_TIMEOUT; use bot_serenityutils::ephemeral_message::EphemeralMessage; @@ -23,11 +24,14 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult { } let manager = get_voice_manager(ctx).await; - let queue = forward_error!( - ctx, - msg.channel_id, - get_queue_for_guild(ctx, &guild.id).await - ); + let queue = { + let mut data = ctx.data.write().await; + let store = data.get_mut::().unwrap(); + store + .music_queues + .remove(&guild.id) + .expect("No queue for guild.") + }; let queue_lock = queue.lock().await; let handler = manager.get(guild.id); diff --git a/src/main.rs b/src/main.rs index 2843cbf..6a58e93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,8 @@ mod messages; mod providers; pub mod utils; +pub static VERSION: &str = env!("CARGO_PKG_VERSION"); + #[tokio::main] async fn main() { init_logger();