Skip to content

Commit

Permalink
Replace CacheHttp with &Http in more methods (serenity-rs#2818)
Browse files Browse the repository at this point in the history
This is possible now more methods are cache-less.
  • Loading branch information
GnomedDev committed Jun 9, 2024
1 parent a818c36 commit 998ebae
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 60 deletions.
2 changes: 1 addition & 1 deletion examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async fn message(ctx: &Context, msg: &Message) -> Result<(), serenity::Error> {
let guild = guild_id.to_guild_cached(&ctx.cache).unwrap().clone();
let perms = guild.user_permissions_in(
&channel_id.to_channel(ctx).await?.guild().unwrap(),
&*guild.member(ctx, msg.author.id).await?,
&*guild.member(&ctx.http, msg.author.id).await?,
);
channel_id.say(ctx, format!("{:?}", perms)).await?;
} else if let Some(forum_channel_id) = msg.content.strip_prefix("createforumpostin ") {
Expand Down
10 changes: 3 additions & 7 deletions src/builder/create_stage_instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use crate::http::CacheHttp;
use crate::http::Http;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
Expand Down Expand Up @@ -59,12 +59,8 @@ impl<'a> CreateStageInstance<'a> {
///
/// Returns [`Error::Http`] if there is already a stage instance currently.
#[cfg(feature = "http")]
pub async fn execute(
mut self,
cache_http: impl CacheHttp,
channel_id: ChannelId,
) -> Result<StageInstance> {
pub async fn execute(mut self, http: &Http, channel_id: ChannelId) -> Result<StageInstance> {
self.channel_id = Some(channel_id);
cache_http.http().create_stage_instance(&self, self.audit_log_reason).await
http.create_stage_instance(&self, self.audit_log_reason).await
}
}
10 changes: 3 additions & 7 deletions src/builder/create_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;

use super::CreateAttachment;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
use crate::http::Http;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
#[cfg(feature = "http")]
Expand Down Expand Up @@ -63,14 +63,10 @@ impl<'a> CreateWebhook<'a> {
/// [`Text`]: ChannelType::Text
/// [`News`]: ChannelType::News
#[cfg(feature = "http")]
pub async fn execute(
self,
cache_http: impl CacheHttp,
channel_id: ChannelId,
) -> Result<Webhook> {
pub async fn execute(self, http: &Http, channel_id: ChannelId) -> Result<Webhook> {
crate::model::error::Minimum::WebhookName.check_underflow(self.name.chars().count())?;
crate::model::error::Maximum::WebhookName.check_overflow(self.name.chars().count())?;

cache_http.http().create_webhook(channel_id, &self, self.audit_log_reason).await
http.create_webhook(channel_id, &self, self.audit_log_reason).await
}
}
10 changes: 3 additions & 7 deletions src/builder/edit_stage_instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use crate::http::CacheHttp;
use crate::http::Http;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
Expand Down Expand Up @@ -52,11 +52,7 @@ impl<'a> EditStageInstance<'a> {
/// Returns [`Error::Http`] if the channel is not a stage channel, or there is no stage
/// instance currently.
#[cfg(feature = "http")]
pub async fn execute(
self,
cache_http: impl CacheHttp,
channel_id: ChannelId,
) -> Result<StageInstance> {
cache_http.http().edit_stage_instance(channel_id, &self, self.audit_log_reason).await
pub async fn execute(self, http: &Http, channel_id: ChannelId) -> Result<StageInstance> {
http.edit_stage_instance(channel_id, &self, self.audit_log_reason).await
}
}
8 changes: 4 additions & 4 deletions src/builder/edit_voice_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "http")]
use crate::http::CacheHttp;
use crate::http::Http;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
Expand Down Expand Up @@ -75,16 +75,16 @@ impl EditVoiceState {
#[cfg(feature = "http")]
pub async fn execute(
mut self,
cache_http: impl CacheHttp,
http: &Http,
guild_id: GuildId,
channel_id: ChannelId,
user_id: Option<UserId>,
) -> Result<()> {
self.channel_id = Some(channel_id);
if let Some(user_id) = user_id {
cache_http.http().edit_voice_state(guild_id, user_id, &self).await
http.edit_voice_state(guild_id, user_id, &self).await
} else {
cache_http.http().edit_voice_state_me(guild_id, &self).await
http.edit_voice_state_me(guild_id, &self).await
}
}
}
16 changes: 6 additions & 10 deletions src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,8 @@ impl ChannelId {
/// # Errors
///
/// See [`CreateWebhook::execute`] for a detailed list of possible errors.
pub async fn create_webhook(
self,
cache_http: impl CacheHttp,
builder: CreateWebhook<'_>,
) -> Result<Webhook> {
builder.execute(cache_http, self).await
pub async fn create_webhook(self, http: &Http, builder: CreateWebhook<'_>) -> Result<Webhook> {
builder.execute(http, self).await
}

/// Returns a builder which can be awaited to obtain a message or stream of messages in this
Expand Down Expand Up @@ -806,10 +802,10 @@ impl ChannelId {
/// Returns [`Error::Http`] if there is already a stage instance currently.
pub async fn create_stage_instance(
self,
cache_http: impl CacheHttp,
http: &Http,
builder: CreateStageInstance<'_>,
) -> Result<StageInstance> {
builder.execute(cache_http, self).await
builder.execute(http, self).await
}

/// Edits the stage instance
Expand All @@ -822,10 +818,10 @@ impl ChannelId {
/// instance currently.
pub async fn edit_stage_instance(
self,
cache_http: impl CacheHttp,
http: &Http,
builder: EditStageInstance<'_>,
) -> Result<StageInstance> {
builder.execute(cache_http, self).await
builder.execute(http, self).await
}

/// Edits a thread.
Expand Down
28 changes: 10 additions & 18 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,15 +470,15 @@ impl GuildChannel {
/// [Mute Members]: Permissions::MUTE_MEMBERS
pub async fn edit_voice_state(
&self,
cache_http: impl CacheHttp,
http: &Http,
user_id: UserId,
builder: EditVoiceState,
) -> Result<()> {
if self.kind != ChannelType::Stage {
return Err(Error::from(ModelError::InvalidChannelType));
}

builder.execute(cache_http, self.guild_id, self.id, Some(user_id)).await
builder.execute(http, self.guild_id, self.id, Some(user_id)).await
}

/// Edits the current user's voice state in a stage channel.
Expand Down Expand Up @@ -527,12 +527,8 @@ impl GuildChannel {
///
/// [Request to Speak]: Permissions::REQUEST_TO_SPEAK
/// [Mute Members]: Permissions::MUTE_MEMBERS
pub async fn edit_own_voice_state(
&self,
cache_http: impl CacheHttp,
builder: EditVoiceState,
) -> Result<()> {
builder.execute(cache_http, self.guild_id, self.id, None).await
pub async fn edit_own_voice_state(&self, http: &Http, builder: EditVoiceState) -> Result<()> {
builder.execute(http, self.guild_id, self.id, None).await
}

/// Follows the News Channel
Expand Down Expand Up @@ -903,18 +899,14 @@ impl GuildChannel {
///
/// See [`CreateWebhook::execute`] for a detailed list of other
/// possible errors,
pub async fn create_webhook(
&self,
cache_http: impl CacheHttp,
builder: CreateWebhook<'_>,
) -> Result<Webhook> {
pub async fn create_webhook(&self, http: &Http, builder: CreateWebhook<'_>) -> Result<Webhook> {
// forum channels are not text-based, but webhooks can be created in them
// and used to send messages in their posts
if !self.is_text_based() && self.kind != ChannelType::Forum {
return Err(Error::Model(ModelError::InvalidChannelType));
}

self.id.create_webhook(cache_http, builder).await
self.id.create_webhook(http, builder).await
}

/// Gets a stage instance.
Expand All @@ -941,14 +933,14 @@ impl GuildChannel {
/// Returns [`Error::Http`] if there is already a stage instance currently.
pub async fn create_stage_instance(
&self,
cache_http: impl CacheHttp,
http: &Http,
builder: CreateStageInstance<'_>,
) -> Result<StageInstance> {
if self.kind != ChannelType::Stage {
return Err(Error::Model(ModelError::InvalidChannelType));
}

self.id.create_stage_instance(cache_http, builder).await
self.id.create_stage_instance(http, builder).await
}

/// Edits the stage instance
Expand All @@ -961,14 +953,14 @@ impl GuildChannel {
/// instance currently.
pub async fn edit_stage_instance(
&self,
cache_http: impl CacheHttp,
http: &Http,
builder: EditStageInstance<'_>,
) -> Result<StageInstance> {
if self.kind != ChannelType::Stage {
return Err(Error::Model(ModelError::InvalidChannelType));
}

self.id.edit_stage_instance(cache_http, builder).await
self.id.edit_stage_instance(http, builder).await
}

/// Deletes a stage instance.
Expand Down
8 changes: 2 additions & 6 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,15 +1478,11 @@ impl Guild {
///
/// Returns an [`Error::Http`] if the user is not in the guild or if the guild is otherwise
/// unavailable.
pub async fn member(
&self,
cache_http: impl CacheHttp,
user_id: UserId,
) -> Result<Cow<'_, Member>> {
pub async fn member(&self, http: &Http, user_id: UserId) -> Result<Cow<'_, Member>> {
if let Some(member) = self.members.get(&user_id) {
Ok(Cow::Borrowed(member))
} else {
cache_http.http().get_member(self.id, user_id).await.map(Cow::Owned)
http.get_member(self.id, user_id).await.map(Cow::Owned)
}
}

Expand Down

0 comments on commit 998ebae

Please sign in to comment.