Skip to content

Commit

Permalink
Remove even more impl CacheHttp (#2822)
Browse files Browse the repository at this point in the history
These were found by auditing .http() calls, instead of just manually
looking at `impl CacheHttp`.
  • Loading branch information
GnomedDev committed Mar 29, 2024
1 parent 8e7f857 commit 357d41a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 33 deletions.
2 changes: 1 addition & 1 deletion examples/e03_struct_utilities/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl EventHandler for Handler {
// In this case, you can direct message a User directly by simply calling a method on
// its instance, with the content of the message.
let builder = CreateMessage::new().content("Hello!");
let dm = msg.author.dm(&context, builder).await;
let dm = msg.author.dm(&context.http, builder).await;

if let Err(why) = dm {
println!("Error when direct messaging user: {why:?}");
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ async fn message(ctx: &Context, msg: &Message) -> Result<(), serenity::Error> {
.flags(MessageFlags::IS_VOICE_MESSAGE)
.add_file(CreateAttachment::url(&ctx.http, audio_url, "testing.ogg").await?);

msg.author.dm(ctx, builder).await?;
msg.author.dm(&ctx.http, builder).await?;
} else if let Some(channel) = msg.content.strip_prefix("movetorootandback") {
let mut channel =
channel.trim().parse::<ChannelId>().unwrap().to_channel(ctx).await?.guild().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ impl Guild {
cache_http: impl CacheHttp,
builder: CreateSticker<'a>,
) -> Result<Sticker> {
self.id.create_sticker(cache_http.http(), builder).await
self.id.create_sticker(cache_http, builder).await
}

/// Deletes the current guild if the current user is the owner of the
Expand Down
4 changes: 2 additions & 2 deletions src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,11 @@ impl PartialGuild {
/// [`Error::Json`]: crate::error::Error::Json
pub async fn start_prune(
&self,
cache_http: impl CacheHttp,
http: &Http,
days: u8,
reason: Option<&str>,
) -> Result<GuildPrune> {
self.id.start_prune(cache_http.http(), days, reason).await
self.id.start_prune(http, days, reason).await
}

/// Kicks a [`Member`] from the guild.
Expand Down
40 changes: 12 additions & 28 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ impl User {
/// # Errors
///
/// See [`UserId::create_dm_channel`] for what errors may be returned.
pub async fn create_dm_channel(&self, cache_http: impl CacheHttp) -> Result<PrivateChannel> {
pub async fn create_dm_channel(&self, http: &Http) -> Result<PrivateChannel> {
if self.bot() {
return Err(Error::Model(ModelError::MessagingBot));
}

self.id.create_dm_channel(cache_http).await
self.id.create_dm_channel(http).await
}

/// Retrieves the time that this user was created at.
Expand All @@ -416,22 +416,14 @@ impl User {
/// # Errors
///
/// See [`UserId::direct_message`] for errors.
pub async fn direct_message(
&self,
cache_http: impl CacheHttp,
builder: CreateMessage<'_>,
) -> Result<Message> {
self.id.direct_message(cache_http, builder).await
pub async fn direct_message(&self, http: &Http, builder: CreateMessage<'_>) -> Result<Message> {
self.id.direct_message(http, builder).await
}

/// This is an alias of [`Self::direct_message`].
#[allow(clippy::missing_errors_doc)]
pub async fn dm(
&self,
cache_http: impl CacheHttp,
builder: CreateMessage<'_>,
) -> Result<Message> {
self.direct_message(cache_http, builder).await
pub async fn dm(&self, http: &Http, builder: CreateMessage<'_>) -> Result<Message> {
self.direct_message(http, builder).await
}

/// Retrieves the URL to the user's avatar, falling back to the default avatar if needed.
Expand Down Expand Up @@ -607,7 +599,7 @@ impl UserId {
/// returned by the Discord API.
///
/// [current user]: CurrentUser
pub async fn create_dm_channel(self, cache_http: impl CacheHttp) -> Result<PrivateChannel> {
pub async fn create_dm_channel(self, http: &Http) -> Result<PrivateChannel> {
#[derive(serde::Serialize)]
struct CreateDmChannel {
recipient_id: UserId,
Expand All @@ -617,7 +609,7 @@ impl UserId {
recipient_id: self,
};

cache_http.http().create_private_channel(&body).await
http.create_private_channel(&body).await
}

/// Sends a message to a user through a direct message channel. This is a channel that can only
Expand Down Expand Up @@ -656,23 +648,15 @@ impl UserId {
/// May also return an [`Error::Http`] if the user cannot be sent a direct message.
///
/// Returns an [`Error::Json`] if there is an error deserializing the API response.
pub async fn direct_message(
self,
cache_http: impl CacheHttp,
builder: CreateMessage<'_>,
) -> Result<Message> {
self.create_dm_channel(&cache_http).await?.send_message(cache_http, builder).await
pub async fn direct_message(self, http: &Http, builder: CreateMessage<'_>) -> Result<Message> {
self.create_dm_channel(http).await?.send_message(http, builder).await
}

/// This is an alias of [`Self::direct_message`].
#[allow(clippy::missing_errors_doc)]
#[inline]
pub async fn dm(
self,
cache_http: impl CacheHttp,
builder: CreateMessage<'_>,
) -> Result<Message> {
self.direct_message(cache_http, builder).await
pub async fn dm(self, http: &Http, builder: CreateMessage<'_>) -> Result<Message> {
self.direct_message(http, builder).await
}

/// First attempts to find a [`User`] by its Id in the cache, upon failure requests it via the
Expand Down

0 comments on commit 357d41a

Please sign in to comment.