Combine the command together and tweak some stuff in main.rs
All checks were successful
Build and push container image / build (push) Successful in 7m17s
All checks were successful
Build and push container image / build (push) Successful in 7m17s
This commit is contained in:
parent
6076e1b3c7
commit
3dbf087a71
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -794,7 +794,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kon"
|
name = "kon"
|
||||||
version = "0.1.4"
|
version = "0.1.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cargo_toml",
|
"cargo_toml",
|
||||||
"gamedig",
|
"gamedig",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "kon"
|
name = "kon"
|
||||||
version = "0.1.4"
|
version = "0.1.5"
|
||||||
rust-version = "1.74"
|
rust-version = "1.74"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
@ -1,64 +0,0 @@
|
|||||||
use crate::{Error, COLOR};
|
|
||||||
|
|
||||||
use gamedig::protocols::{
|
|
||||||
valve::{
|
|
||||||
Engine, GatheringSettings, Response
|
|
||||||
},
|
|
||||||
types::TimeoutSettings,
|
|
||||||
valve,
|
|
||||||
};
|
|
||||||
use std::{
|
|
||||||
str::FromStr,
|
|
||||||
net::SocketAddr,
|
|
||||||
time::Duration,
|
|
||||||
env::var
|
|
||||||
};
|
|
||||||
|
|
||||||
fn query_server() -> Result<Response, Error> {
|
|
||||||
let server_ip = var("ATS_SERVER_IP").expect("Expected a \"ATS_SERVER_IP\" in the envvar but none was found");
|
|
||||||
let addr = SocketAddr::from_str(&server_ip).unwrap();
|
|
||||||
let engine = Engine::Source(None);
|
|
||||||
let gather_settings = GatheringSettings {
|
|
||||||
players: true,
|
|
||||||
rules: false,
|
|
||||||
check_app_id: false
|
|
||||||
};
|
|
||||||
|
|
||||||
let read_timeout = Duration::from_secs(2);
|
|
||||||
let write_timeout = Duration::from_secs(2);
|
|
||||||
let retries = 1;
|
|
||||||
let timeout_settings = TimeoutSettings::new(
|
|
||||||
Some(read_timeout),
|
|
||||||
Some(write_timeout),
|
|
||||||
retries
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
let response = valve::query(
|
|
||||||
&addr,
|
|
||||||
engine,
|
|
||||||
Some(gather_settings),
|
|
||||||
Some(timeout_settings)
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(response?)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Retrieve the server status from ATS
|
|
||||||
#[poise::command(slash_command)]
|
|
||||||
pub async fn ats_status(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
|
||||||
match query_server() {
|
|
||||||
Ok(response) => {
|
|
||||||
ctx.send(|m| m.embed(|e|
|
|
||||||
e.color(COLOR)
|
|
||||||
.title("American Truck Simulator Server Status")
|
|
||||||
.fields(vec![
|
|
||||||
("Name", format!("{}", response.info.name), true),
|
|
||||||
("Players", format!("{}/{}", response.info.players_online, response.info.players_maximum), true)
|
|
||||||
])
|
|
||||||
)).await?;
|
|
||||||
}
|
|
||||||
Err(why) => println!("Error querying the server: {:?}", why)
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@ -1,3 +1,2 @@
|
|||||||
pub mod ping;
|
pub mod ping;
|
||||||
pub mod wg_status;
|
pub mod status;
|
||||||
pub mod ats_status;
|
|
||||||
|
@ -1,13 +1,23 @@
|
|||||||
use crate::{Error, COLOR};
|
use crate::{Error, COLOR};
|
||||||
|
|
||||||
|
use gamedig::protocols::{
|
||||||
|
valve::{
|
||||||
|
Engine, GatheringSettings, Response
|
||||||
|
},
|
||||||
|
types::TimeoutSettings,
|
||||||
|
valve,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
str::FromStr,
|
||||||
|
net::SocketAddr,
|
||||||
|
time::Duration,
|
||||||
|
collections::HashMap,
|
||||||
|
env::var
|
||||||
|
};
|
||||||
use reqwest::{
|
use reqwest::{
|
||||||
Client,
|
Client,
|
||||||
header::USER_AGENT
|
header::USER_AGENT
|
||||||
};
|
};
|
||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
env::var
|
|
||||||
};
|
|
||||||
use cargo_toml::Manifest;
|
use cargo_toml::Manifest;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use tokio::join;
|
use tokio::join;
|
||||||
@ -16,9 +26,78 @@ lazy_static::lazy_static! {
|
|||||||
static ref PMS_BASE: String = var("WG_PMS").expect("Expected a \"WG_PMS\" in the envvar but none was found");
|
static ref PMS_BASE: String = var("WG_PMS").expect("Expected a \"WG_PMS\" in the envvar but none was found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn query_server() -> Result<Response, Error> {
|
||||||
|
let server_ip = var("ATS_SERVER_IP").expect("Expected a \"ATS_SERVER_IP\" in the envvar but none was found");
|
||||||
|
let addr = SocketAddr::from_str(&server_ip).unwrap();
|
||||||
|
let engine = Engine::Source(None);
|
||||||
|
let gather_settings = GatheringSettings {
|
||||||
|
players: true,
|
||||||
|
rules: false,
|
||||||
|
check_app_id: false
|
||||||
|
};
|
||||||
|
|
||||||
|
let read_timeout = Duration::from_secs(2);
|
||||||
|
let write_timeout = Duration::from_secs(2);
|
||||||
|
let retries = 1;
|
||||||
|
let timeout_settings = TimeoutSettings::new(
|
||||||
|
Some(read_timeout),
|
||||||
|
Some(write_timeout),
|
||||||
|
retries
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let response = valve::query(
|
||||||
|
&addr,
|
||||||
|
engine,
|
||||||
|
Some(gather_settings),
|
||||||
|
Some(timeout_settings)
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(response?)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
|
||||||
|
let bot_version = Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap();
|
||||||
|
|
||||||
|
let client = Client::new();
|
||||||
|
let req = client.get(url)
|
||||||
|
.header(USER_AGENT, format!("Kon/{}/Rust", bot_version))
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
let response = req.json::<HashMap<String, Value>>().await?;
|
||||||
|
let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone();
|
||||||
|
|
||||||
|
Ok(servers)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Query the server statuses
|
||||||
|
#[poise::command(slash_command, subcommands("ats", "wg"), subcommand_required)]
|
||||||
|
pub async fn status(_: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieve the server status from ATS
|
||||||
|
#[poise::command(slash_command)]
|
||||||
|
pub async fn ats(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
||||||
|
match query_server() {
|
||||||
|
Ok(response) => {
|
||||||
|
ctx.send(|m| m.embed(|e|
|
||||||
|
e.color(COLOR)
|
||||||
|
.title("American Truck Simulator Server Status")
|
||||||
|
.fields(vec![
|
||||||
|
("Name", format!("{}", response.info.name), true),
|
||||||
|
("Players", format!("{}/{}", response.info.players_online, response.info.players_maximum), true)
|
||||||
|
])
|
||||||
|
)).await?;
|
||||||
|
}
|
||||||
|
Err(why) => println!("Error querying the server: {:?}", why)
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Retrieve the server statuses from Wargaming
|
/// Retrieve the server statuses from Wargaming
|
||||||
#[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
pub async fn wg_status(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
pub async fn wg(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
||||||
let pms_asia = &PMS_BASE;
|
let pms_asia = &PMS_BASE;
|
||||||
let pms_eu = PMS_BASE.replace("asia", "eu");
|
let pms_eu = PMS_BASE.replace("asia", "eu");
|
||||||
|
|
||||||
@ -53,17 +132,3 @@ pub async fn wg_status(ctx: poise::Context<'_, (), Error>) -> Result<(), Error>
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
|
|
||||||
let bot_version = Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap();
|
|
||||||
|
|
||||||
let client = Client::new();
|
|
||||||
let req = client.get(url)
|
|
||||||
.header(USER_AGENT, format!("Kon/{}/Rust", bot_version))
|
|
||||||
.send()
|
|
||||||
.await?;
|
|
||||||
let response = req.json::<HashMap<String, Value>>().await?;
|
|
||||||
let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone();
|
|
||||||
|
|
||||||
Ok(servers)
|
|
||||||
}
|
|
16
src/main.rs
16
src/main.rs
@ -1,6 +1,7 @@
|
|||||||
mod commands;
|
mod commands;
|
||||||
|
|
||||||
use poise::serenity_prelude::{self as serenity};
|
use poise::serenity_prelude::{self as serenity};
|
||||||
|
use std::env::var;
|
||||||
|
|
||||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ async fn on_ready(
|
|||||||
)
|
)
|
||||||
)).await?;
|
)).await?;
|
||||||
|
|
||||||
let register_commands = std::env::var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true);
|
let register_commands = var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true);
|
||||||
|
|
||||||
if register_commands {
|
if register_commands {
|
||||||
let builder = poise::builtins::create_application_commands(&framework.options().commands);
|
let builder = poise::builtins::create_application_commands(&framework.options().commands);
|
||||||
@ -31,11 +32,9 @@ async fn on_ready(
|
|||||||
}).await;
|
}).await;
|
||||||
|
|
||||||
match commands {
|
match commands {
|
||||||
Ok(cmdmap) => {
|
Ok(cmdmap) => for command in cmdmap.iter() {
|
||||||
for command in cmdmap.iter() {
|
|
||||||
println!("Registered command globally: {}", command.name);
|
println!("Registered command globally: {}", command.name);
|
||||||
}
|
},
|
||||||
},
|
|
||||||
Err(why) => println!("Error registering commands: {:?}", why)
|
Err(why) => println!("Error registering commands: {:?}", why)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,15 +44,14 @@ async fn on_ready(
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let token = std::env::var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found");
|
let token = var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found");
|
||||||
|
|
||||||
let client = poise::Framework::builder().token(token)
|
let client = poise::Framework::builder().token(token)
|
||||||
.intents(serenity::GatewayIntents::MESSAGE_CONTENT | serenity::GatewayIntents::GUILDS)
|
.intents(serenity::GatewayIntents::GUILDS)
|
||||||
.options(poise::FrameworkOptions {
|
.options(poise::FrameworkOptions {
|
||||||
commands: vec![
|
commands: vec![
|
||||||
commands::ping::ping(),
|
commands::ping::ping(),
|
||||||
commands::wg_status::wg_status(),
|
commands::status::status()
|
||||||
commands::ats_status::ats_status()
|
|
||||||
],
|
],
|
||||||
pre_command: |ctx| Box::pin(async move {
|
pre_command: |ctx| Box::pin(async move {
|
||||||
let get_guild_name = match ctx.guild() {
|
let get_guild_name = match ctx.guild() {
|
||||||
|
Loading…
Reference in New Issue
Block a user