Skip to content

Commit

Permalink
objectify async fn are too hard for safety, use trait instead
Browse files Browse the repository at this point in the history
  • Loading branch information
HadziqM committed Nov 9, 2024
1 parent 76eaeaa commit 3134e5b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 48 deletions.
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion rain-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 2 additions & 0 deletions rain-bot/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum MyError {
#[error("Test")]
Expand Down
57 changes: 20 additions & 37 deletions rain-bot/src/setup.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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!()
}
}

0 comments on commit 3134e5b

Please sign in to comment.