Logging
This commit is contained in:
parent
2373741bc3
commit
ffea97668a
202
Cargo.lock
generated
202
Cargo.lock
generated
@ -35,6 +35,56 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.102"
|
||||
@ -58,7 +108,7 @@ checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -231,6 +281,12 @@ dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||
|
||||
[[package]]
|
||||
name = "combine"
|
||||
version = "4.6.7"
|
||||
@ -331,7 +387,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"strsim",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -342,7 +398,7 @@ checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -404,7 +460,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -422,6 +478,29 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_filter"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32e90c2accc4b07a8456ea0debdc2e7587bdd890680d71173a15d4ae604f6eef"
|
||||
dependencies = [
|
||||
"log",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.11.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0621c04f2196ac3f488dd583365b9c09be011a4ab8b9f37248ffcc8f6198b56a"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"env_filter",
|
||||
"jiff",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.1"
|
||||
@ -546,7 +625,7 @@ checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -907,7 +986,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -953,12 +1032,42 @@ version = "2.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||
|
||||
[[package]]
|
||||
name = "jiff"
|
||||
version = "0.2.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f00b5dbd620d61dfdcb6007c9c1f6054ebd75319f163d886a9055cec1155073d"
|
||||
dependencies = [
|
||||
"jiff-static",
|
||||
"log",
|
||||
"portable-atomic",
|
||||
"portable-atomic-util",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jiff-static"
|
||||
version = "0.2.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e000de030ff8022ea1da3f466fbb0f3a809f5e51ed31f6dd931c35181ad8e6d7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jni"
|
||||
version = "0.22.4"
|
||||
@ -986,7 +1095,7 @@ dependencies = [
|
||||
"quote",
|
||||
"rustc_version",
|
||||
"simd_cesu8",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1005,7 +1114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1060,9 +1169,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.22"
|
||||
version = "0.4.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
||||
|
||||
[[package]]
|
||||
name = "lru-slab"
|
||||
@ -1159,6 +1268,12 @@ version = "1.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "openssl-probe"
|
||||
version = "0.2.1"
|
||||
@ -1202,10 +1317,12 @@ checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
|
||||
|
||||
[[package]]
|
||||
name = "playerbot"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"env_logger",
|
||||
"futures",
|
||||
"log",
|
||||
"poise",
|
||||
"reqwest 0.13.3",
|
||||
"serde",
|
||||
@ -1233,6 +1350,21 @@ dependencies = [
|
||||
"trim-in-place",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic-util"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618"
|
||||
dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
@ -1250,9 +1382,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.88"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
@ -1326,9 +1458,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.37"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
@ -1783,7 +1915,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1853,7 +1985,7 @@ dependencies = [
|
||||
"darling",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1991,9 +2123,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.87"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -2017,7 +2149,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2086,7 +2218,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2097,7 +2229,7 @@ checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2182,7 +2314,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2301,7 +2433,7 @@ checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2390,7 +2522,7 @@ checksum = "536b6812192bda8551cfa0e52524e328c6a951b48e66529ee4522d6c721243d6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2441,6 +2573,12 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
@ -2525,7 +2663,7 @@ dependencies = [
|
||||
"bumpalo",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
@ -2628,7 +2766,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2639,7 +2777,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2806,7 +2944,7 @@ checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
@ -2827,7 +2965,7 @@ checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2847,7 +2985,7 @@ checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
@ -2876,7 +3014,7 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.87",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
[package]
|
||||
name = "playerbot"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.102"
|
||||
env_logger = "0.11.10"
|
||||
futures = "0.3.32"
|
||||
log = "0.4.29"
|
||||
poise = "0.6.2"
|
||||
reqwest = {version = "0.13.3", features = ["json"]}
|
||||
serde = {version = "1.0.215", features = ["derive", "serde_derive"]}
|
||||
|
||||
@ -12,7 +12,7 @@ Playerbot is a utility to monitor the status of game servers through Discord bot
|
||||
|
||||
## Configuration
|
||||
|
||||
Place a file named config.json at the current working directory.
|
||||
The program requires a file named config.json to be present in its current working directory. Below is an example of its schema.
|
||||
|
||||
```json
|
||||
{
|
||||
@ -29,7 +29,7 @@ Place a file named config.json at the current working directory.
|
||||
}
|
||||
```
|
||||
|
||||
The file should consist of a list of entries, each representing a single server, represented by a single bot. The address can either be `Domain`, `IPv4`, or `IPv6`, representing a hostname, an IPv4 address, or an IPv6 address, respectively.
|
||||
The file should consist of a list of entries, each representing a single server, represented by a single bot. The address can either be `Domain`, `IPv4`, or `IPv6`, representing a hostname, an IPv4 address, or an IPv6 address, respectively. Port numbers are supported.
|
||||
|
||||
Presently, only Minecraft servers are supported, but soon I will backport support for the SCP: Secret Laboratory handler.
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
|
||||
|
||||
use crate::request::request;
|
||||
use crate::types::*;
|
||||
use log::*;
|
||||
use poise::serenity_prelude as serenity;
|
||||
use poise::serenity_prelude::{ActivityData, ClientBuilder, GatewayIntents};
|
||||
use tokio::sync::Mutex;
|
||||
@ -41,7 +40,7 @@ impl BotRunner {
|
||||
.framework(framework)
|
||||
.activity(ActivityData::custom("Waiting on first heartbeat"))
|
||||
.await
|
||||
.unwrap();
|
||||
.expect("Could not build client");
|
||||
BotRunner { client }
|
||||
}
|
||||
|
||||
@ -60,9 +59,16 @@ pub async fn event_handler(
|
||||
serenity::FullEvent::Ready {
|
||||
data_about_bot: _bot_data,
|
||||
} => loop {
|
||||
println!("Checking status: {}", data.server.api_address());
|
||||
let http_response = request(data.server.api_address()).await.unwrap();
|
||||
let results = data.server.parse(http_response.text().await.unwrap());
|
||||
info!("Checking status: {}", data.server.api_address());
|
||||
let http_response = request(data.server.api_address())
|
||||
.await
|
||||
.expect("Error executing request");
|
||||
let results = data.server.parse(
|
||||
http_response
|
||||
.text()
|
||||
.await
|
||||
.expect("Response had no body text"),
|
||||
);
|
||||
match &results {
|
||||
ServerResponse::Offline => {
|
||||
ctx.set_presence(
|
||||
@ -86,7 +92,7 @@ pub async fn event_handler(
|
||||
);
|
||||
}
|
||||
}
|
||||
//println!("{:#?}", &results);
|
||||
trace!("Response content: {:#?}", &results);
|
||||
*data.cached_reply.lock().await = Some(results);
|
||||
tokio::time::sleep(std::time::Duration::from_secs(30)).await;
|
||||
},
|
||||
@ -131,10 +137,11 @@ pub async fn players(
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn join(ctx: poise::Context<'_, Data, serenity::Error>) -> Result<(), serenity::Error> {
|
||||
let cache = ctx.data().cached_reply.lock().await;
|
||||
match cache.as_ref().unwrap() {
|
||||
match cache.as_ref().expect("Cache is not present") {
|
||||
ServerResponse::Offline => {
|
||||
ctx.say("Server is offline! Cannot get server info.").await?;
|
||||
},
|
||||
ctx.say("Server is offline! Cannot get server info.")
|
||||
.await?;
|
||||
}
|
||||
ServerResponse::Online(on) => {
|
||||
ctx.say(format!(
|
||||
"To join, type `{}` in the server URL box or search bar.\nEnsure you are on version `{}`.",
|
||||
|
||||
@ -3,6 +3,8 @@ use serde::Deserialize;
|
||||
use std::{fs::File, path::Path};
|
||||
use url::Host;
|
||||
|
||||
const SCHEMA_VERSION: &str = "0.1.0";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ConfigEntry {
|
||||
handler_type: String,
|
||||
@ -22,7 +24,7 @@ pub fn parse_configs(path: &Path) -> Result<Config, anyhow::Error> {
|
||||
}
|
||||
|
||||
pub fn build_handlers(conf: Config) -> Vec<Box<dyn ServerInfo>> {
|
||||
assert_eq!(conf.version, "0.1.0");
|
||||
assert_eq!(conf.version, SCHEMA_VERSION);
|
||||
let mut results: Vec<Box<dyn ServerInfo>> = vec![];
|
||||
for item in conf.entries {
|
||||
#[allow(clippy::single_match)]
|
||||
|
||||
@ -67,23 +67,29 @@ impl ServerInfo for Server {
|
||||
|
||||
fn api_address(&self) -> Url {
|
||||
let url_string = format!("https://api.mcsrvstat.us/3/{}", self.addr);
|
||||
Url::parse(&url_string).unwrap()
|
||||
Url::parse(&url_string).expect("Could not parse URL")
|
||||
}
|
||||
|
||||
fn parse(&self, response: String) -> ServerResponse {
|
||||
let parsed_data: ApiResponse = serde_json::from_str(&response).unwrap();
|
||||
let parsed_data: ApiResponse =
|
||||
serde_json::from_str(&response).expect("API returned malformed data");
|
||||
match parsed_data.online {
|
||||
true => {
|
||||
let players = parsed_data.players.unwrap();
|
||||
let motd = parsed_data.motd.as_ref().unwrap();
|
||||
let players = parsed_data
|
||||
.players
|
||||
.expect("Players field unexpectedly absent");
|
||||
let motd = parsed_data
|
||||
.motd
|
||||
.as_ref()
|
||||
.expect("MOTD field unexpectedly absent");
|
||||
self::ServerResponse::Online(ServerOnlineResponse {
|
||||
searchable_name: self.addr.to_string(),
|
||||
readable_name: motd.clean.first().unwrap().into(),
|
||||
reported_name: motd.clean.first().unwrap().into(),
|
||||
clean_name: motd.clean.first().unwrap().into(),
|
||||
readable_name: motd.clean.first().unwrap_or(&"(no name)".to_owned()).into(),
|
||||
reported_name: motd.clean.first().unwrap_or(&"(no name)".to_owned()).into(),
|
||||
clean_name: motd.clean.first().unwrap_or(&"(no name)".to_owned()).into(),
|
||||
players_online: players.online,
|
||||
player_limit: players.max,
|
||||
version: parsed_data.version.unwrap(),
|
||||
version: parsed_data.version.unwrap_or("(no version)".to_owned()),
|
||||
players: Some(
|
||||
players
|
||||
.list
|
||||
|
||||
@ -4,14 +4,20 @@ mod handlers;
|
||||
mod request;
|
||||
mod types;
|
||||
|
||||
use env_logger;
|
||||
use log::*;
|
||||
use crate::bot_runner::BotRunner;
|
||||
use futures::{self, future::try_join_all};
|
||||
use std::path::Path;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let config = config_parser::parse_configs(Path::new("config.json")).unwrap();
|
||||
env_logger::init();
|
||||
let config = config_parser::parse_configs(Path::new("config.json"))
|
||||
.expect("Could not parse config.json");
|
||||
info!("Got config file");
|
||||
let handlers = config_parser::build_handlers(config);
|
||||
info!("Parsed {} handlers", handlers.len());
|
||||
let mut bots: Vec<bot_runner::BotRunner> = vec![];
|
||||
for item in handlers {
|
||||
bots.push(BotRunner::new(item).await);
|
||||
|
||||
@ -1,9 +1,14 @@
|
||||
use reqwest::{header::HeaderValue, Response};
|
||||
use url::Url;
|
||||
|
||||
const USER_AGENT: &str = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Mobile Safari/537.36";
|
||||
|
||||
pub async fn request(url: Url) -> Result<Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let mut request = reqwest::Request::new(reqwest::Method::GET, url);
|
||||
request.headers_mut().append("User-Agent", HeaderValue::from_str("Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Mobile Safari/537.36").unwrap());
|
||||
request.headers_mut().append(
|
||||
"User-Agent",
|
||||
HeaderValue::from_str(USER_AGENT).expect("Could not build header"),
|
||||
);
|
||||
client.execute(request).await
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user