From 244e66cfa37a5edb737fd746619c834df1826693 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 18 Jun 2023 18:00:46 -0700 Subject: [PATCH] Create demo serenity/poise integration --- .env.example | 3 +- Cargo.lock | 400 ++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 27 +++- 4 files changed, 425 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 594bf3c..7bfdb7a 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,5 @@ CLIENT_ID= CLIENT_SECRET= DOMAIN=https://tegakituesday.com/ # head -c64 /dev/urandom | base64 -SECRET= \ No newline at end of file +SECRET= +DISCORD_TOKEN= diff --git a/Cargo.lock b/Cargo.lock index 87d9d54..0f55c92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,6 +158,22 @@ dependencies = [ "syn 2.0.18", ] +[[package]] +name = "async-tungstenite" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1b71b31561643aa8e7df3effe284fa83ab1a840e52294c5f4bd7bfd8b2becbb" +dependencies = [ + "futures-io", + "futures-util", + "log 0.4.18", + "pin-project-lite", + "tokio", + "tokio-rustls 0.23.4", + "tungstenite", + "webpki-roots", +] + [[package]] name = "atomic" version = "0.5.3" @@ -528,6 +544,66 @@ dependencies = [ "cipher", ] +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2 1.0.59", + "quote 1.0.28", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote 1.0.28", + "syn 1.0.109", +] + +[[package]] +name = "dashmap" +version = "5.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc" +dependencies = [ + "cfg-if 1.0.0", + "hashbrown", + "lock_api", + "once_cell", + "parking_lot_core", + "serde", +] + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2 1.0.59", + "quote 1.0.28", + "syn 1.0.109", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -836,6 +912,17 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2 1.0.59", + "quote 1.0.28", + "syn 2.0.18", +] + [[package]] name = "futures-sink" version = "0.3.28" @@ -857,6 +944,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -1076,7 +1164,7 @@ dependencies = [ "time 0.1.45", "traitobject", "typeable", - "unicase", + "unicase 1.4.2", "url 1.7.2", ] @@ -1104,6 +1192,19 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-rustls" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0646026eb1b3eea4cd9ba47912ea5ce9cc07713d105b1a14698f4e6433d348b7" +dependencies = [ + "http", + "hyper 0.14.26", + "rustls 0.21.2", + "tokio", + "tokio-rustls 0.24.1", +] + [[package]] name = "hyper-tls" version = "0.5.0" @@ -1140,6 +1241,12 @@ dependencies = [ "cc", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.1.5" @@ -1448,6 +1555,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime 0.3.17", + "unicase 2.6.0", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1532,7 +1649,7 @@ dependencies = [ "log 0.4.18", "memchr", "mime 0.3.17", - "spin", + "spin 0.9.8", "tokio", "tokio-util", "version_check 0.9.4", @@ -1781,6 +1898,15 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "ordered-float" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7940cf2ca942593318d07fcf2596cdca60a85c9e7fab408a5e21a4f9dcd40d87" +dependencies = [ + "num-traits", +] + [[package]] name = "overload" version = "0.1.1" @@ -1991,6 +2117,37 @@ dependencies = [ "time 0.3.21", ] +[[package]] +name = "poise" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d591af1c934c29adda172665f69b837e642d4fee85598baffb95dd98110467d" +dependencies = [ + "async-trait", + "derivative", + "futures-core", + "futures-util", + "log 0.4.18", + "once_cell", + "parking_lot", + "poise_macros", + "regex", + "serenity", + "tokio", +] + +[[package]] +name = "poise_macros" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40270099e1527efae99fdc0609d397e76310b529d4980ad38ab14d81803ca0fa" +dependencies = [ + "darling", + "proc-macro2 1.0.59", + "quote 1.0.28", + "syn 1.0.109", +] + [[package]] name = "polyval" version = "0.6.0" @@ -2182,28 +2339,51 @@ dependencies = [ "http", "http-body", "hyper 0.14.26", + "hyper-rustls", "hyper-tls", "ipnet", "js-sys", "log 0.4.18", "mime 0.3.17", + "mime_guess", "native-tls", "once_cell", "percent-encoding 2.2.0", "pin-project-lite", + "rustls 0.21.2", + "rustls-pemfile", "serde", "serde_json", "serde_urlencoded", "tokio", "tokio-native-tls", + "tokio-rustls 0.24.1", + "tokio-util", "tower-service", "url 2.3.1", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", + "webpki-roots", "winreg", ] +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin 0.5.2", + "untrusted", + "web-sys", + "winapi 0.3.9", +] + [[package]] name = "rocket" version = "0.4.11" @@ -2405,6 +2585,49 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "rustls" +version = "0.20.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f" +dependencies = [ + "log 0.4.18", + "ring", + "sct", + "webpki", +] + +[[package]] +name = "rustls" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e32ca28af694bc1bbf399c33a516dbdf1c90090b8ab23c2bc24f834aa2247f5f" +dependencies = [ + "log 0.4.18", + "ring", + "rustls-webpki", + "sct", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d194b56d58803a43635bdc398cd17e383d6f71f9182b9a192c127ca42494a59b" +dependencies = [ + "base64 0.21.2", +] + +[[package]] +name = "rustls-webpki" +version = "0.100.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6207cd5ed3d8dca7816f8f3725513a34609c0c765bf652b8c3cb4cfd87db46b" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "rustversion" version = "1.0.12" @@ -2466,6 +2689,16 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sct" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "security-framework" version = "2.9.1" @@ -2504,6 +2737,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-value" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" +dependencies = [ + "ordered-float", + "serde", +] + [[package]] name = "serde_derive" version = "1.0.163" @@ -2560,6 +2803,49 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "serenity" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82fd5e7b5858ad96e99d440138f34f5b98e1b959ebcd3a1036203b30e78eb788" +dependencies = [ + "async-trait", + "async-tungstenite", + "base64 0.13.1", + "bitflags 1.3.2", + "bytes", + "cfg-if 1.0.0", + "chrono", + "dashmap", + "flate2", + "futures", + "mime 0.3.17", + "mime_guess", + "parking_lot", + "percent-encoding 2.2.0", + "reqwest", + "rustversion", + "serde", + "serde-value", + "serde_json", + "time 0.3.21", + "tokio", + "tracing", + "typemap_rev", + "url 2.3.1", +] + +[[package]] +name = "sha-1" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5058ada175748e33390e40e872bd0fe59a19f265d0158daa551c5a88a76009c" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest", +] + [[package]] name = "sha2" version = "0.10.6" @@ -2635,6 +2921,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "spin" version = "0.9.8" @@ -2742,6 +3034,7 @@ dependencies = [ "comrak", "derive_more", "dotenv", + "poise", "reqwest", "rocket 0.5.0-rc.3", "rocket_contrib", @@ -2919,6 +3212,27 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-rustls" +version = "0.23.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59" +dependencies = [ + "rustls 0.20.8", + "tokio", + "webpki", +] + +[[package]] +name = "tokio-rustls" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" +dependencies = [ + "rustls 0.21.2", + "tokio", +] + [[package]] name = "tokio-stream" version = "0.1.14" @@ -3000,6 +3314,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if 1.0.0", + "log 0.4.18", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -3067,6 +3382,27 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" +[[package]] +name = "tungstenite" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e27992fd6a8c29ee7eef28fc78349aa244134e10ad447ce3b9f0ac0ed0fa4ce0" +dependencies = [ + "base64 0.13.1", + "byteorder", + "bytes", + "http", + "httparse", + "log 0.4.18", + "rand", + "rustls 0.20.8", + "sha-1", + "thiserror", + "url 2.3.1", + "utf-8", + "webpki", +] + [[package]] name = "typeable" version = "0.1.2" @@ -3079,6 +3415,12 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" +[[package]] +name = "typemap_rev" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5b74f0a24b5454580a79abb6994393b09adf0ab8070f15827cb666255de155" + [[package]] name = "typenum" version = "1.16.0" @@ -3169,6 +3511,15 @@ dependencies = [ "version_check 0.1.5", ] +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check 0.9.4", +] + [[package]] name = "unicode-bidi" version = "0.3.13" @@ -3224,6 +3575,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + [[package]] name = "url" version = "1.7.2" @@ -3244,8 +3601,15 @@ dependencies = [ "form_urlencoded", "idna 0.3.0", "percent-encoding 2.2.0", + "serde", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8parse" version = "0.2.1" @@ -3374,6 +3738,19 @@ version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed9d5b4305409d1fc9482fee2d7f9bcbf24b3972bf59817ef757e23982242a93" +[[package]] +name = "wasm-streams" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bbae3363c08332cadccd13b67db371814cd214c2524020932f0804b8cf7c078" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "web-sys" version = "0.3.63" @@ -3384,6 +3761,25 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "webpki-roots" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" +dependencies = [ + "webpki", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index f3ee4f3..a39a7de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ chrono = { version = "0.4.26", features = ["serde"] } comrak = "0.18.0" derive_more = "0.99.17" dotenv = "0.15.0" +poise = "0.5.5" reqwest = "0.11.18" rocket = { version = "=0.5.0-rc.3", features = ["secrets"] } rocket_contrib = { version = "0.4.11", features = ["templates"] } diff --git a/src/main.rs b/src/main.rs index 38915e5..6d45625 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,9 @@ extern crate rocket; use chrono::{Duration, Utc}; +use poise::serenity_prelude::{GatewayIntents, Client, Http}; use core::panic; +use std::ops::Deref; use derive_more::From; use reqwest::StatusCode; use rocket::form::Form; @@ -10,7 +12,7 @@ use rocket::fs::{relative, FileServer}; use rocket::http::{Cookie, CookieJar}; use rocket::request::FromRequest; use rocket::response::{content::RawHtml, Redirect}; -use rocket::{request, Request}; +use rocket::{request, Request, State}; use rocket_dyn_templates::{context, Template}; use sass_rocket_fairing::SassFairing; use serde::ser::SerializeStruct; @@ -173,6 +175,20 @@ fn logout(cookies: &CookieJar<'_>, referer: Referer) -> Redirect { Redirect::to(redirect_url) } +#[get("/testing")] +async fn testing(cookies: &CookieJar<'_>, http: &State) -> String { + // Get logged in user's join date in 字ちゃん server + format!("{:?}", http.get_guild(814700630958276649).await + .expect("Failed to get testing guild") + .member(http.deref(), User::get(cookies).await + .expect("Failed to get logged in user data") + .expect("No logged in user") + .id + ).await + .expect("Failed to fetch user in server") + .joined_at) +} + #[derive(Default, Deserialize)] struct User { #[serde(deserialize_with = "deserialize_id")] @@ -308,15 +324,20 @@ impl User { } #[launch] -fn rocket() -> _ { +async fn rocket() -> _ { dotenv::dotenv().expect("Failed to load .env file"); + + let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); + let http = Http::new(&token); + let config = rocket::Config::figment() .merge(("port", 1313)) .merge(("secret_key", env::var("SECRET").expect("Must have SECRET defined in .env for private cookie persistence"))); rocket::custom(config) + .manage(http) .mount( "/", - routes![get_challenge, login, post_login, success, logout], + routes![get_challenge, login, post_login, success, logout, testing], ) .mount("/css", FileServer::from(relative!("styles/css"))) .attach(Template::fairing())