Skip to content

Commit

Permalink
Message flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
Droid00000 committed Dec 10, 2024
1 parent 2330a27 commit 476897a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/discordrb/api/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def message(token, channel_id, message_id)
# https://discord.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, embeds = nil, nonce = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
body = { content: message, tts: tts, embeds: embeds, nonce: nonce, allowed_mentions: allowed_mentions, message_reference: message_reference, components: components&.to_a }
def create_message(token, channel_id, message, tts = false, embeds = nil, nonce = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil)
body = { content: message, tts: tts, embeds: embeds, nonce: nonce, allowed_mentions: allowed_mentions, message_reference: message_reference, components: components&.to_a, flags: flags }
body = if attachments
files = [*0...attachments.size].zip(attachments).to_h
{ **files, payload_json: body.to_json }
Expand Down Expand Up @@ -117,13 +117,13 @@ def upload_file(token, channel_id, file, caption: nil, tts: false)

# Edit a message
# https://discord.com/developers/docs/resources/channel#edit-message
def edit_message(token, channel_id, message_id, message, mentions = [], embeds = nil, components = nil)
def edit_message(token, channel_id, message_id, message, mentions = [], embeds = nil, components = nil, flags = nil)
Discordrb::API.request(
:channels_cid_messages_mid,
channel_id,
:patch,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}",
{ content: message, mentions: mentions, embeds: embeds, components: components }.to_json,
{ content: message, mentions: mentions, embeds: embeds, components: components, flags: flags }.reject { |_, v| v == :undef }.to_json,
Authorization: token,
content_type: :json
)
Expand Down
10 changes: 6 additions & 4 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,16 @@ def delete_invite(code)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @param message_reference [Message, String, Integer, nil] The message, or message ID, to reply to if any.
# @param components [View, Array<Hash>] Interaction components to associate with this message.
# @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set.
# @return [Message] The message that was sent.
def send_message(channel, content, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
def send_message(channel, content, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil)
channel = channel.resolve_id
debug("Sending message to #{channel} with content '#{content}'")
allowed_mentions = { parse: [] } if allowed_mentions == false
message_reference = { message_id: message_reference.id } if message_reference.respond_to?(:id)
embeds = (embeds.instance_of?(Array) ? embeds.map(&:to_hash) : [embeds&.to_hash]).compact

response = API::Channel.create_message(token, channel, content, tts, embeds, nil, attachments, allowed_mentions&.to_hash, message_reference, components)
response = API::Channel.create_message(token, channel, content, tts, embeds, nil, attachments, allowed_mentions&.to_hash, message_reference, components, flags)
Message.new(JSON.parse(response), self)
end

Expand All @@ -424,11 +425,12 @@ def send_message(channel, content, tts = false, embeds = nil, attachments = nil,
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @param message_reference [Message, String, Integer, nil] The message, or message ID, to reply to if any.
# @param components [View, Array<Hash>] Interaction components to associate with this message.
def send_temporary_message(channel, content, timeout, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
# @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set.
def send_temporary_message(channel, content, timeout, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil)
Thread.new do
Thread.current[:discordrb_name] = "#{@current_thread}-temp-msg"

message = send_message(channel, content, tts, embeds, attachments, allowed_mentions, message_reference, components)
message = send_message(channel, content, tts, embeds, attachments, allowed_mentions, message_reference, components, flags)
sleep(timeout)
message.delete
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 @@ -428,9 +428,10 @@ def slowmode?
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @param message_reference [Message, String, Integer, nil] The message, or message ID, to reply to if any.
# @param components [View, Array<Hash>] Interaction components to associate with this message.
# @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set.
# @return [Message] the message that was sent.
def send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
@bot.send_message(@id, content, tts, embed, attachments, allowed_mentions, message_reference, components)
def send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil)
@bot.send_message(@id, content, tts, embed, attachments, allowed_mentions, message_reference, components, flags)
end

alias_method :send, :send_message
Expand All @@ -444,8 +445,9 @@ def send_message(content, tts = false, embed = nil, attachments = nil, allowed_m
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @param message_reference [Message, String, Integer, nil] The message, or message ID, to reply to if any.
# @param components [View, Array<Hash>] Interaction components to associate with this message.
def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
@bot.send_temporary_message(@id, content, timeout, tts, embed, attachments, allowed_mentions, message_reference, components)
# @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set.
def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil)
@bot.send_temporary_message(@id, content, timeout, tts, embed, attachments, allowed_mentions, message_reference, components, flags)
end

# Convenience method to send a message with an embed.
Expand All @@ -461,16 +463,17 @@ def send_temporary_message(content, timeout, tts = false, embed = nil, attachmen
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @param message_reference [Message, String, Integer, nil] The message, or message ID, to reply to if any.
# @param components [View, Array<Hash>] Interaction components to associate with this message.
# @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set.
# @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, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil)
def send_embed(message = '', embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil)
embed ||= Discordrb::Webhooks::Embed.new
view = Discordrb::Webhooks::View.new

yield(embed, view) if block_given?

send_message(message, tts, embed, attachments, allowed_mentions, message_reference, components || view.to_a)
send_message(message, tts, embed, attachments, allowed_mentions, message_reference, components || view.to_a, flags)
end

# Sends multiple messages to a channel
Expand Down
14 changes: 13 additions & 1 deletion lib/discordrb/data/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ class Message
# @return [Integer, nil] the webhook ID that sent this message, or `nil` if it wasn't sent through a webhook.
attr_reader :webhook_id

# @return [Array<Component>]
# @return [Array<Component>] An array of interaction components if it has any.
attr_reader :components

# @return [Integer] Flags set for this message.
attr_reader :flags

# @!visibility private
def initialize(data, bot)
@bot = bot
Expand Down Expand Up @@ -157,6 +160,8 @@ def initialize(data, bot)

@components = []
@components = data['components'].map { |component_data| Components.from_data(component_data, @bot) } if data['components']

@flags = data['flags'] || 0
end

# Replies to this message with the specified content.
Expand Down Expand Up @@ -230,6 +235,13 @@ def crosspost
Message.new(JSON.parse(response), @bot)
end

# Suppresses the embeds on a message.
def suppress_embeds
flags = @flags | 1 << 2
response = API::Channel.edit_message(@bot.token, @channel.id, @id, @content, [], :undef, :undef, flags)
Message.new(JSON.parse(response), @bot)
end

# Add an {Await} for a message with the same user and channel.
# @see Bot#add_await
# @deprecated Will be changed to blocking behavior in v4.0. Use {#await!} instead.
Expand Down

0 comments on commit 476897a

Please sign in to comment.