From a54d14783a82544665fd345f44e16c7c6c9dc650 Mon Sep 17 00:00:00 2001 From: Maxime Leriche Date: Wed, 17 Jan 2024 22:22:42 +0000 Subject: [PATCH] feat: extract the start bot part and init work on the framework --- Cargo.lock | 34 ++++++++++++++++++ Cargo.toml | 2 ++ src/bot/handler.rs | 26 ++++++++++++++ src/bot/init.rs | 49 ++++++++++++++++++++++++++ src/bot/mod.rs | 2 ++ src/lib.rs | 3 +- src/main.rs | 88 +++------------------------------------------- 7 files changed, 120 insertions(+), 84 deletions(-) create mode 100644 src/bot/handler.rs create mode 100644 src/bot/init.rs create mode 100644 src/bot/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 0c1f414..7ab53dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -472,6 +472,17 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "command_attr" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32f08c85a02e066b7b4f7dcb60eee6ae0793ef7d6452a3547d1f19665df070a9" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -1073,6 +1084,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "levenshtein" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760" + [[package]] name = "libc" version = "0.2.152" @@ -1840,20 +1857,25 @@ dependencies = [ "base64", "bitflags 2.4.1", "bytes", + "command_attr", "flate2", "futures", + "levenshtein", "mime_guess", + "parking_lot", "percent-encoding", "reqwest", "secrecy", "serde", "serde_json", + "static_assertions", "time", "tokio", "tokio-tungstenite", "tracing", "typemap_rev", "url", + "uwl", ] [[package]] @@ -1958,6 +1980,12 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "stringprep" version = "0.1.4" @@ -2428,6 +2456,12 @@ dependencies = [ "serde", ] +[[package]] +name = "uwl" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4bf03e0ca70d626ecc4ba6b0763b934b6f2976e8c744088bb3c1d646fbb1ad0" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index 32d13db..8876b1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,8 @@ serenity = { version = "0.12", default-features = false, features = [ "gateway", "rustls_backend", "model", + "framework", + "standard_framework", ] } tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] } diff --git a/src/bot/handler.rs b/src/bot/handler.rs new file mode 100644 index 0000000..8453d69 --- /dev/null +++ b/src/bot/handler.rs @@ -0,0 +1,26 @@ +use serenity::{ + all::{Message, Ready}, + async_trait, + client::{Context, EventHandler}, +}; + +pub 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" { + 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); + } +} diff --git a/src/bot/init.rs b/src/bot/init.rs new file mode 100644 index 0000000..d08075d --- /dev/null +++ b/src/bot/init.rs @@ -0,0 +1,49 @@ +use super::handler::Handler; +use crate::config::Config; +use serenity::{all::GatewayIntents, http::Http, Client}; +use std::collections::HashSet; +use tokio::task::spawn_blocking; + +pub fn start_bot(config: Config) { + if config.token != "" { + spawn_blocking(move || { + let rt = tokio::runtime::Handle::current(); + rt.block_on(async move { + 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; + }) + }); + } +} diff --git a/src/bot/mod.rs b/src/bot/mod.rs new file mode 100644 index 0000000..abbb294 --- /dev/null +++ b/src/bot/mod.rs @@ -0,0 +1,2 @@ +pub mod handler; +pub mod init; diff --git a/src/lib.rs b/src/lib.rs index a105933..5a9b66e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ -pub mod config; \ No newline at end of file +pub mod config; +pub mod bot; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 33722bf..e100a47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,88 +1,10 @@ -use std::collections::HashSet; +mod bot; +mod config; use actix_cors::Cors; -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"); - } -} +use actix_web::{App, HttpServer}; +use bot::init::start_bot; +use config::parse_local_config; #[actix_web::main] // or #[tokio::main] async fn main() -> std::io::Result<()> {