Initial commit

This commit is contained in:
toast 2023-12-08 14:01:13 +11:00
commit 1a71c92a8e
9 changed files with 2600 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
.env

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
],
"rust-analyzer.showUnlinkedFileNotification": false
}

2370
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

26
Cargo.toml Normal file
View File

@ -0,0 +1,26 @@
[package]
name = "kon"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lazy_static = "1.4.0"
poise = "0.5.7"
reqwest = "0.11.22"
serde_json = "1.0.108"
serenity = "0.12.0"
tokio = { version = "1.34.0", features = ["macros", "signal", "rt-multi-thread"] }
[[bin]]
name = "kon"
path = "src/main.rs"
[profile.dev]
opt-level = 0
debug = true
[profile.release]
opt-level = 3
debug = false

37
renovate.json Normal file
View File

@ -0,0 +1,37 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended",
"default:automergeDigest",
"default:automergeBranchPush",
"helpers:pinGitHubActionDigests"
],
"commitBody": "PR created by Renovate Bot.",
"timezone": "Australia/Sydney",
"vulnerabilityAlerts": {
"groupName": "renovate-security",
"schedule": ["before 11pm every day"],
"dependencyDashboardApproval": false,
"minimumReleaseAge": "1h",
"rangeStrategy": "update-lockfile",
"commitMessageSuffix": "[SECURITY]",
"branchTopic": "{{{datasource}}}-{{{depName}}}-vulnerability",
"prCreation": "immediate"
},
"pinDigests": true,
"ignoreTests": true,
"pruneStaleBranches": true,
"pruneBranchAfterAutomerge": true,
"automerge": true,
"automergeType": "pr",
"automergeStrategy": "squash",
"automergeSchedule": [
"at any time"
],
"packageRules": [
{
"matchManagers": ["cargo"],
"enabled": true
}
]
}

2
src/commands/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod ping;
pub mod status;

8
src/commands/ping.rs Normal file
View File

@ -0,0 +1,8 @@
use crate::Error;
/// Check if the bot is alive
#[poise::command(slash_command)]
pub async fn ping(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
ctx.reply(format!("Powong! `{:?}`", ctx.ping().await)).await?;
Ok(())
}

80
src/commands/status.rs Normal file
View File

@ -0,0 +1,80 @@
use crate::Error;
use reqwest::get;
use std::collections::HashMap;
use serde_json::Value;
lazy_static::lazy_static! {
static ref PMS_BASE: String = std::env::var("WG_PMS").expect("Expected a \"WG_PMS\" in the envvar but none was found");
}
/// Retrieve the server data from Wargaming
#[poise::command(slash_command, subcommands("asia", "europe"))]
pub async fn status(_ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
Ok(())
}
/// Check the server status for Asia server
#[poise::command(slash_command)]
pub async fn asia(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
let asia_pms = &PMS_BASE;
let servers = pms_serverstatus(&asia_pms).await?;
let mut fields = Vec::new();
for server in servers {
let name = server["name"].as_str().unwrap().to_owned();
let status = match server["availability"].as_str().unwrap() {
"1" => "Online",
"-1" => "Offline",
_ => "Unknown"
};
fields.push((name, status, true));
}
ctx.send(|m|
m.embed(|e| {
e.color(crate::COLOR)
.title("World of Tanks Asia Status")
.fields(fields)
})
).await?;
Ok(())
}
/// Check the server status for EU servers
#[poise::command(slash_command)]
pub async fn europe(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
let eu_pms = PMS_BASE.replace("asia", "eu");
let servers = pms_serverstatus(&eu_pms).await?;
let mut fields = Vec::new();
for server in servers {
let name = server["name"].as_str().unwrap().to_owned();
let status = match server["availability"].as_str().unwrap() {
"1" => "Online",
"-1" => "Offline",
_ => "Unknown"
};
fields.push((name, status, true));
}
ctx.send(|m|
m.embed(|e| {
e.color(crate::COLOR)
.title("World of Tanks Europe Status")
.fields(fields)
})
).await?;
Ok(())
}
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
let response = get(url).await?.json::<HashMap<String, Value>>().await?;
let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone();
Ok(servers)
}

69
src/main.rs Normal file
View File

@ -0,0 +1,69 @@
mod commands;
use poise::serenity_prelude::{self as serenity};
type Error = Box<dyn std::error::Error + Send + Sync>;
pub static COLOR: i32 = 0x5a99c7;
async fn on_ready(
ctx: &serenity::Context,
ready: &serenity::Ready,
framework: &poise::Framework<(), Error>
) -> Result<(), Error> {
println!("Connected to API as {}", ready.user.name);
serenity::ChannelId(865673694184996888).send_message(&ctx.http, |m| {
m.embed(|e|
e.color(COLOR)
.thumbnail(ready.user.avatar_url().unwrap_or_default())
.author(|a|
a.name(format!("{} is ready!", ready.user.name))
)
)
}).await?;
let register_commands = std::env::var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true);
if register_commands {
let builder = poise::builtins::create_application_commands(&framework.options().commands);
let commands = serenity::Command::set_global_application_commands(&ctx.http, |commands| {
*commands = builder.clone();
commands
}).await;
match commands {
Ok(cmdmap) => {
for command in cmdmap.iter() {
println!("Registered command globally: {}", command.name);
}
},
Err(why) => println!("Error registering commands: {:?}", why)
}
}
Ok(())
}
#[tokio::main]
async fn main() {
let token = std::env::var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found");
let client = poise::Framework::builder().token(token)
.intents(serenity::GatewayIntents::MESSAGE_CONTENT | serenity::GatewayIntents::GUILDS)
.options(poise::FrameworkOptions {
commands: vec![
commands::ping::ping(),
commands::status::status()
],
pre_command: |ctx| Box::pin(async move {
println!("{} ran /{}", ctx.author().name, ctx.command().qualified_name)
}),
..Default::default()
}).setup(|ctx, ready, framework| Box::pin(on_ready(ctx, ready, framework)))
.build().await.expect("Error while building the client");
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}