diff --git a/Cargo.lock b/Cargo.lock index edc1fca..ca146da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,7 +35,7 @@ dependencies = [ [[package]] name = "appflow" version = "0.1.0" -source = "git+https://github.com/HadziqM/myrustlib.git#6fbec7af066338fd3aed3876af74238cb4463a2b" +source = "git+https://github.com/HadziqM/myrustlib.git#5e9dd1602a0c7f2875270508e01b3ec4bd040906" dependencies = [ "indexmap", "log", @@ -1089,7 +1089,7 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "logger" version = "0.1.0" -source = "git+https://github.com/HadziqM/myrustlib.git#6fbec7af066338fd3aed3876af74238cb4463a2b" +source = "git+https://github.com/HadziqM/myrustlib.git#5e9dd1602a0c7f2875270508e01b3ec4bd040906" dependencies = [ "chrono", "log", @@ -1102,7 +1102,7 @@ dependencies = [ [[package]] name = "macros" version = "0.1.0" -source = "git+https://github.com/HadziqM/myrustlib.git#6fbec7af066338fd3aed3876af74238cb4463a2b" +source = "git+https://github.com/HadziqM/myrustlib.git#5e9dd1602a0c7f2875270508e01b3ec4bd040906" dependencies = [ "darling", "proc-macro2", @@ -1370,6 +1370,7 @@ dependencies = [ "log", "logger", "serenity", + "thiserror 2.0.1", "tokio", ] @@ -1947,7 +1948,7 @@ dependencies = [ [[package]] name = "sysdir" version = "0.1.0" -source = "git+https://github.com/HadziqM/myrustlib.git#6fbec7af066338fd3aed3876af74238cb4463a2b" +source = "git+https://github.com/HadziqM/myrustlib.git#5e9dd1602a0c7f2875270508e01b3ec4bd040906" dependencies = [ "dirs", "log", @@ -2509,7 +2510,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6fb2d1a..625c6a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,7 @@ macros = { git = "https://github.com/HadziqM/myrustlib.git", subdir = "macros" } # async runtime tokio = { version = "1.32", features = ["full"] } -sqlx = { version = "0.8.2", features = [ - "postgres", - "chrono", - "runtime-tokio-native-tls", -] } +sqlx = { version = "0.8.2", features = ["postgres", "chrono"] } reqwest = { version = "0.12.9", features = ["json"] } # Image processing diff --git a/rain-bot/Cargo.toml b/rain-bot/Cargo.toml index 824c3ea..4e6c09d 100644 --- a/rain-bot/Cargo.toml +++ b/rain-bot/Cargo.toml @@ -9,5 +9,6 @@ serenity.workspace = true appflow.workspace = true log.workspace = true logger.workspace = true -common = { path = "../common" } lazy_static.workspace = true +thiserror.workspace = true +common = { path = "../common" } diff --git a/rain-bot/src/error.rs b/rain-bot/src/error.rs index c2e101a..3d2fb5b 100644 --- a/rain-bot/src/error.rs +++ b/rain-bot/src/error.rs @@ -1,3 +1,5 @@ +use thiserror::Error; + #[derive(Debug, Error)] pub enum MyError { #[error("Test")] diff --git a/rain-bot/src/setup.rs b/rain-bot/src/setup.rs index e1bc4a3..e44272f 100644 --- a/rain-bot/src/setup.rs +++ b/rain-bot/src/setup.rs @@ -1,11 +1,6 @@ use common::setting::SettingAll; -use lazy_static::lazy_static; use serenity::all::*; -use std::{ - collections::HashMap, - future::Future, - sync::{Arc, RwLock, RwLockReadGuard}, -}; +use std::{collections::HashMap, sync::Arc}; use tokio::sync::RwLock; use crate::error::MyError; @@ -16,55 +11,43 @@ pub struct App { pub setting: Arc<RwLock<SettingAll>>, } -pub struct DiscordHandler { +pub struct DiscordHandler<T: CommandInteractionTrait> { pub app: Arc<App>, - pub command_list: HashMap<String, InteractionHandler>, + pub command_list: HashMap<String, T>, } -pub struct InteractionHandler { - pub handle: Fn(App, CommandInteraction, Context) -> Future<Output = MyResult<()>>, - pub id: String, - pub command: CreateCommand, -} - -impl InteractionHandler { - async fn handle_command(&self, app: App, cmd: CommandInteraction, ctx: Context) { - if let Err(e) = self.handle(app, cmd, ctx).await { - // TODO: Proper Error Handling - log::error!("Getting error: {:#?}", e); +#[async_trait] +pub trait CommandInteractionTrait: Sync + Send + 'static { + async fn hand(&self, app: Arc<App>, cmd: CommandInteraction, ctx: Context) { + if let Err(e) = Self::handle(app, cmd, ctx).await { + // TODO: Proper error handling + log::error!("there is error {:?}", e); } } -} - -pub trait InteractionTrait { - fn id() -> String; + async fn handle(app: Arc<App>, cmd: CommandInteraction, ctx: Context) -> MyResult<()>; fn command() -> CreateCommand; - async fn handle(app: App, cmd: CommandInteraction, ctx: Context) -> MyResult<()>; - fn reg(&self) -> InteractionHandler { - InteractionHandler { - id: Self::id(), - handle: Self::handle, - command: Self::command(), - } - } + fn name() -> String; } -impl EventHandler for DiscordHandler { - async fn ready(&self, ctx: Context, data_about_bot: Ready) { +#[async_trait] +impl<T: CommandInteractionTrait> EventHandler for DiscordHandler<T> { + async fn ready(&self, _ctx: Context, _data_about_bot: Ready) { todo!() } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { match interaction { Interaction::Command(cmd) => { - if let Some(hnd) = self.command_list.get(&cmd.data.name) { - hnd.handle_command(self.app.clone(), cmd, ctx).await; + if let Some(x) = self.command_list.get(&cmd.data.name) { + x.hand(self.app.clone(), cmd, ctx).await; } } + Interaction::Component(_x) => { + todo!() + } _ => {} } - todo!() } - async fn message(&self, ctx: Context, new_message: Message) { + async fn message(&self, _ctx: Context, _new_message: Message) { todo!() } }