From 9b24bd0882a365d157b3992bdd8975a1734f6367 Mon Sep 17 00:00:00 2001 From: toast Date: Mon, 11 Mar 2024 10:06:40 +1100 Subject: [PATCH] Bring the codebase more up to date with Kon --- .vscode/settings.json | 4 +-- Cargo.lock | 9 +++--- Cargo.toml | 4 +-- docker-compose.yml | 2 +- src/commands/data.rs | 65 ------------------------------------------ src/commands/mod.rs | 1 - src/commands/uptime.rs | 6 ++-- src/main.rs | 20 +++++++++---- 8 files changed, 28 insertions(+), 83 deletions(-) delete mode 100644 src/commands/data.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 2938df3..85dc154 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { + "rust-analyzer.showUnlinkedFileNotification": false, "rust-analyzer.linkedProjects": [ "./Cargo.toml" - ], - "rust-analyzer.showUnlinkedFileNotification": false + ] } diff --git a/Cargo.lock b/Cargo.lock index de9b70f..8df7026 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1250,6 +1250,7 @@ version = "0.1.0" dependencies = [ "poise", "reqwest", + "serde", "serde_json", "serenity", "sysinfo", @@ -1423,18 +1424,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.194" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.194" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 0dda11b..d853c42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,6 @@ name = "rustbot" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] poise = "0.6.1" reqwest = { version = "0.11.25", features = ["json"] } @@ -14,6 +12,7 @@ tempfile = "3.10.1" sysinfo = "0.30.7" tokio = { version = "1.36.0", features = ["macros", "signal", "rt-multi-thread"] } uptime_lib = "0.3.0" +serde = "1.0.197" [[bin]] name = "rustbot" @@ -26,3 +25,4 @@ debug = true [profile.release] opt-level = 3 debug = false +strip = true diff --git a/docker-compose.yml b/docker-compose.yml index bcbed8f..f1c09d5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '3.8' services: bot: - container_name: Rustbot + container_name: rustbot image: 'git.toast-server.net/toast/rustbot:main' build: . env_file: diff --git a/src/commands/data.rs b/src/commands/data.rs deleted file mode 100644 index 23737ed..0000000 --- a/src/commands/data.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::Error; - -use reqwest::get; -use std::collections::HashMap; -use serde_json::Value; -use poise::CreateReply; -use serenity::builder::{ - CreateEmbed, - CreateEmbedAuthor, - CreateEmbedFooter -}; - -/// Retrieve the data from FS-Server -#[poise::command(slash_command)] -pub async fn data( - ctx: poise::Context<'_, (), Error>, - #[description = "DSS url to extract"] url: String -) -> Result<(), Error> { - // Find .xml and replace it with .json before passing to reqwest. - let url = url.replace(".xml", ".json"); - - // Send a GET request to the provided URL - let response = get(&url).await?.json::>().await?; - - // Extract the required values from the parsed JSON - let server = &response["server"]; - let slots = &response["slots"]; - - // Variable list of JSON values - let name = server["name"].as_str().unwrap(); - let ver = server["version"].as_str().unwrap(); - let map = server["mapName"].as_str().unwrap(); - let slot_cap = slots["capacity"].as_i64().unwrap(); - let slot_cur = slots["used"].as_i64().unwrap(); - let daytime = server["dayTime"].as_i64().unwrap(); - - // todo: Add careerSavegame support when passing in DSS url. - // So I can get the following values for Autosave, Timescale and Slot usage. - - // Convert dayTime (ms) to a military time format - let hour = (daytime / 3600 / 1000) % 24; - let minute = (daytime / 60 / 1000) % 60; - let time = format!("{:02}:{:02}", hour, minute); - - let embed = CreateEmbed::new().color(crate::EMBED_COLOR); - ctx.send(CreateReply::default() - .embed(embed - .title(name) - .description("*Nobody is playing*") - .fields(vec![ - ("Map", map, true), - ("Version", ver, true), - ("Time", &time, true), - ("Slot usage", "xx/xx", true), - ("Autosave", "xx", true), - ("Timescale", "0x", true) - ]) - .author(CreateEmbedAuthor::new(format!("{}/{}", slot_cur, slot_cap)).clone()) - .footer(CreateEmbedFooter::new("Last updated").clone()) - .timestamp(poise::serenity_prelude::Timestamp::now()) - ) - ).await?; - - Ok(()) -} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 75125a7..c9a56b0 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,4 +1,3 @@ pub mod ping; pub mod eval; -pub mod data; pub mod uptime; diff --git a/src/commands/uptime.rs b/src/commands/uptime.rs index 287cafd..a321bd0 100644 --- a/src/commands/uptime.rs +++ b/src/commands/uptime.rs @@ -38,13 +38,13 @@ fn format_duration(secs: u64) -> String { let mut formatted_string = String::new(); if days > 0 { - formatted_string.push_str(&format!("{}d, ", days)); + formatted_string.push_str(&format!("{}d, ", days)); } if hours > 0 || days > 0 { - formatted_string.push_str(&format!("{}h, ", hours)); + formatted_string.push_str(&format!("{}h, ", hours)); } if minutes > 0 || hours > 0 { - formatted_string.push_str(&format!("{}m, ", minutes)); + formatted_string.push_str(&format!("{}m, ", minutes)); } formatted_string.push_str(&format!("{}s", seconds)); diff --git a/src/main.rs b/src/main.rs index 9af076d..9efa315 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use serenity::{ CreateEmbed, CreateEmbedAuthor }, + Context, + Ready, ClientBuilder, GatewayIntents }; @@ -18,8 +20,8 @@ pub static EMBED_COLOR: i32 = 0xf1d63c; static BOT_READY_NOTIFY: u64 = 865673694184996888; async fn on_ready( - ctx: &serenity::Context, - ready: &serenity::Ready, + ctx: &Context, + ready: &Ready, framework: &poise::Framework<(), Error> ) -> Result<(), Error> { println!("Connected to API as {}", ready.user.name); @@ -40,8 +42,8 @@ async fn on_ready( match commands { Ok(cmdmap) => for command in cmdmap.iter() { - println!("Registered command globally: {}", command.name); - }, + println!("Registered command globally: {}", command.name); + }, Err(why) => println!("Error registering commands: {:?}", why) } } @@ -58,7 +60,6 @@ async fn main() { commands: vec![ commands::ping::ping(), commands::eval::eval(), - commands::data::data(), commands::uptime::uptime() ], pre_command: |ctx| Box::pin(async move { @@ -68,6 +69,15 @@ async fn main() { }; println!("[{}] {} ran /{}", get_guild_name, ctx.author().name, ctx.command().qualified_name) }), + on_error: |error| Box::pin(async move { + match error { + poise::FrameworkError::Command { error, ctx, .. } => { + println!("PoiseCommandError({}): {}", ctx.command().qualified_name, error); + } + other => println!("PoiseOtherError: {:?}", other) + } + }), + initialize_owners: true, ..Default::default() }) .setup(|ctx, ready, framework| Box::pin(on_ready(ctx, ready, framework)))