Update team command

This commit is contained in:
Em (Ethan) Ruszanowski 2023-01-13 18:38:45 -05:00
parent ca376be31c
commit f4ccf55906
No known key found for this signature in database
GPG key ID: C3E7A3C0B1491DFE
7 changed files with 273 additions and 32 deletions

223
Cargo.lock generated
View file

@ -127,6 +127,16 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
@ -280,12 +290,21 @@ name = "echbot"
version = "0.1.0"
dependencies = [
"chrono",
"itertools",
"md5",
"poise",
"rand",
"reqwest",
"serde",
"tokio",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "encoding_rs"
version = "0.8.31"
@ -295,6 +314,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "fastrand"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499"
dependencies = [
"instant",
]
[[package]]
name = "flate2"
version = "1.0.25"
@ -311,6 +339,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "form_urlencoded"
version = "1.1.0"
@ -511,6 +554,19 @@ dependencies = [
"tokio-rustls",
]
[[package]]
name = "hyper-tls"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"native-tls",
"tokio",
"tokio-native-tls",
]
[[package]]
name = "iana-time-zone"
version = "0.1.53"
@ -561,12 +617,30 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]]
name = "ipnet"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.5"
@ -582,6 +656,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.139"
@ -665,6 +745,24 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "native-tls"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
dependencies = [
"lazy_static",
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@ -700,6 +798,51 @@ version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
[[package]]
name = "openssl"
version = "0.10.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b102428fd03bc5edf97f62620f7298614c45cedf287c271e7ed450bbaf83f2e1"
dependencies = [
"bitflags",
"cfg-if",
"foreign-types",
"libc",
"once_cell",
"openssl-macros",
"openssl-sys",
]
[[package]]
name = "openssl-macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23bbbf7854cd45b83958ebe919f0e8e516793727652e27fda10a8384cfc790b7"
dependencies = [
"autocfg",
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "ordered-float"
version = "2.10.0"
@ -750,6 +893,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]]
name = "poise"
version = "0.5.2"
@ -859,6 +1008,15 @@ version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]]
name = "reqwest"
version = "0.11.13"
@ -875,11 +1033,13 @@ dependencies = [
"http-body",
"hyper",
"hyper-rustls",
"hyper-tls",
"ipnet",
"js-sys",
"log",
"mime",
"mime_guess",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
@ -889,6 +1049,7 @@ dependencies = [
"serde_json",
"serde_urlencoded",
"tokio",
"tokio-native-tls",
"tokio-rustls",
"tokio-util",
"tower-service",
@ -948,6 +1109,15 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]]
name = "schannel"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "713cfb06c7059f3588fb8044c0fad1d09e3c01d225e25b9220dbfdcf16dbb1b3"
dependencies = [
"windows-sys",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -970,6 +1140,29 @@ dependencies = [
"untrusted",
]
[[package]]
name = "security-framework"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bc1bb97804af6631813c55739f771071e0f2ed33ee20b68c86ec505d906356c"
dependencies = [
"bitflags",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "serde"
version = "1.0.152"
@ -1114,6 +1307,20 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
dependencies = [
"cfg-if",
"fastrand",
"libc",
"redox_syscall",
"remove_dir_all",
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.3"
@ -1225,6 +1432,16 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
dependencies = [
"native-tls",
"tokio",
]
[[package]]
name = "tokio-rustls"
version = "0.23.4"
@ -1388,6 +1605,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -11,3 +11,6 @@ tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
rand = "0.8.5"
chrono = "0.4.23"
md5 = "0.7.0"
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11.13", features = ["json"] }
itertools = "0.10.5"

View file

@ -1,27 +1,31 @@
use chrono;
use crate::Error;
use serde::{Serialize, Deserialize};
fn get_utc_timestamp() -> String {
return chrono::Utc::now().format("%Y%m%d%H%M%S").to_string();
#[derive(Debug, Serialize, Deserialize)]
struct Session {
ret_msg: String,
session_id: String,
timestamp: String,
}
fn generate_signature(timestamp: String) -> String {
let dev_id = std::env::var("DEV_ID")
pub(crate) async fn get_random_god() -> Result<(), Error> {
let timestamp = chrono::Utc::now().format("%Y%m%d%H%M%S").to_string();
let method = "createsession";
let dev_id: String = std::env::var("DEV_ID")
.expect("Missing DEV_ID");
let auth_key = std::env::var("AUTH_KEY")
.expect("Missing AUTH_KEY"); // Get Hi-Rez API credentials from env
let signature = md5::compute(format!("{}createsession{}{}", dev_id, auth_key, timestamp));
.expect("Missing AUTH_KEY");
return format!("{:?}", signature);
}
let hash = md5::compute(format!("{}{}{}{}", dev_id, method, auth_key, timestamp));
let signature = format!("{:x}", hash);
let request = format!("https://api.smitegame.com/smiteapi.svc/createsessionJson/{}/{}/{}", dev_id, signature, timestamp);
println!("{}", request);
let response = reqwest::get(&request).await?;
let sessions: Vec<Session> = response.json().await?;
println!("{:?}", sessions);
fn create_session() -> Result<(), Error> {
let timestamp = get_utc_timestamp();
let signature: String = generate_signature(timestamp);
println!("{}", signature);
Ok(())
}
pub(crate) fn get_random_god() -> &'static str {
return "some god";
}

View file

@ -15,9 +15,9 @@ pub(crate) async fn random(
pub(crate) async fn god(
ctx: Context<'_>,
) -> Result<(), Error> {
let god = get_random_god();
ctx.say(format!("{}", god)).await?;
let god = "some god";
get_random_god().await?;
// ctx.say(format!("{}", god)).await?;
Ok(())
}

View file

@ -23,7 +23,7 @@ pub(crate) async fn slur(
.embed(|f| f
.title("DMBrandon Sez:")
.description(format!("\"{}\"", quote))
.color(serenity::Colour::BLUE)
.color(serenity::Colour::GOLD)
)).await?; // Send embed with team picks
Ok(())
}

View file

@ -1,5 +1,13 @@
use std::collections::HashMap;
use crate::{Context, Error};
use crate::serenity;
use poise::serenity_prelude::{UserId, VoiceState};
use rand::seq::SliceRandom;
use std::string::String;
fn team_to_ping(team: &[&String]) -> String {
return team.iter().map(|o| format!("<@{}>", o)).collect::<Vec<String>>().join(", ");
}
/// Splits up players for custom matches
#[poise::command(slash_command)]
@ -10,10 +18,10 @@ pub(crate) async fn team(
#[description = "Team size"]
#[min = 1] size: u8,
) -> Result<(), Error> {
let mut v = ctx.guild().unwrap().voice_states; // Get hashmap of users' voice states within the guild
v.retain(|_, s| s.channel_id == Some(channel.id())); // Drop users not active in requested voice channel from hashmap
let mut v: HashMap<UserId, VoiceState> = ctx.guild().unwrap().voice_states; // Get hashmap of users' voice states within the guild
v.retain(|_, s: &mut VoiceState| s.channel_id == Some(channel.id())); // Drop users not active in requested voice channel from hashmap
if v.keys().len() < size as usize { // Make sure there are enough members in the voice channel
if v.keys().len() < size as usize * 2 { // Make sure there are enough members in the voice channel
ctx.send(|f| f
.embed(|f| f
.title(format!("Custom {}v{} Teams", size, size))
@ -21,13 +29,17 @@ pub(crate) async fn team(
.color(serenity::Colour::RED)
)).await?; // Insult the user for not having enough members in call
} else {
let users: Vec<String> = Vec::from_iter(v.keys().map(|u| u.to_string())); // Get vec of PIDs
let players: Vec<&String> = users.choose_multiple(
&mut rand::thread_rng(), size as usize * 2).collect(); // Pick players randomly into slice
let (order, chaos) = players.split_at(players.len() / 2); // Split slice into two teams
ctx.send(|f| f
.embed(|f| f
.title(format!("Custom {}v{} Teams", size, size))
.description("I'm not done with this yet.")
.field("Order", "Some names", true)
.field("Chaos", "Other names", true)
.field("Spectators", "You guessed it, names.", false)
.description("Good luck have fun.")
.field("Order", team_to_ping(order), false)
.field("Chaos", team_to_ping(chaos), false)
.color(serenity::Colour::DARK_GREEN)
)).await?; // Send embed with team picks
}

View file

@ -14,7 +14,6 @@ async fn main() {
commands: vec![
commands::slur::slur(),
commands::team::team(),
commands::random::random(),
], // IntelliJ doesn't like this, but it's fine.
..Default::default()
})