Skip to content

Commit

Permalink
Add audit log reason support to most HTTP endpoints (serenity-rs#1354)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangalio authored and arqunis committed Dec 23, 2021
1 parent 587bf7b commit 77b2068
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 83 deletions.
190 changes: 142 additions & 48 deletions src/http/client.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/model/channel/channel_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ChannelCategory {
f(&mut edit_channel);
let map = serenity_utils::hashmap_to_json_map(edit_channel.0);

cache_http.http().edit_channel(self.id.0, &map).await.map(|channel| {
cache_http.http().edit_channel(self.id.0, &map, None).await.map(|channel| {
let GuildChannel {
id,
guild_id,
Expand Down
12 changes: 6 additions & 6 deletions src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl ChannelId {

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

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

/// Creates a [permission overwrite][`PermissionOverwrite`] for either a
Expand Down Expand Up @@ -347,7 +347,7 @@ impl ChannelId {

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

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

/// Edits a [`Message`] in the channel given its Id.
Expand Down Expand Up @@ -556,7 +556,7 @@ impl ChannelId {
/// [Manage Messages]: Permissions::MANAGE_MESSAGES
#[inline]
pub async fn pin(self, http: impl AsRef<Http>, message_id: impl Into<MessageId>) -> Result<()> {
http.as_ref().pin_message(self.0, message_id.into().0).await
http.as_ref().pin_message(self.0, message_id.into().0, None).await
}

/// Crossposts a [`Message`].
Expand Down Expand Up @@ -846,7 +846,7 @@ impl ChannelId {
http: impl AsRef<Http>,
message_id: impl Into<MessageId>,
) -> Result<()> {
http.as_ref().unpin_message(self.0, message_id.into().0).await
http.as_ref().unpin_message(self.0, message_id.into().0, None).await
}

/// Retrieves the channel's webhooks.
Expand Down Expand Up @@ -877,7 +877,7 @@ impl ChannelId {
"name": name.to_string(),
});

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

/// Creates a webhook with a name and an avatar.
Expand Down Expand Up @@ -932,7 +932,7 @@ impl ChannelId {
"avatar": avatar
});

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

/// Returns a future that will await one message sent in this channel.
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl GuildChannel {
f(&mut edit_channel);
let edited = serenity_utils::hashmap_to_json_map(edit_channel.0);

*self = cache_http.http().edit_channel(self.id.0, &edited).await?;
*self = cache_http.http().edit_channel(self.id.0, &edited, None).await?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ impl Message {
}
}

cache_http.http().unpin_message(self.channel_id.0, self.id.0).await
cache_http.http().unpin_message(self.channel_id.0, self.id.0, None).await
}

/// Tries to return author's nickname in the current channel's guild.
Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Emoji {
});

*self = AsRef::<Http>::as_ref(&cache_http)
.edit_emoji(guild_id.0, self.id.0, &map)
.edit_emoji(guild_id.0, self.id.0, &map, None)
.await?;

Ok(())
Expand Down
32 changes: 14 additions & 18 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl GuildId {

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

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

/// Creates an emoji in the guild with a name and base64-encoded image.
Expand Down Expand Up @@ -247,7 +247,7 @@ impl GuildId {
"image": image,
});

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

/// Creates an integration for the guild.
Expand All @@ -272,7 +272,7 @@ impl GuildId {
"type": kind,
});

http.as_ref().create_guild_integration(self.0, integration_id.0, &map).await
http.as_ref().create_guild_integration(self.0, integration_id.0, &map, None).await
}

/// Creates a new role in the guild with the data set, if any.
Expand All @@ -296,7 +296,7 @@ impl GuildId {
f(&mut edit_role);
let map = utils::hashmap_to_json_map(edit_role.0);

let role = http.as_ref().create_role(self.0, &map).await?;
let role = http.as_ref().create_role(self.0, &map, None).await?;

if let Some(position) = map.get("position").and_then(Value::as_u64) {
self.edit_role_position(&http, role.id, position).await?;
Expand Down Expand Up @@ -402,7 +402,7 @@ impl GuildId {
f(&mut edit_guild);
let map = utils::hashmap_to_json_map(edit_guild.0);

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

/// Edits an [`Emoji`]'s name in the guild.
Expand All @@ -429,7 +429,7 @@ impl GuildId {
"name": name,
});

http.as_ref().edit_emoji(self.0, emoji_id.into().0, &map).await
http.as_ref().edit_emoji(self.0, emoji_id.into().0, &map, None).await
}

/// Edits the properties of member of the guild, such as muting or
Expand Down Expand Up @@ -465,7 +465,7 @@ impl GuildId {
f(&mut edit_member);
let map = utils::hashmap_to_json_map(edit_member.0);

http.as_ref().edit_member(self.0, user_id.into().0, &map).await
http.as_ref().edit_member(self.0, user_id.into().0, &map, None).await
}

/// Edits the current user's nickname for the guild.
Expand Down Expand Up @@ -523,7 +523,7 @@ impl GuildId {
f(&mut edit_role);
let map = utils::hashmap_to_json_map(edit_role.0);

http.as_ref().edit_role(self.0, role_id.into().0, &map).await
http.as_ref().edit_role(self.0, role_id.into().0, &map, None).await
}

/// Edits the order of [`Role`]s
Expand Down Expand Up @@ -551,7 +551,7 @@ impl GuildId {
role_id: impl Into<RoleId>,
position: u64,
) -> Result<Vec<Role>> {
http.as_ref().edit_role_position(self.0, role_id.into().0, position).await
http.as_ref().edit_role_position(self.0, role_id.into().0, position, None).await
}

/// Edits the [`GuildWelcomeScreen`].
Expand Down Expand Up @@ -743,7 +743,7 @@ impl GuildId {
user_id: impl Into<UserId>,
reason: &str,
) -> Result<()> {
http.as_ref().kick_member_with_reason(self.0, user_id.into().0, reason).await
http.as_ref().kick_member_with_reason(self.0, user_id.into().0, reason, None).await
}

/// Leaves the guild.
Expand Down Expand Up @@ -867,7 +867,7 @@ impl GuildId {
let mut map = JsonMap::new();
map.insert("channel_id".to_string(), from_number(channel_id.into().0));

http.as_ref().edit_member(self.0, user_id.into().0, &map).await
http.as_ref().edit_member(self.0, user_id.into().0, &map, None).await
}

/// Returns the name of whatever guild this id holds.
Expand Down Expand Up @@ -895,7 +895,7 @@ impl GuildId {
) -> Result<Member> {
let mut map = JsonMap::new();
map.insert("channel_id".to_string(), NULL);
http.as_ref().edit_member(self.0, user_id.into().0, &map).await
http.as_ref().edit_member(self.0, user_id.into().0, &map, None).await
}

/// Gets the number of [`Member`]s that would be pruned with the given
Expand Down Expand Up @@ -1047,11 +1047,7 @@ impl GuildId {
/// [Kick Members]: Permissions::KICK_MEMBERS
#[inline]
pub async fn start_prune(self, http: impl AsRef<Http>, days: u16) -> Result<GuildPrune> {
let map = json!({
"days": days,
});

http.as_ref().start_guild_prune(self.0, &map).await
http.as_ref().start_guild_prune(self.0, days as u64, None).await
}

/// Unbans a [`User`] from the guild.
Expand All @@ -1065,7 +1061,7 @@ impl GuildId {
/// [Ban Members]: Permissions::BAN_MEMBERS
#[inline]
pub async fn unban(self, http: impl AsRef<Http>, user_id: impl Into<UserId>) -> Result<()> {
http.as_ref().remove_ban(self.0, user_id.into().0).await
http.as_ref().remove_ban(self.0, user_id.into().0, None).await
}

/// Retrieve's the guild's vanity URL.
Expand Down
17 changes: 11 additions & 6 deletions src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ impl Member {
return Ok(());
}

match http.as_ref().add_member_role(self.guild_id.0, self.user.id.0, role_id.0).await {
match http.as_ref().add_member_role(self.guild_id.0, self.user.id.0, role_id.0, None).await
{
Ok(()) => {
self.roles.push(role_id);

Expand Down Expand Up @@ -119,7 +120,7 @@ impl Member {
builder.roles(&self.roles);
let map = utils::hashmap_to_json_map(builder.0);

match http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map).await {
match http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map, None).await {
Ok(member) => Ok(member.roles),
Err(why) => {
self.roles.retain(|r| !role_ids.contains(r));
Expand Down Expand Up @@ -236,7 +237,7 @@ impl Member {
f(&mut edit_member);
let map = utils::hashmap_to_json_map(edit_member.0);

http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map).await
http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map, None).await
}

/// Retrieves the ID and position of the member's highest role in the
Expand Down Expand Up @@ -447,7 +448,11 @@ impl Member {
return Ok(());
}

match http.as_ref().remove_member_role(self.guild_id.0, self.user.id.0, role_id.0).await {
match http
.as_ref()
.remove_member_role(self.guild_id.0, self.user.id.0, role_id.0, None)
.await
{
Ok(()) => {
self.roles.retain(|r| r.0 != role_id.0);

Expand Down Expand Up @@ -479,7 +484,7 @@ impl Member {
builder.roles(&self.roles);
let map = utils::hashmap_to_json_map(builder.0);

match http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map).await {
match http.as_ref().edit_member(self.guild_id.0, self.user.id.0, &map, None).await {
Ok(member) => Ok(member.roles),
Err(why) => {
self.roles.extend_from_slice(role_ids);
Expand Down Expand Up @@ -520,7 +525,7 @@ impl Member {
/// [Ban Members]: Permissions::BAN_MEMBERS
#[inline]
pub async fn unban(&self, http: impl AsRef<Http>) -> Result<()> {
http.as_ref().remove_ban(self.guild_id.0, self.user.id.0).await
http.as_ref().remove_ban(self.guild_id.0, self.user.id.0, None).await
}

/// Returns the formatted URL of the member's per guild avatar, if one exists.
Expand Down
2 changes: 1 addition & 1 deletion src/model/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Invite {

let map = utils::hashmap_to_json_map(f(CreateInvite::default()).0);

cache_http.http().create_invite(channel_id.0, &map).await
cache_http.http().create_invite(channel_id.0, &map, None).await
}

/// Deletes the invite.
Expand Down

0 comments on commit 77b2068

Please sign in to comment.