From 71550cb1ff4664c99e794d9c24d0f7e847cdb75e Mon Sep 17 00:00:00 2001 From: Gnome! Date: Thu, 28 Mar 2024 13:04:26 +0000 Subject: [PATCH] Replace `CacheHttp` with `&Http` in more methods (#2818) This is possible now more methods are cache-less. --- examples/testing/src/main.rs | 2 +- src/builder/create_stage_instance.rs | 10 +++------- src/builder/create_webhook.rs | 10 +++------- src/builder/edit_stage_instance.rs | 10 +++------- src/builder/edit_voice_state.rs | 8 ++++---- src/model/channel/channel_id.rs | 16 ++++++---------- src/model/channel/guild_channel.rs | 28 ++++++++++------------------ src/model/guild/mod.rs | 8 ++------ 8 files changed, 32 insertions(+), 60 deletions(-) diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index a3d5d1496ff..3699cb9a8e1 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -210,7 +210,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 ") { diff --git a/src/builder/create_stage_instance.rs b/src/builder/create_stage_instance.rs index 75c76a4fe85..8dc589f76a6 100644 --- a/src/builder/create_stage_instance.rs +++ b/src/builder/create_stage_instance.rs @@ -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::*; @@ -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 { + pub async fn execute(mut self, http: &Http, channel_id: ChannelId) -> Result { 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 } } diff --git a/src/builder/create_webhook.rs b/src/builder/create_webhook.rs index c39decef0d9..3b267b8a494 100644 --- a/src/builder/create_webhook.rs +++ b/src/builder/create_webhook.rs @@ -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")] @@ -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 { + pub async fn execute(self, http: &Http, channel_id: ChannelId) -> Result { 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 } } diff --git a/src/builder/edit_stage_instance.rs b/src/builder/edit_stage_instance.rs index fad4dd445f5..ce488691084 100644 --- a/src/builder/edit_stage_instance.rs +++ b/src/builder/edit_stage_instance.rs @@ -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::*; @@ -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 { - 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 { + http.edit_stage_instance(channel_id, &self, self.audit_log_reason).await } } diff --git a/src/builder/edit_voice_state.rs b/src/builder/edit_voice_state.rs index d93301d4c20..0436733167e 100644 --- a/src/builder/edit_voice_state.rs +++ b/src/builder/edit_voice_state.rs @@ -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::*; @@ -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, ) -> 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 } } } diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index db6fa373d97..c9d83952064 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -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 { - builder.execute(cache_http, self).await + pub async fn create_webhook(self, http: &Http, builder: CreateWebhook<'_>) -> Result { + builder.execute(http, self).await } /// Returns a builder which can be awaited to obtain a message or stream of messages in this @@ -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 { - builder.execute(cache_http, self).await + builder.execute(http, self).await } /// Edits the stage instance @@ -822,10 +818,10 @@ impl ChannelId { /// instance currently. pub async fn edit_stage_instance( self, - cache_http: impl CacheHttp, + http: &Http, builder: EditStageInstance<'_>, ) -> Result { - builder.execute(cache_http, self).await + builder.execute(http, self).await } /// Edits a thread. diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 9298f6bd218..31d26660ccb 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -469,7 +469,7 @@ 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<()> { @@ -477,7 +477,7 @@ impl GuildChannel { 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. @@ -526,12 +526,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 @@ -902,18 +898,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 { + pub async fn create_webhook(&self, http: &Http, builder: CreateWebhook<'_>) -> Result { // 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. @@ -940,14 +932,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 { 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 @@ -960,14 +952,14 @@ impl GuildChannel { /// instance currently. pub async fn edit_stage_instance( &self, - cache_http: impl CacheHttp, + http: &Http, builder: EditStageInstance<'_>, ) -> Result { 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. diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 18f322f5b1d..28a77e8c8a2 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -1442,15 +1442,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> { + pub async fn member(&self, http: &Http, user_id: UserId) -> Result> { 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) } }