From 476897ae1d1c749cb28a4a0405623f390e445856 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Tue, 10 Dec 2024 18:30:23 -0500 Subject: [PATCH] Message flags. --- lib/discordrb/api/channel.rb | 8 ++++---- lib/discordrb/bot.rb | 10 ++++++---- lib/discordrb/data/channel.rb | 15 +++++++++------ lib/discordrb/data/message.rb | 14 +++++++++++++- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/discordrb/api/channel.rb b/lib/discordrb/api/channel.rb index 0548fd6c7..a43d4049f 100644 --- a/lib/discordrb/api/channel.rb +++ b/lib/discordrb/api/channel.rb @@ -75,8 +75,8 @@ def message(token, channel_id, message_id) # https://discord.com/developers/docs/resources/channel#create-message # @param attachments [Array, 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 } @@ -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 ) diff --git a/lib/discordrb/bot.rb b/lib/discordrb/bot.rb index 9fb43b91d..ba3068fdf 100644 --- a/lib/discordrb/bot.rb +++ b/lib/discordrb/bot.rb @@ -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] 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 @@ -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] 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 diff --git a/lib/discordrb/data/channel.rb b/lib/discordrb/data/channel.rb index 4f442b7c8..00021eb79 100644 --- a/lib/discordrb/data/channel.rb +++ b/lib/discordrb/data/channel.rb @@ -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] 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 @@ -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] 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. @@ -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] 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 diff --git a/lib/discordrb/data/message.rb b/lib/discordrb/data/message.rb index 7af0fef46..e46065d4e 100644 --- a/lib/discordrb/data/message.rb +++ b/lib/discordrb/data/message.rb @@ -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] + # @return [Array] 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 @@ -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. @@ -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.