From f0db44f672657503efdaf7dbdbd941786d0fcc4f Mon Sep 17 00:00:00 2001 From: toast Date: Fri, 8 Dec 2023 22:25:44 +1100 Subject: [PATCH] Add Docker stuff, combine status subcmds into one. --- .dockerignore | 5 ++++ .gitignore | 2 +- Dockerfile | 12 +++++++++ docker-compose.yml | 9 +++++++ src/commands/status.rs | 59 ++++++++++++------------------------------ 5 files changed, 44 insertions(+), 43 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3fddeae --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.vscode +target +.env +.gitignore +renovate.json diff --git a/.gitignore b/.gitignore index fedaa2b..c5dd462 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/target +target .env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2856c15 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM rust:1.74-alpine3.18 AS compiler +ENV RUSTFLAGS="-C target-feature=-crt-static" +RUN apk add --no-cache openssl-dev musl-dev +WORKDIR /usr/src/kon +COPY . . +RUN cargo fetch && cargo build -r + +FROM alpine:3.19 +RUN apk add --no-cache openssl-dev libgcc +WORKDIR /kon +COPY --from=compiler /usr/src/kon/target/release/kon . +CMD [ "./kon" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ce61174 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3.8' +services: + bot: + container_name: Kon + image: 'git.toast-server.net/toast/kon:main' + build: . + env_file: + - .env + restart: unless-stopped diff --git a/src/commands/status.rs b/src/commands/status.rs index dfb96d0..93f44ab 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -1,73 +1,48 @@ -use crate::Error; +use crate::{Error, COLOR}; use reqwest::get; use std::collections::HashMap; use serde_json::Value; +use tokio::join; lazy_static::lazy_static! { static ref PMS_BASE: String = std::env::var("WG_PMS").expect("Expected a \"WG_PMS\" in the envvar but none was found"); } -/// Retrieve the server data from Wargaming -#[poise::command(slash_command, subcommands("asia", "europe"))] -pub async fn status(_ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { - Ok(()) -} - -/// Check the server status for Asia server +/// Retrieve the server statuses from Wargaming #[poise::command(slash_command)] -pub async fn asia(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { - let asia_pms = &PMS_BASE; - let servers = pms_serverstatus(&asia_pms).await?; +pub async fn status(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { + let pms_asia = &PMS_BASE; + let pms_eu = PMS_BASE.replace("asia", "eu"); - let mut fields = Vec::new(); + let (servers_asia, servers_eu) = join!(pms_serverstatus(&pms_asia), pms_serverstatus(&pms_eu)); - for server in servers { + let mut embed_fields = Vec::new(); + for server in servers_eu.unwrap() { let name = server["name"].as_str().unwrap().to_owned(); let status = match server["availability"].as_str().unwrap() { "1" => "Online", "-1" => "Offline", _ => "Unknown" }; - fields.push((name, status, true)); + embed_fields.push((name, status, true)); } - ctx.send(|m| - m.embed(|e| { - e.color(crate::COLOR) - .title("World of Tanks Asia Status") - .fields(fields) - }) - ).await?; - - Ok(()) -} - -/// Check the server status for EU servers -#[poise::command(slash_command)] -pub async fn europe(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { - let eu_pms = PMS_BASE.replace("asia", "eu"); - let servers = pms_serverstatus(&eu_pms).await?; - - let mut fields = Vec::new(); - - for server in servers { + for server in servers_asia.unwrap() { let name = server["name"].as_str().unwrap().to_owned(); let status = match server["availability"].as_str().unwrap() { "1" => "Online", "-1" => "Offline", _ => "Unknown" }; - fields.push((name, status, true)); + embed_fields.push((name, status, true)); } - ctx.send(|m| - m.embed(|e| { - e.color(crate::COLOR) - .title("World of Tanks Europe Status") - .fields(fields) - }) - ).await?; + ctx.send(|m| m.embed(|e| + e.color(COLOR) + .title("World of Tanks Server Status") + .fields(embed_fields) + )).await?; Ok(()) }