use reqwest::{self, header::HeaderMap, Method, Error};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct BaseResponse {
error: bool,
status: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebsiteStatistics {
#[serde(flatten)]
base: BaseResponse,
servers: Servers,
bots: Bots,
users: Users,
templates: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Servers {
total: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Bots {
total: u64,
approved: u64,
premium: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Users {
total: u64,
premium: u64,
staff: Staff,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Staff {
total: u64,
mods: u64,
assistants: u64,
admins: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebsiteHealth {
#[serde(flatten)]
base: BaseResponse,
redis_ok: bool,
mongo_ok: bool,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BotInformation {
#[serde(flatten)]
base: BaseResponse,
bot: Bot,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Bot {
id: String,
name: String,
prefix: String,
tags: Vec<String>,
vanity_url: String,
server_count: u64,
shard_count: u64,
short_desc: String,
long_desc: String,
editors: Vec<String>,
owner: Owner,
avatar: Avatar,
links: Links,
status: Status,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Owner {
id: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Avatar {
hash: String,
url: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Links {
invite: String,
support: String,
website: String,
donation: String,
repo: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Status {
approved: bool,
site_bot: bool,
archived: bool,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BotStatistics {
#[serde(flatten)]
base: BaseResponse,
guild_count: u64,
shard_count: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ServerInformation {
#[serde(flatten)]
base: BaseResponse,
server: Server,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Server {
id: String,
name: String,
short_desc: String,
long_desc: String,
tags: Vec<String>,
owner: Owner,
icon: Icon,
links: Links,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Icon {
hash: String,
url: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct TemplateInformation {
#[serde(flatten)]
base: BaseResponse,
template: Template,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Template {
id: String,
name: String,
region: String,
locale: String,
afk_timeout: u64,
verification_level: u64,
default_message_notifications: u64,
explicit_content: u64,
roles: Vec<APIRole>,
channels: Vec<APIChannel>,
usage_count: u64,
short_desc: String,
long_desc: String,
tags: Vec<String>,
from_guild: String,
owner: Owner,
icon: Icon,
links: Links,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct APIRole {
id: String,
name: String,
color: u64,
hoist: bool,
position: u64,
permissions: u64,
managed: bool,
mentionable: bool,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct APIChannel {
id: String,
name: String,
type_: u64,
position: u64,
permission_overwrites: Vec<PermissionOverwrite>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct PermissionOverwrite {
id: String,
type_: u64,
allow: u64,
deny: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct UserInformation {
#[serde(flatten)]
base: BaseResponse,
user: User,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct User {
id: String,
name: String,
discrim: String,
full_username: String,
avatar: Avatar,
profile: Profile,
game: Game,
rank: Rank,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Profile {
bio: String,
links: Links,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Game {
snakes: Snakes,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Snakes {
max_score: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Rank {
admin: bool,
mod_: bool,
assistant: bool,
tester: bool,
translator: bool,
covid: bool,
}
pub struct Auth {
pub token: String,
pub id: String,
}
pub struct Del {
token: String,
id: String,
}
impl Del {
pub fn new(token: String, id: String) -> Self {
Self { token, id }
}
async fn request<T>(
&self,
auth: Option<Auth>,
method: Method,
route: &str,
body: Option<String>,
) -> Result<impl Serialize, Error>
where
for<'a> T: Serialize + Deserialize<'a>,
{
let client = reqwest::Client::new();
let mut headers = HeaderMap::new();
headers.insert(
"User-Agent",
format!(
"del-rs ( {}, {} )",
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_REPOSITORY")
).parse().unwrap(),
);
if let Some(auth) = auth {
headers.insert("Authorization", auth.token.parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
}
let mut res = client
.request(
method,
format!("https://api.discordextremelist.xyz/v2/{}", route),
)
.headers(headers)
.body(body.unwrap_or("".to_owned()))
.send()
.await?;
res.json::<T>().await
}
}