Skip to content

Commit

Permalink
Moved all resources to res module
Browse files Browse the repository at this point in the history
  • Loading branch information
AS1100K committed Feb 6, 2025
1 parent b04df35 commit 9ebce8c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 65 deletions.
1 change: 1 addition & 0 deletions PRERELEASE-CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Hide functions available in `docsrs` feature
- `plugins` module is now private
- disabled default features of dependencies
- Moved all resources to `res` module

### Fixed
- Typos in documentation
Expand Down
2 changes: 1 addition & 1 deletion src/bot/event_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy_ecs::prelude::*;

use crate::events::bot::*;
use crate::http::DiscordHttpResource;
use crate::res::DiscordHttpResource;

pub(crate) fn handle_b_ready_event(mut events: EventReader<BReadyEvent>, mut commands: Commands) {
for event in events.read() {
Expand Down
48 changes: 2 additions & 46 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//! .run();
//! ```
use crate::res::DiscordHttpResource;
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::Resource;
use serenity::http::Http;
use std::sync::Arc;

Expand Down Expand Up @@ -46,50 +46,6 @@ impl Plugin for DiscordHttpPlugin {
fn build(&self, app: &mut App) {
let http: Arc<Http> = Arc::new(Http::new(&self.0));

app.insert_resource(DiscordHttpResource { http });
}
}

/// A Bevy resource that provides access to the Discord HTTP client.
///
/// This resource is automatically inserted into the Bevy app when using
/// [`DiscordHttpPlugin`]. It wraps Serenity's [`Http`] client in an [`Arc`]
/// for safe concurrent access.
///
/// When using [`DiscordBotPlugin`](crate::bot::DiscordBotPlugin) this resource is
/// automatically inserted into the Bevy app once [BReadyEvent](crate::bot::events::BReadyEvent)
/// is emitted.
#[derive(Resource)]
pub struct DiscordHttpResource {
http: Arc<Http>,
}

impl DiscordHttpResource {
pub fn new(http: Arc<Http>) -> Self {
Self { http }
}

/// Returns a cloned Arc reference to the HTTP client.
///
/// Use this method when you need to share ownership of the client
/// across multiple systems or threads.
///
/// # Returns
///
/// An [`Arc`]<[`Http`]> instance that can be safely cloned and shared.
pub fn client(&self) -> Arc<Http> {
self.http.clone()
}

/// Returns a reference to the underlying HTTP client.
///
/// Use this method when you only need temporary access to the client
/// and don't need to share ownership.
///
/// # Returns
///
/// A reference to the [`Http`] client.
pub fn client_ref(&self) -> &Http {
&self.http
app.insert_resource(DiscordHttpResource::new(http));
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use bevy_ecs::schedule::SystemSet;
#[cfg_attr(docsrs, doc(cfg(feature = "bot")))]
pub mod bot;

pub mod res;

mod common;

#[cfg(feature = "http")]
Expand Down
119 changes: 119 additions & 0 deletions src/res.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//! Resources for managing Discord HTTP client and Rich Presence functionality.
//!
//! This module provides two main resources:
//! - `DiscordHttpResource`: For handling Discord HTTP client operations
//! - `DiscordRichPresenceRes`: For managing Discord Rich Presence integration
use bevy_ecs::prelude::*;
use std::sync::Arc;

/// A Bevy resource that provides access to the Discord HTTP client.
///
/// This resource is automatically inserted into the Bevy app when using
/// `DiscordHttpPlugin`. It wraps Serenity's `Http` client in an `Arc`
/// for safe concurrent access.
///
/// # Examples
///
/// ```rust,no_run
/// use bevy_discord::res::DiscordHttpResource;
/// use std::sync::Arc;
/// use serenity::all::Http;
///
/// let http = Arc::new(Http::new("token"));
/// let resource = DiscordHttpResource::new(http);
///
/// // Get a cloned Arc reference
/// let client = resource.client();
///
/// // Get a reference to the client
/// let client_ref = resource.client_ref();
/// ```
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[derive(Resource)]
pub struct DiscordHttpResource {
pub http: Arc<serenity::all::Http>,
}

#[cfg(feature = "http")]
impl DiscordHttpResource {
/// Creates a new `DiscordHttpResource` instance.
///
/// # Arguments
///
/// * `http` - An Arc-wrapped Serenity HTTP client
pub fn new(http: Arc<serenity::all::Http>) -> Self {
Self { http }
}

/// Returns a cloned Arc reference to the HTTP client.
///
/// Use this method when you need to share ownership of the client
/// across multiple systems or threads.
pub fn client(&self) -> Arc<serenity::all::Http> {
self.http.clone()
}

/// Returns a reference to the underlying HTTP client.
///
/// Use this method when you only need temporary access to the client
/// and don't need to share ownership.
pub fn client_ref(&self) -> &serenity::all::Http {
&self.http
}
}

/// A global resource for managing Discord Rich Presence functionality.
///
/// This resource maintains the bot's Rich Presence state and provides access
/// to the Discord SDK client for updating presence information.
///
/// # Examples
///
/// ```rust,no_run
/// use bevy_discord::res::DiscordRichPresenceRes;
/// use std::sync::Arc;
/// use discord_sdk::Discord;
///
/// let discord = Arc::new(Discord::new(/* ... */));
/// let resource = DiscordRichPresenceRes::new(discord);
///
/// // Get a cloned Arc reference
/// let rp = resource.get_rp();
///
/// // Get a reference to the Discord client
/// let rp_ref = resource.get_rp_ref();
/// ```
#[cfg(feature = "rich_presence")]
#[cfg_attr(docsrs, doc(cfg(feature = "rich_presence")))]
#[derive(Resource)]
pub struct DiscordRichPresenceRes {
pub discord: Arc<discord_sdk::Discord>,
}

#[cfg(feature = "rich_presence")]
impl DiscordRichPresenceRes {
/// Creates a new `DiscordRichPresenceRes` instance.
///
/// # Arguments
///
/// * `rp` - An Arc-wrapped Discord SDK client
pub fn new(rp: Arc<discord_sdk::Discord>) -> Self {
Self { discord: rp }
}

/// Returns a cloned Arc reference to the Discord SDK client.
///
/// Use this when you need to share ownership of the client.
pub fn get_rp(&self) -> Arc<discord_sdk::Discord> {
self.discord.clone()
}

/// Returns a reference to the Discord SDK client.
///
/// Use this when you only need temporary access to the client.
pub fn get_rp_ref(&self) -> &discord_sdk::Discord {
&self.discord
}
}
26 changes: 8 additions & 18 deletions src/rich_presence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ use bevy_ecs::prelude::*;
use discord_sdk::{Discord, Subscriptions};
use std::sync::Arc;

/// A global resource for the Discord bot.
///
/// This resource maintains the bot's internal state and event communication channel.
#[derive(Resource)]
pub struct DiscordRichPresenceRes {
pub discord: Arc<Discord>,
}

#[derive(Resource, Clone)]
pub struct DiscordRichPresenceConfig {
pub app: AppId,
Expand Down Expand Up @@ -72,16 +64,14 @@ fn setup_rich_presence(

let discord_rich_presence_config = discord_rich_presence_config.clone();
let discord_res = || {
commands.insert_resource(DiscordRichPresenceRes {
discord: Arc::new(
Discord::new(
discord_rich_presence_config.app,
discord_rich_presence_config.subscriptions,
event_handler,
)
.expect("Failed to create a Discord Rich Presence Client"),
),
});
commands.insert_resource(crate::res::DiscordRichPresenceRes::new(Arc::new(
Discord::new(
discord_rich_presence_config.app,
discord_rich_presence_config.subscriptions,
event_handler,
)
.expect("Failed to create a Discord Rich Presence Client"),
)))
};

tokio_runtime().block_on(async move { discord_res() });
Expand Down

0 comments on commit 9ebce8c

Please sign in to comment.