From 4b4691690e7afa99964864ff3cfac7a70f4f7a3f Mon Sep 17 00:00:00 2001 From: max Date: Tue, 16 Jan 2024 23:44:41 +0000 Subject: [PATCH] feat: mise en place api ++ bot discord --- Cargo.lock | 240 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 +- README.md | 2 +- resources/config.toml | 2 + resources/test_config.toml | 2 + src/config.rs | 7 +- src/main.rs | 88 +++++++++++++- 7 files changed, 344 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42240a0..0c1f414 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -299,6 +299,15 @@ dependencies = [ "libc", ] +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +dependencies = [ + "serde", +] + [[package]] name = "async-trait" version = "0.1.77" @@ -374,7 +383,9 @@ dependencies = [ "reqwest", "serde", "serde_json", + "serenity", "serial_test", + "tokio", "tokio-postgres", "toml", "utoipa", @@ -550,6 +561,12 @@ dependencies = [ "parking_lot_core", ] +[[package]] +name = "data-encoding" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" + [[package]] name = "deadpool" version = "0.10.0" @@ -592,6 +609,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ "powerfmt", + "serde", ] [[package]] @@ -942,6 +960,20 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-rustls" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" +dependencies = [ + "futures-util", + "http", + "hyper", + "rustls", + "tokio", + "tokio-rustls", +] + [[package]] name = "hyper-tls" version = "0.5.0" @@ -1527,29 +1559,51 @@ dependencies = [ "http", "http-body", "hyper", + "hyper-rustls", "hyper-tls", "ipnet", "js-sys", "log", "mime", + "mime_guess", "native-tls", "once_cell", "percent-encoding", "pin-project-lite", + "rustls", + "rustls-pemfile", "serde", "serde_json", "serde_urlencoded", "system-configuration", "tokio", "tokio-native-tls", + "tokio-rustls", + "tokio-util", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", + "webpki-roots", "winreg", ] +[[package]] +name = "ring" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74" +dependencies = [ + "cc", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.48.0", +] + [[package]] name = "rust-embed" version = "8.2.0" @@ -1613,6 +1667,37 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustls" +version = "0.21.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" +dependencies = [ + "log", + "ring", + "rustls-webpki", + "sct", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64", +] + +[[package]] +name = "rustls-webpki" +version = "0.101.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "ryu" version = "1.0.16" @@ -1643,6 +1728,26 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "sct" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "secrecy" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e" +dependencies = [ + "serde", + "zeroize", +] + [[package]] name = "security-framework" version = "2.9.2" @@ -1724,6 +1829,33 @@ dependencies = [ "serde", ] +[[package]] +name = "serenity" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "385647faa24a889929028973650a4f158fb1b4272b2fcf94feb9fcc3c009e813" +dependencies = [ + "arrayvec", + "async-trait", + "base64", + "bitflags 2.4.1", + "bytes", + "flate2", + "futures", + "mime_guess", + "percent-encoding", + "reqwest", + "secrecy", + "serde", + "serde_json", + "time", + "tokio", + "tokio-tungstenite", + "tracing", + "typemap_rev", + "url", +] + [[package]] name = "serial_test" version = "2.0.0" @@ -1820,6 +1952,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + [[package]] name = "stringprep" version = "0.1.4" @@ -1967,13 +2105,26 @@ dependencies = [ "bytes", "libc", "mio", + "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", + "tokio-macros", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "tokio-native-tls" version = "0.3.1" @@ -2022,6 +2173,31 @@ dependencies = [ "whoami", ] +[[package]] +name = "tokio-rustls" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212d5dcb2a1ce06d81107c3d0ffa3121fe974b73f068c8282cb1c32328113b6c" +dependencies = [ + "futures-util", + "log", + "rustls", + "tokio", + "tokio-rustls", + "tungstenite", + "webpki-roots", +] + [[package]] name = "tokio-util" version = "0.7.10" @@ -2114,6 +2290,32 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tungstenite" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9" +dependencies = [ + "byteorder", + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand", + "rustls", + "sha1", + "thiserror", + "url", + "utf-8", +] + +[[package]] +name = "typemap_rev" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74b08b0c1257381af16a5c3605254d529d3e7e109f3c62befc5d168968192998" + [[package]] name = "typenum" version = "1.17.0" @@ -2150,6 +2352,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "url" version = "2.5.0" @@ -2159,8 +2367,15 @@ dependencies = [ "form_urlencoded", "idna", "percent-encoding", + "serde", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utoipa" version = "4.2.0" @@ -2316,6 +2531,19 @@ version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" +[[package]] +name = "wasm-streams" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4609d447824375f43e1ffbc051b50ad8f4b3ae8219680c94452ea05eb240ac7" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "web-sys" version = "0.3.67" @@ -2326,6 +2554,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-roots" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1778a42e8b3b90bff8d0f5032bf22250792889a5cdc752aa0020c84abe3aaf10" + [[package]] name = "whoami" version = "1.4.1" @@ -2547,6 +2781,12 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "zeroize" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" + [[package]] name = "zip" version = "0.6.6" diff --git a/Cargo.toml b/Cargo.toml index d674a81..32d13db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,9 +30,16 @@ dotenvy = "0.15.7" actix = "0.13.1" serial_test = "2.0.0" +serenity = { version = "0.12", default-features = false, features = [ + "client", + "gateway", + "rustls_backend", + "model", +] } +tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] } + [[bin]] name = "botdiscord" path = "src/main.rs" test = true - diff --git a/README.md b/README.md index 46d00be..48e240d 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Main lib used : [serenity-rs/serenity](https://github.com/serenity-rs/serenity) ## previous project -- [TestDiscord](https://git.weebo.fr/sandbox/TestDiscord/src/branch/master ): Bot that check if a world match with one in the image warehouse and answer with a picture +- [TestDiscord](https://git.weebo.fr/sandbox/TestDiscord): Bot that check if a world match with one in the image warehouse and answer with a picture - [UnlabeledBot](https://git.weebo.fr/sandbox/UnlabeledBot) : Soundboard project ## Database diff --git a/resources/config.toml b/resources/config.toml index 491239e..bc097f9 100644 --- a/resources/config.toml +++ b/resources/config.toml @@ -1,6 +1,8 @@ bot_name = "AGAU" env = "dev-che" port = 5437 +token = "" +prefix = "!" [persistence] host = "localhost" diff --git a/resources/test_config.toml b/resources/test_config.toml index 250dc21..143519f 100644 --- a/resources/test_config.toml +++ b/resources/test_config.toml @@ -1,6 +1,8 @@ bot_name = "AGAU_DEV" env = "test" port = 5437 +token = "" +prefix= "&" [persistence] host = "localhost" diff --git a/src/config.rs b/src/config.rs index 2175cc8..664edf7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,6 @@ use std::env; use std::fs::read_to_string; use std::path::PathBuf; - const PERSISTENCE_HOST: &str = "PERSISTENCE_HOST"; const PERSISTENCE_PORT: &str = "PERSISTENCE_PORT"; const PERSISTENCE_USER: &str = "PERSISTENCE_USER"; @@ -14,6 +13,8 @@ const PERSISTENCE_TLS: &str = "PERSISTENCE_TLS"; const PERSISTENCE_TLS_INSECURE: &str = "PERSISTENCE_TLS_INSECURE"; const BOT_NAME: &str = "BOT_NAME"; +const BOT_TOKEN: &str = "BOT_TOKEN"; +const BOT_PREFIX: &str = "BOT_PREFIX"; const RUST_ENV: &str = "RUST_ENV"; @@ -25,6 +26,8 @@ pub struct Config { pub persistence: PersistenceConfig, pub env: String, pub port: u16, + pub token: String, + pub prefix: String, } #[derive(Deserialize, Clone)] @@ -72,6 +75,8 @@ fn override_config_with_env_vars(config: Config) -> Config { Config { bot_name: env::var(BOT_NAME).unwrap_or(config.bot_name), env: env::var(RUST_ENV).unwrap_or(config.env), + token: env::var(BOT_TOKEN).unwrap_or(config.token), + prefix: env::var(BOT_PREFIX).unwrap_or(config.prefix), port: env::var(PORT) .unwrap_or(config.port.to_string()) .parse::() diff --git a/src/main.rs b/src/main.rs index a1a1764..33722bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,94 @@ +use std::collections::HashSet; + use actix_cors::Cors; -use actix_web::{App, HttpServer}; -use botdiscord::config::parse_local_config; +use actix_web::{rt::task::spawn_blocking, App, HttpServer}; +use botdiscord::config::{parse_local_config, Config}; +use serenity::{ + async_trait, + client::{Context, EventHandler}, + http::Http, + model::{ + channel::Message, + gateway::{GatewayIntents, Ready}, + }, + Client, +}; + +struct Handler; + +#[async_trait] +impl EventHandler for Handler { + async fn message(&self, ctx: Context, msg: Message) { + if msg.author.bot { + return; + } + if msg.content == "!ping" { + // Sending a message can fail, due to a network error, an + // authentication error, or lack of permissions to post in the + // channel, so log to stdout when some error happens, with a + // description of it. + if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await { + println!("Error sending message: {:?}", why); + } + return; + } + } + + async fn ready(&self, _: Context, ready: Ready) { + println!("{} is connected!", ready.user.name); + } +} + +fn start_bot(config: Config) { + if config.token != "" { + spawn_blocking(move || { + let rt = tokio::runtime::Handle::current(); + rt.block_on(async { + let local = tokio::task::LocalSet::new(); + let _ = local + .run_until(async move { + let intents = GatewayIntents::GUILD_MESSAGES + | GatewayIntents::DIRECT_MESSAGES + | GatewayIntents::MESSAGE_CONTENT + | GatewayIntents::GUILD_VOICE_STATES + | GatewayIntents::GUILDS + | GatewayIntents::GUILD_MEMBERS; + let http = Http::new(&config.token); + let (_owners, _bot_id) = match http.get_current_application_info().await { + Ok(info) => { + let mut owners = HashSet::new(); + match info.owner { + Some(user) => { + owners.insert(user.id); + println!("{}", user.global_name.unwrap()) + } + None => {} + } + + (owners, info.id) + } + Err(why) => panic!("Could not access application info: {:?}", why), + }; + let mut client = Client::builder(&config.token, intents) + .event_handler(Handler) + .await + .expect("Err creating client"); + if let Err(why) = client.start().await { + println!("Client error: {why:?}"); + } + }) + .await; + }) + }); + println!("JOJO 8"); + } +} #[actix_web::main] // or #[tokio::main] async fn main() -> std::io::Result<()> { let config = parse_local_config(); let port = config.port; + start_bot(config.clone()); HttpServer::new(|| { let cors = Cors::default() .allow_any_header() @@ -16,6 +99,5 @@ async fn main() -> std::io::Result<()> { .bind(("0.0.0.0", port))? .run() .await?; - Ok(()) }