This commit is contained in:
Sakuhl 2018-01-02 00:07:11 +01:00
parent 56bf12b4de
commit 37c369cfcb

View File

@ -1,130 +1,17 @@
extern crate serenity;
extern crate wynncraft;
use wynncraft::{Guild, Player};
extern crate serenity;
use serenity::prelude::*;
use serenity::model::*;
extern crate reqwest;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use serde_json::Value;
use std::collections::HashMap;
use std::env;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Guild {
pub name: String,
pub prefix: String,
pub members: Vec<Member>,
pub xp: f64,
pub level: u64,
pub created: String,
pub created_friendly: String,
pub territories: u64
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Member {
pub name: String,
pub rank: Rank,
pub contributed: u64,
pub joined: String,
pub joined_friendly: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Rank {
Owner,
Chief,
Captain,
Recruiter,
Recruit
}
#[derive(Deserialize)]
#[allow(non_snake_case)]
pub struct Player {
pub username: String,
pub rank: String,
pub tag: String,
pub displayTag: bool,
pub veteran: bool,
pub playtime: u64,
pub first_join: String,
pub last_join: String,
pub first_join_friendly: String,
pub last_join_friendly: String,
pub current_server: String,
pub global: GlobalPlayerInfo,
pub classes: HashMap<String, Class>,
pub wizard_fortress: WFPlayerInfo,
pub rankings: PlayerRankings,
pub guild: PlayerGuildInfo
}
#[derive(Deserialize)]
pub struct GlobalPlayerInfo {
pub items_identified: u64,
pub mobs_killed: u64,
pub pvp_kills: u64,
pub pvp_deaths: u64,
pub chests_found: u64,
pub blocks_walked: u64,
pub logins: u64,
pub deaths: u64,
pub total_level: u64
}
#[derive(Deserialize)]
#[allow(non_snake_case)]
pub struct Class {
pub level: u64,
pub xp: f64,
pub dungeons: Value,
pub quests: Vec<String>,
pub dungeonsAmount: String,
pub questsAmount: String,
pub skills: Value,
pub items_identified: u64,
pub mobs_killed: u64,
pub pvp_kills: u64,
pub pvp_deaths: u64,
pub chests_found: u64,
pub blocks_walked: u64,
pub logins: u64,
pub deaths: u64,
pub events_won: u64,
/// in minutes
pub playtime: u64
}
#[derive(Deserialize)]
pub struct WFPlayerInfo {
pub kills: u64,
pub deaths: u64,
pub wins: u64,
pub losses: u64
}
#[derive(Deserialize)]
pub struct PlayerRankings {
pub pvp: u64,
pub player: u64,
pub guild: u64
}
#[derive(Deserialize)]
pub struct PlayerGuildInfo {
pub name: String,
pub rank: String
}
struct Handler;
impl EventHandler for Handler {
@ -135,11 +22,31 @@ impl EventHandler for Handler {
// of a single event can be dispatched simultaneously.
fn on_message(&self, _: Context, msg: Message) {
if msg.content.starts_with("wc!guild ") {
let guild = &msg.content[9..];
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", guild)).unwrap();
assert!(resp.status().is_success());
wc_guild(msg);
} else if msg.content.starts_with("wc!territory ") {
wc_territory(msg);
} else if msg.content.starts_with("wc!status") {
wc_status(msg);
} else if msg.content.starts_with("wc!player ") {
wc_player(msg);
}
}
if let Ok(guild) = serde_json::from_reader(resp) {
// Set a handler to be called on the `on_ready` event. This is called when a
// shard is booted, and a READY payload is sent by Discord. This payload
// contains data like the current user's guild Ids, current user data,
// private channels, and more.
//
// In this case, just print what the current user's username is.
fn on_ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
fn wc_guild(msg: Message) {
let guild = &msg.content[9..];
if let Ok(guild) = wynncraft::guild(guild) {
let guild: Guild = guild;
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
@ -172,7 +79,19 @@ impl EventHandler for Handler {
) + &message
).unwrap();
}
} else if msg.content.starts_with("wc!territory ") {
}
fn wc_status(msg: Message) {
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayersSum").unwrap();
let value: Value = serde_json::from_reader(resp).unwrap();
msg.channel_id.say(
format!("{} players online", value.get("players_online").unwrap().as_u64().unwrap())
).unwrap();
}
fn wc_territory(msg: Message) {
let territory = &msg.content[13..];
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=territoryList").unwrap();
@ -200,20 +119,18 @@ impl EventHandler for Handler {
message
).unwrap();
}
} else if msg.content.starts_with("wc!status") {
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayersSum").unwrap();
}
let value: Value = serde_json::from_reader(resp).unwrap();
msg.channel_id.say(
format!("{} players online", value.get("players_online").unwrap().as_u64().unwrap())
).unwrap();
} else if msg.content.starts_with("wc!player ") {
fn wc_player(msg: Message) {
let player = &msg.content[10..];
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=playerStats&command={}", player)).unwrap();
let player: Player = wynncraft::player(player).unwrap();
let player: Player = serde_json::from_reader(resp).unwrap();
let current_server_msg = if player.current_server != "null" {
format!("\n**Currently online on**: {}", player.current_server)
} else {
String::new()
};
msg.channel_id.say(
format!(
@ -222,28 +139,17 @@ impl EventHandler for Handler {
**Playtime**: {} hours
**Kills**: {} mobs & {} players
**Chests looted**: {}
**Total level**: {}",
**Total level**: {}{}",
player.username,
player.guild.name,
player.playtime / 60,
player.global.mobs_killed, player.global.pvp_kills,
player.global.chests_found,
player.classes.iter().map(|(_, x)| x.level).sum::<u64>()
player.classes.iter().map(|(_, x)| x.level).sum::<u64>(),
current_server_msg
)
).unwrap();
}
}
// Set a handler to be called on the `on_ready` event. This is called when a
// shard is booted, and a READY payload is sent by Discord. This payload
// contains data like the current user's guild Ids, current user data,
// private channels, and more.
//
// In this case, just print what the current user's username is.
fn on_ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
fn main() {
// Configure the client with your Discord bot token in the environment.