Skip to content

Commit

Permalink
Add support for attachment:// in create_message
Browse files Browse the repository at this point in the history
  • Loading branch information
swarley authored Aug 31, 2020
1 parent 03410e9 commit 0ac2aa2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
22 changes: 17 additions & 5 deletions lib/discordrb/api/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,28 @@ def message(token, channel_id, message_id)
end

# Send a message to a channel
# https://discord.com/developers/docs/resources/channel#create-message
def create_message(token, channel_id, message, tts = false, embed = nil, nonce = nil)
# https://discordapp.com/developers/docs/resources/channel#create-message
# @param attachments [Array<File>, nil] Attachments to use with `attachment://` in embeds. See
# https://discord.com/developers/docs/resources/channel#create-message-using-attachments-within-embeds
def create_message(token, channel_id, message, tts = false, embed = nil, nonce = nil, attachments = nil)
body = { content: message, tts: tts, embed: embed, nonce: nonce }
body = if attachments
files = [*0...attachments.size].zip(attachments).to_h
{ **files, payload_json: body.to_json }
else
body.to_json
end

headers = { Authorization: token }
headers[:content_type] = :json unless attachments

Discordrb::API.request(
:channels_cid_messages_mid,
channel_id,
:post,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages",
{ content: message, tts: tts, embed: embed, nonce: nonce }.to_json,
Authorization: token,
content_type: :json
body,
**headers
)
rescue RestClient::BadRequest => e
parsed = JSON.parse(e.response.body)
Expand Down
4 changes: 2 additions & 2 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,11 @@ def delete_invite(code)
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
# @return [Message] The message that was sent.
def send_message(channel, content, tts = false, embed = nil)
def send_message(channel, content, tts = false, embed = nil, attachments = nil)
channel = channel.resolve_id
debug("Sending message to #{channel} with content '#{content}'")

response = API::Channel.create_message(token, channel, content, tts, embed ? embed.to_hash : nil)
response = API::Channel.create_message(token, channel, content, tts, embed ? embed.to_hash : nil, nil, attachments)
Message.new(JSON.parse(response), self)
end

Expand Down
15 changes: 9 additions & 6 deletions lib/discordrb/data/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ def slowmode?
# @param content [String] The content to send. Should not be longer than 2000 characters or it will result in an error.
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
# @param attachments [Array<File>] Files that can be referenced in embeds via `attachment://file.png`
# @return [Message] the message that was sent.
def send_message(content, tts = false, embed = nil)
@bot.send_message(@id, content, tts, embed)
def send_message(content, tts = false, embed = nil, attachments = nil)
@bot.send_message(@id, content, tts, embed, attachments)
end

alias_method :send, :send_message
Expand All @@ -350,8 +351,9 @@ def send_message(content, tts = false, embed = nil)
# @param timeout [Float] The amount of time in seconds after which the message sent will be deleted.
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
def send_temporary_message(content, timeout, tts = false, embed = nil)
@bot.send_temporary_message(@id, content, timeout, tts, embed)
# @param attachments [Array<File>] Files that can be referenced in embeds via `attachment://file.png`
def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil)
@bot.send_temporary_message(@id, content, timeout, tts, embed, attachments)
end

# Convenience method to send a message with an embed.
Expand All @@ -362,13 +364,14 @@ def send_temporary_message(content, timeout, tts = false, embed = nil)
# end
# @param message [String] The message that should be sent along with the embed. If this is the empty string, only the embed will be shown.
# @param embed [Discordrb::Webhooks::Embed, nil] The embed to start the building process with, or nil if one should be created anew.
# @param attachments [Array<File>] Files that can be referenced in embeds via `attachment://file.png`
# @yield [embed] Yields the embed to allow for easy building inside a block.
# @yieldparam embed [Discordrb::Webhooks::Embed] The embed from the parameters, or a new one.
# @return [Message] The resulting message.
def send_embed(message = '', embed = nil)
def send_embed(message = '', embed = nil, attachments = nil)
embed ||= Discordrb::Webhooks::Embed.new
yield(embed) if block_given?
send_message(message, false, embed)
send_message(message, false, embed, attachments)
end

# Sends multiple messages to a channel
Expand Down
10 changes: 6 additions & 4 deletions lib/discordrb/events/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ module Respondable
# @param content [String] The message to send to the channel
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
# @param attachments [Array<File>] Files that can be referenced in embeds via `attachment://file.png`
# @return [Discordrb::Message] the message that was sent
def send_message(content, tts = false, embed = nil)
channel.send_message(content, tts, embed)
def send_message(content, tts = false, embed = nil, attachments = nil)
channel.send_message(content, tts, embed, attachments)
end

# The same as {#send_message}, but yields a {Webhooks::Embed} for easy building of embedded content inside a block.
# @see Channel#send_embed
# @param message [String] The message that should be sent along with the embed. If this is the empty string, only the embed will be shown.
# @param embed [Discordrb::Webhooks::Embed, nil] The embed to start the building process with, or nil if one should be created anew.
# @param attachments [Array<File>] Files that can be referenced in embeds via `attachment://file.png`
# @yield [embed] Yields the embed to allow for easy building inside a block.
# @yieldparam embed [Discordrb::Webhooks::Embed] The embed from the parameters, or a new one.
# @return [Message] The resulting message.
def send_embed(message = '', embed = nil, &block)
channel.send_embed(message, embed, &block)
def send_embed(message = '', embed = nil, attachments = nil, &block)
channel.send_embed(message, embed, attachments, &block)
end

# Sends a temporary message to the channel this message was sent in, right now.
Expand Down

0 comments on commit 0ac2aa2

Please sign in to comment.