Create internals dir and optimize usage of Reqwest
All checks were successful
Build and push container image / build (push) Successful in 11m10s
All checks were successful
Build and push container image / build (push) Successful in 11m10s
This commit is contained in:
parent
b5847fcd88
commit
38bfcf62a9
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -837,7 +837,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "kon"
|
||||
version = "0.1.20"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"cargo_toml",
|
||||
"gamedig",
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "kon"
|
||||
version = "0.1.20"
|
||||
version = "0.2.0"
|
||||
rust-version = "1.75"
|
||||
edition = "2021"
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
Error,
|
||||
utils::EMBED_COLOR,
|
||||
internals::utils::EMBED_COLOR,
|
||||
models::gameservers::Gameservers
|
||||
};
|
||||
|
||||
|
@ -2,18 +2,14 @@ use crate::{
|
||||
Error,
|
||||
models::gameservers::Gameservers,
|
||||
commands::gameserver::ac_server_name,
|
||||
utils::BOT_VERSION,
|
||||
utils::EMBED_COLOR
|
||||
internals::utils::EMBED_COLOR,
|
||||
internals::http::HttpClient
|
||||
};
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
env::var
|
||||
};
|
||||
use reqwest::{
|
||||
Client,
|
||||
header::USER_AGENT
|
||||
};
|
||||
use tokio::join;
|
||||
use poise::CreateReply;
|
||||
use serenity::builder::CreateEmbed;
|
||||
@ -45,11 +41,9 @@ struct MinecraftPlayers {
|
||||
}
|
||||
|
||||
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
|
||||
let client = Client::new();
|
||||
let req = client.get(url)
|
||||
.header(USER_AGENT, format!("Kon/{}/Rust", &**BOT_VERSION))
|
||||
.send()
|
||||
.await?;
|
||||
let client = HttpClient::new();
|
||||
let req = client.get(url).await?;
|
||||
|
||||
let response = req.json::<HashMap<String, Value>>().await?;
|
||||
let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone();
|
||||
|
||||
@ -57,11 +51,8 @@ async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
|
||||
}
|
||||
|
||||
async fn gs_query_minecraft(server_ip: &str) -> Result<MinecraftQueryData, Error> {
|
||||
let client = Client::new();
|
||||
let req = client.get(format!("https://api.mcsrvstat.us/2/{}", server_ip))
|
||||
.header(USER_AGENT, format!("Kon/{}/Rust", &**BOT_VERSION))
|
||||
.send()
|
||||
.await?;
|
||||
let client = HttpClient::new();
|
||||
let req = client.get(&format!("https://api.mcsrvstat.us/2/{}", server_ip)).await?;
|
||||
|
||||
if req.status().is_success() {
|
||||
let data: MinecraftQueryData = req.json().await?;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
Error,
|
||||
utils::{
|
||||
internals::utils::{
|
||||
format_duration,
|
||||
concat_message,
|
||||
BOT_VERSION
|
||||
@ -35,7 +35,7 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
||||
}
|
||||
|
||||
let stat_msg = vec![
|
||||
format!("**{} {}**", _bot.name, &**BOT_VERSION),
|
||||
format!("**{} {}**", _bot.name, BOT_VERSION.as_str()),
|
||||
format!(">>> System: `{}`", format_duration(sys_uptime)),
|
||||
format!("Process: `{}`", format_duration(proc_uptime))
|
||||
];
|
||||
|
30
src/internals/http.rs
Normal file
30
src/internals/http.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use std::sync::Arc;
|
||||
use once_cell::sync::Lazy;
|
||||
use reqwest::{
|
||||
Client,
|
||||
header::USER_AGENT
|
||||
};
|
||||
|
||||
static CUSTOM_USER_AGENT: Lazy<String> = Lazy::new(||
|
||||
format!("Kon/{}/Rust", super::utils::BOT_VERSION.as_str())
|
||||
);
|
||||
|
||||
pub struct HttpClient {
|
||||
client: Arc<Client>
|
||||
}
|
||||
|
||||
impl HttpClient {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
client: Arc::new(Client::new())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(&self, url: &str) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let req = self.client.get(url)
|
||||
.header(USER_AGENT, CUSTOM_USER_AGENT.as_str())
|
||||
.send()
|
||||
.await?;
|
||||
Ok(req)
|
||||
}
|
||||
}
|
2
src/internals/mod.rs
Normal file
2
src/internals/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod utils;
|
||||
pub mod http;
|
@ -1,7 +1,7 @@
|
||||
mod commands;
|
||||
mod controllers;
|
||||
mod models;
|
||||
mod utils;
|
||||
mod internals;
|
||||
|
||||
use poise::serenity_prelude::{self as serenity};
|
||||
use std::{
|
||||
@ -33,7 +33,7 @@ async fn on_ready(
|
||||
|
||||
let message = CreateMessage::new();
|
||||
let ready_embed = CreateEmbed::new()
|
||||
.color(utils::EMBED_COLOR)
|
||||
.color(internals::utils::EMBED_COLOR)
|
||||
.thumbnail(ready.user.avatar_url().unwrap_or_default())
|
||||
.author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user