Skip to content

Commit

Permalink
Add new audit log events from discord/discord-api-docs#1191
Browse files Browse the repository at this point in the history
  • Loading branch information
swarley committed Jun 29, 2020
1 parent 639df58 commit b150b46
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
28 changes: 16 additions & 12 deletions lib/discordrb/api/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,30 @@ def edit_message(token, channel_id, message_id, message, mentions = [], embed =
end

# Delete a message
# https://discord.com/developers/docs/resources/channel#delete-message
def delete_message(token, channel_id, message_id)
# https://discordapp.com/developers/docs/resources/channel#delete-message
def delete_message(token, channel_id, message_id, reason = nil)
Discordrb::API.request(
:channels_cid_messages_mid,
channel_id,
:delete,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}",
Authorization: token
Authorization: token,
'X-Audit-Log-Reason': reason
)
end

# Delete messages in bulk
# https://discord.com/developers/docs/resources/channel#bulk-delete-messages
def bulk_delete_messages(token, channel_id, messages = [])
# https://discordapp.com/developers/docs/resources/channel#bulk-delete-messages
def bulk_delete_messages(token, channel_id, messages = [], reason = nil)
Discordrb::API.request(
:channels_cid_messages_bulk_delete,
channel_id,
:post,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages/bulk-delete",
{ messages: messages }.to_json,
Authorization: token,
content_type: :json
content_type: :json,
'X-Audit-Log-Reason': reason
)
end

Expand Down Expand Up @@ -290,27 +292,29 @@ def pinned_messages(token, channel_id)
end

# Pin a message
# https://discord.com/developers/docs/resources/channel#add-pinned-channel-message
def pin_message(token, channel_id, message_id)
# https://discordapp.com/developers/docs/resources/channel#add-pinned-channel-message
def pin_message(token, channel_id, message_id, reason = nil)
Discordrb::API.request(
:channels_cid_pins_mid,
channel_id,
:put,
"#{Discordrb::API.api_base}/channels/#{channel_id}/pins/#{message_id}",
nil,
Authorization: token
Authorization: token,
'X-Audit-Log-Reason': reason
)
end

# Unpin a message
# https://discord.com/developers/docs/resources/channel#delete-pinned-channel-message
def unpin_message(token, channel_id, message_id)
# https://discordapp.com/developers/docs/resources/channel#delete-pinned-channel-message
def unpin_message(token, channel_id, message_id, reason = nil)
Discordrb::API.request(
:channels_cid_pins_mid,
channel_id,
:delete,
"#{Discordrb::API.api_base}/channels/#{channel_id}/pins/#{message_id}",
Authorization: token
Authorization: token,
'X-Audit-Log-Reason': reason
)
end

Expand Down
14 changes: 8 additions & 6 deletions lib/discordrb/api/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,16 @@ def integrations(token, server_id)
end

# Create a server integration
# https://discord.com/developers/docs/resources/guild#create-guild-integration
def create_integration(token, server_id, type, id)
# https://discordapp.com/developers/docs/resources/guild#create-guild-integration
def create_integration(token, server_id, type, id, reason = nil)
Discordrb::API.request(
:guilds_sid_integrations,
server_id,
:post,
"#{Discordrb::API.api_base}/guilds/#{server_id}/integrations",
{ type: type, id: id },
Authorization: token
Authorization: token,
'X-Audit-Log-Reason': reason
)
end

Expand All @@ -401,14 +402,15 @@ def update_integration(token, server_id, integration_id, expire_behavior, expire
end

# Delete a server integration
# https://discord.com/developers/docs/resources/guild#delete-guild-integration
def delete_integration(token, server_id, integration_id)
# https://discordapp.com/developers/docs/resources/guild#delete-guild-integration
def delete_integration(token, server_id, integration_id, reason = nil)
Discordrb::API.request(
:guilds_sid_integrations_iid,
server_id,
:delete,
"#{Discordrb::API.api_base}/guilds/#{server_id}/integrations/#{integration_id}",
Authorization: token
Authorization: token,
'X-Audit-Log-Reason': reason
)
end

Expand Down
39 changes: 35 additions & 4 deletions lib/discordrb/data/audit_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class AuditLogs
23 => :member_ban_remove,
24 => :member_update,
25 => :member_role_update,
26 => :member_move,
27 => :member_disconnect,
28 => :bot_add,
30 => :role_create,
31 => :role_update,
32 => :role_delete,
Expand All @@ -32,9 +35,35 @@ class AuditLogs
62 => :emoji_delete,
# 70
# 71
72 => :message_delete
72 => :message_delete,
73 => :message_bulk_delete,
74 => :message_pin,
75 => :message_unpin,
80 => :integration_create,
81 => :integration_update,
82 => :integration_delete
}.freeze

# @!visibility private
CREATE_ACTIONS = %i[
channel_create channel_overwrite_create member_ban_add role_create
invite_create webhook_create emoji_create integration_create
].freeze

# @!visibility private
DELETE_ACTIONS = %i[
channel_delete channel_overwrite_delete member_kick member_prune
member_ban_remove role_delete invite_delete webhook_delete
emoji_delete message_delete message_bulk_delete integration_delete
].freeze

# @!visibility private
UPDATE_ACTIONS = %i[
server_update channel_update channel_overwrite_update member_update
member_role_update role_update invite_update webhook_update
emoji_update integration_update
].freeze

# @return [Hash<String => User>] the users included in the audit logs.
attr_reader :users

Expand Down Expand Up @@ -142,6 +171,7 @@ def process_target(id, type)
when :invite then @bot.invite(@data['changes'].find { |change| change['key'] == 'code' }.values.delete_if { |v| v == 'code' }.first)
when :webhook then @server.webhooks.find { |webhook| webhook.id == id } || @logs.webhook(id)
when :emoji then @server.emoji[id]
when :integration then @server.integrations.find { |integration| integration.id == id }
end
end

Expand Down Expand Up @@ -295,6 +325,7 @@ def self.target_type_for(action)
when 50..59 then :webhook
when 60..69 then :emoji
when 70..79 then :message
when 80..89 then :integration
else :unknown
end
end
Expand All @@ -304,9 +335,9 @@ def self.target_type_for(action)
# @!visibility private
def self.action_type_for(action)
action = ACTIONS[action]
return :create if %i[channel_create channel_overwrite_create member_ban_add role_create invite_create webhook_create emoji_create].include?(action)
return :delete if %i[channel_delete channel_overwrite_delete member_kick member_prune member_ban_remove role_delete invite_delete webhook_delete emoji_delete message_delete].include?(action)
return :update if %i[server_update channel_update channel_overwrite_update member_update member_role_update role_update invite_update webhook_update emoji_update].include?(action)
return :create if CREATE_ACTIONS.include?(action)
return :delete if DELETE_ACTIONS.include?(action)
return :update if UPDATE_ACTIONS.include?(action)

:unknown
end
Expand Down
16 changes: 9 additions & 7 deletions lib/discordrb/data/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,13 @@ def pins
# @param amount [Integer] The amount of message history to consider for pruning. Must be a value between 2 and 100 (Discord limitation)
# @param strict [true, false] Whether an error should be raised when a message is reached that is too old to be bulk
# deleted. If this is false only a warning message will be output to the console.
# @param reason [String, nil] The reason for pruning
# @raise [ArgumentError] if the amount of messages is not a value between 2 and 100
# @yield [message] Yields each message in this channels history for filtering the messages to delete
# @example Pruning messages from a specific user ID
# channel.prune(100) { |m| m.author.id == 83283213010599936 }
# @return [Integer] The amount of messages that were successfully deleted
def prune(amount, strict = false, &block)
def prune(amount, strict = false, reason = nil, &block)
raise ArgumentError, 'Can only delete between 1 and 100 messages!' unless amount.between?(1, 100)

messages =
Expand All @@ -583,24 +584,25 @@ def prune(amount, strict = false, &block)
when 0
0
when 1
API::Channel.delete_message(@bot.token, @id, messages.first)
API::Channel.delete_message(@bot.token, @id, messages.first, reason)
1
else
bulk_delete(messages, strict)
bulk_delete(messages, strict, reason)
end
end

# Deletes a collection of messages
# @param messages [Array<Message, String, Integer>] the messages (or message IDs) to delete. Total must be an amount between 2 and 100 (Discord limitation)
# @param strict [true, false] Whether an error should be raised when a message is reached that is too old to be bulk
# deleted. If this is false only a warning message will be output to the console.
# @param reason [String, nil] The reason for deleting the messages
# @raise [ArgumentError] if the amount of messages is not a value between 2 and 100
# @return [Integer] The amount of messages that were successfully deleted
def delete_messages(messages, strict = false)
def delete_messages(messages, strict = false, reason = nil)
raise ArgumentError, 'Can only delete between 2 and 100 messages!' unless messages.count.between?(2, 100)

messages.map!(&:resolve_id)
bulk_delete(messages, strict)
bulk_delete(messages, strict, reason)
end

# Updates the cached permission overwrites
Expand Down Expand Up @@ -790,7 +792,7 @@ def link
TWO_WEEKS = 86_400 * 14

# Deletes a list of messages on this channel using bulk delete.
def bulk_delete(ids, strict = false)
def bulk_delete(ids, strict = false, reason = nil)
min_snowflake = IDObject.synthesise(Time.now - TWO_WEEKS)

ids.reject! do |e|
Expand All @@ -803,7 +805,7 @@ def bulk_delete(ids, strict = false)
true
end

API::Channel.bulk_delete_messages(@bot.token, @id, ids)
API::Channel.bulk_delete_messages(@bot.token, @id, ids, reason)
ids.size
end

Expand Down
12 changes: 6 additions & 6 deletions lib/discordrb/data/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ def edit(new_content, new_embed = nil)
end

# Deletes this message.
def delete
API::Channel.delete_message(@bot.token, @channel.id, @id)
def delete(reason = nil)
API::Channel.delete_message(@bot.token, @channel.id, @id, reason)
nil
end

# Pins this message
def pin
API::Channel.pin_message(@bot.token, @channel.id, @id)
def pin(reason = nil)
API::Channel.pin_message(@bot.token, @channel.id, @id, reason)
@pinned = true
nil
end

# Unpins this message
def unpin
API::Channel.unpin_message(@bot.token, @channel.id, @id)
def unpin(reason = nil)
API::Channel.unpin_message(@bot.token, @channel.id, @id, reason)
@pinned = false
nil
end
Expand Down

0 comments on commit b150b46

Please sign in to comment.