Skip to content

Commit

Permalink
Implement create_invite on ChannelId (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Oct 13, 2020
1 parent c6150ae commit c0c2137
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::model::prelude::*;
use std::fmt::Write as FmtWrite;
#[cfg(feature = "model")]
use crate::builder::{
CreateInvite,
CreateMessage,
EditChannel,
EditMessage,
Expand All @@ -13,7 +14,7 @@ use crate::builder::{
use crate::cache::Cache;
#[cfg(feature = "model")]
use crate::http::AttachmentType;
#[cfg(feature = "model")]
#[cfg(all(feature = "model", feature = "utils"))]
use crate::utils;
#[cfg(feature = "model")]
use crate::http::{Http, CacheHttp, Typing};
Expand Down Expand Up @@ -60,6 +61,20 @@ impl ChannelId {
http.as_ref().broadcast_typing(self.0).await
}

/// Creates an invite leading to the given channel.
#[cfg(feature = "utils")]
pub async fn create_invite<F>(&self, http: impl AsRef<Http>, f: F) -> Result<RichInvite>
where
F: FnOnce(&mut CreateInvite) -> &mut CreateInvite
{
let mut invite = CreateInvite::default();
f(&mut invite);

let map = utils::hashmap_to_json_map(invite.0);

http.as_ref().create_invite(self.0, &map).await
}

/// Creates a [permission overwrite][`PermissionOverwrite`] for either a
/// single [`Member`] or [`Role`] within the channel.
///
Expand Down Expand Up @@ -827,7 +842,7 @@ impl<H: AsRef<Http>> MessagesIter<H> {
///
/// This drops any messages that were currently in the buffer, so it should
/// only be called when `self.buffer` is empty. Additionally, this updates
/// `self.before` so that the next call does not return duplicate items.
/// `self.before` so that the next call does not return duplicate items.
/// If there are no more messages to be fetched, then this marks
/// `self.before` as None, indicating that no more calls ought to be made.
///
Expand Down
14 changes: 6 additions & 8 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ impl GuildChannel {
/// ```rust,ignore
/// let invite = channel.create_invite(&context, |i| i.max_uses(5)).await;
/// ```
#[inline]
#[cfg(feature = "utils")]
pub async fn create_invite<F>(&self, cache_http: impl CacheHttp, f: F) -> Result<RichInvite>
where F: FnOnce(&mut CreateInvite) -> &mut CreateInvite {
where
F: FnOnce(&mut CreateInvite) -> &mut CreateInvite
{
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
Expand All @@ -161,12 +164,7 @@ impl GuildChannel {
}
}

let mut invite = CreateInvite::default();
f(&mut invite);

let map = serenity_utils::hashmap_to_json_map(invite.0);

cache_http.http().create_invite(self.id.0, &map).await
self.id.create_invite(cache_http.http(), f).await
}

/// Creates a [permission overwrite][`PermissionOverwrite`] for either a
Expand Down Expand Up @@ -952,7 +950,7 @@ impl GuildChannel {
"data:image/png;base64,".to_string() + &base64::encode(&picture)
},
};

let map = serde_json::json!({
"name": name,
"avatar": avatar
Expand Down

0 comments on commit c0c2137

Please sign in to comment.