Skip to content

Commit

Permalink
Added InteractHttp Event
Browse files Browse the repository at this point in the history
  • Loading branch information
AS1100K committed Aug 19, 2024
1 parent c554e1d commit 467bdc6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - 0.3.0

### Added
- Added `InteractHttp` Event

### Fixes
- Removed Error when the channel is empty

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ license = "GPL-3.0-only"
keywords = ["bevy", "plugin", "discord"]

[features]
full = ["webhook", "bot"]
full = ["webhook", "bot", "http"]
webhook = ["dep:reqwest", "dep:serde", "dep:serde_json"]
bot = ["dep:serenity", "tokio/sync"]
http = []
bot_cache = ["serenity/cache"]

[dependencies]
Expand Down
38 changes: 38 additions & 0 deletions src/bot/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use core::future::Future;
use core::marker::Send;
use std::sync::Arc;
use bevy_ecs::prelude::*;
use serenity::all::*;
use crate::bot::DiscordBotRes;
use crate::runtime::tokio_runtime;

pub type HandlerFn = fn(http: &Arc<Http>, commands: &mut Commands) -> Fut;
pub type Fut = dyn Future<Output=dyn Send + 'static>;

#[derive(Event)]
pub struct InteractHttp
{
pub handler: HandlerFn
}

impl InteractHttp {
pub fn new(handler: HandlerFn) -> Self {
Self {
handler
}
}
}

fn handle_interact_http(
mut events: EventReader<InteractHttp>,
discord_bot_res: Res<DiscordBotRes>,
mut commands: Commands
) {
for InteractHttp { handler } in events.read() {
if let Some(http) = &discord_bot_res.http {
tokio_runtime().spawn(async move {
handler(http, &mut commands).await
});
}
}
}
13 changes: 11 additions & 2 deletions src/bot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::future::Future;
use std::sync::Arc;

use bevy_app::{App, Plugin, Startup, Update};
Expand All @@ -12,11 +13,15 @@ use events::*;
use crate::bot::handle::Handle;
use crate::runtime::tokio_runtime;
use crate::{initialize_field_with_doc, override_field_with_doc, DiscordSet};
use crate::bot::http::InteractHttp;

mod common;
mod event_handlers;
pub mod events;
mod handle;
pub mod events;
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
pub mod http;

/// Re-export serenity
pub mod serenity {
Expand All @@ -37,6 +42,9 @@ impl Plugin for DiscordBotPlugin {
#[cfg(feature = "bot_cache")]
app.add_event::<BCacheRead>().add_event::<BShardsReady>();

#[cfg(feature = "http")]
app.add_event::<InteractHttp>();

app.insert_resource(self.0.clone())
.add_event::<BReadyEvent>()
.add_event::<BCommandPermissionsUpdate>()
Expand Down Expand Up @@ -150,7 +158,8 @@ pub struct DiscordBotRes {
impl DiscordBotRes {
/// [Http] is available once [BReadyEvent] is triggered
///
/// NOTE: Calling this function can be expensive as it returns a clone of [Http]
/// NOTE: Calling this function can be expensive as it returns a clone of [Http], you
/// may want to use [http::InteractHttp]
///
/// ## Example
///
Expand Down

0 comments on commit 467bdc6

Please sign in to comment.