From 77731aea5a77eda01ab585169660a9a7aafc1d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zaj=C4=85c?= Date: Sat, 14 Oct 2023 15:42:32 +0200 Subject: [PATCH 1/8] Allow supressing embeds on Message --- lib/discordrb/api/channel.rb | 13 +++++++++++++ lib/discordrb/data/message.rb | 13 +++++++++++++ spec/data/message_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/lib/discordrb/api/channel.rb b/lib/discordrb/api/channel.rb index 0548fd6c7..899843d1d 100644 --- a/lib/discordrb/api/channel.rb +++ b/lib/discordrb/api/channel.rb @@ -129,6 +129,19 @@ def edit_message(token, channel_id, message_id, message, mentions = [], embeds = ) end + # Suppress embeds on a message + def suppress_embeds(token, channel_id, message_id) + Discordrb::API.request( + :channels_cid_messages_mid, + channel_id, + :patch, + "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}", + { flags: 1 << 2 }.to_json, + Authorization: token, + content_type: :json + ) + end + # Delete a message # https://discord.com/developers/docs/resources/channel#delete-message def delete_message(token, channel_id, message_id, reason = nil) diff --git a/lib/discordrb/data/message.rb b/lib/discordrb/data/message.rb index 7af0fef46..9692f15ad 100644 --- a/lib/discordrb/data/message.rb +++ b/lib/discordrb/data/message.rb @@ -73,6 +73,9 @@ class Message # @return [Array] attr_reader :components + # @return [Integer] flags set on the message + attr_reader :flags + # @!visibility private def initialize(data, bot) @bot = bot @@ -157,6 +160,7 @@ 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. @@ -292,6 +296,15 @@ def my_reactions @reactions.select(&:me) end + # Removes embeds from the message + # @return [Message] the resulting message. + def suppress_embeds + Message.new( + JSON.parse(API::Channel.suppress_embeds(@bot.token, @channel.id, @id)), + @bot + ) + end + # Reacts to a message. # @param reaction [String, #to_reaction] the unicode emoji or {Emoji} def create_reaction(reaction) diff --git a/spec/data/message_spec.rb b/spec/data/message_spec.rb index 86dd4b2b5..6231e3803 100644 --- a/spec/data/message_spec.rb +++ b/spec/data/message_spec.rb @@ -259,4 +259,24 @@ message.respond(content, tts, embed, attachments, allowed_mentions, message_reference, components) end end + + describe '#suppress_embeds' do + let(:message) { described_class.new(message_data, bot) } + let(:message_without_embeds) do + message_data.merge( + flags: 1 << 2, + embeds: [] + ) + end + + it 'removes all the embeds' do + expect(Discordrb::API::Channel).to receive(:suppress_embeds) + .with(token, channel_id, message.id).and_return(message_without_embeds.to_json) + + new_message = message.suppress_embeds + + expect(new_message.embeds).to be_empty + expect(new_message.flags).to eq(1 << 2) + end + end end From ff9a68eb1c1074a96b041c767dc140024d59ace2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zaj=C4=85c?= Date: Tue, 12 Nov 2024 22:11:07 +0100 Subject: [PATCH 2/8] Fix Rubocop issues --- lib/discordrb/bot.rb | 2 +- lib/discordrb/data/activity.rb | 2 +- lib/discordrb/data/server.rb | 2 +- lib/discordrb/data/user.rb | 2 +- lib/discordrb/gateway.rb | 2 +- lib/discordrb/permissions.rb | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/discordrb/bot.rb b/lib/discordrb/bot.rb index 9fb43b91d..7c4a49384 100644 --- a/lib/discordrb/bot.rb +++ b/lib/discordrb/bot.rb @@ -1219,7 +1219,7 @@ def process_token(type, token) def handle_dispatch(type, data) # Check whether there are still unavailable servers and there have been more than 10 seconds since READY - if @unavailable_servers&.positive? && (Time.now - @unavailable_timeout_time) > 10 && !((@intents || 0) & INTENTS[:servers]).zero? + if @unavailable_servers&.positive? && (Time.now - @unavailable_timeout_time) > 10 && !(@intents || 0).nobits?(INTENTS[:servers]) # The server streaming timed out! LOGGER.debug("Server streaming timed out with #{@unavailable_servers} servers remaining") LOGGER.debug('Calling ready now because server loading is taking a long time. Servers may be unavailable due to an outage, or your bot is on very large servers.') diff --git a/lib/discordrb/data/activity.rb b/lib/discordrb/data/activity.rb index 0de16a684..981dad30f 100644 --- a/lib/discordrb/data/activity.rb +++ b/lib/discordrb/data/activity.rb @@ -121,7 +121,7 @@ def instance? # @!visibility private def flag_set?(sym) - !(@flags & FLAGS[sym]).zero? + !@flags.nobits?(FLAGS[sym]) end # Timestamps for the start and end of instanced activities diff --git a/lib/discordrb/data/server.rb b/lib/discordrb/data/server.rb index 2569ee3e6..10a7ce02e 100644 --- a/lib/discordrb/data/server.rb +++ b/lib/discordrb/data/server.rb @@ -137,7 +137,7 @@ def members @bot.debug("Members for server #{@id} not chunked yet - initiating") # If the SERVER_MEMBERS intent flag isn't set, the gateway won't respond when we ask for members. - raise 'The :server_members intent is required to get server members' if (@bot.gateway.intents & INTENTS[:server_members]).zero? + raise 'The :server_members intent is required to get server members' if @bot.gateway.intents.nobits?(INTENTS[:server_members]) @bot.request_chunks(@id) sleep 0.05 until @chunked diff --git a/lib/discordrb/data/user.rb b/lib/discordrb/data/user.rb index b7be8500d..d44949095 100644 --- a/lib/discordrb/data/user.rb +++ b/lib/discordrb/data/user.rb @@ -91,7 +91,7 @@ def avatar_url(format = nil) FLAGS.each do |name, value| define_method("#{name}?") do - (@public_flags & value).positive? + @public_flags.anybits?(value) end end end diff --git a/lib/discordrb/gateway.rb b/lib/discordrb/gateway.rb index d09610140..1907a27c3 100644 --- a/lib/discordrb/gateway.rb +++ b/lib/discordrb/gateway.rb @@ -718,7 +718,7 @@ def handle_dispatch(packet) @session = Session.new(data['session_id']) @session.sequence = 0 - @bot.__send__(:notify_ready) if @intents && (@intents & INTENTS[:servers]).zero? + @bot.__send__(:notify_ready) if @intents && @intents.nobits?(INTENTS[:servers]) when :RESUMED # The RESUMED event is received after a successful op 6 (resume). It does nothing except tell the bot the # connection is initiated (like READY would). Starting with v5, it doesn't set a new heartbeat interval anymore diff --git a/lib/discordrb/permissions.rb b/lib/discordrb/permissions.rb index 10d436bed..8dc5588f4 100644 --- a/lib/discordrb/permissions.rb +++ b/lib/discordrb/permissions.rb @@ -80,7 +80,7 @@ def bits=(bits) # Initialize the instance variables based on the bitset. def init_vars FLAGS.each do |position, flag| - flag_set = ((@bits >> position) & 0x1) == 1 + flag_set = (@bits >> position).allbits?(0x1) instance_variable_set "@#{flag}", flag_set end end @@ -131,7 +131,7 @@ def initialize(bits = 0, writer = nil) # permissions.defined_permissions #=> [:create_instant_invite, :administrator] # @return [Array] the permissions def defined_permissions - FLAGS.filter_map { |value, name| (@bits & (1 << value)).positive? ? name : nil } + FLAGS.filter_map { |value, name| @bits.anybits?((1 << value)) ? name : nil } end # Comparison based on permission bits From 6dcbecca31676887e3e69ef2128c069e10b98189 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Tue, 10 Dec 2024 19:11:18 -0500 Subject: [PATCH 3/8] Message flags --- lib/discordrb/api/channel.rb | 21 ++++----------------- lib/discordrb/bot.rb | 12 +++++++----- lib/discordrb/data/channel.rb | 15 +++++++++------ lib/discordrb/data/message.rb | 16 ++++++++-------- 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/lib/discordrb/api/channel.rb b/lib/discordrb/api/channel.rb index 899843d1d..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,26 +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, - Authorization: token, - content_type: :json - ) - end - - # Suppress embeds on a message - def suppress_embeds(token, channel_id, message_id) - Discordrb::API.request( - :channels_cid_messages_mid, - channel_id, - :patch, - "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}", - { flags: 1 << 2 }.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 7c4a49384..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 @@ -1219,7 +1221,7 @@ def process_token(type, token) def handle_dispatch(type, data) # Check whether there are still unavailable servers and there have been more than 10 seconds since READY - if @unavailable_servers&.positive? && (Time.now - @unavailable_timeout_time) > 10 && !(@intents || 0).nobits?(INTENTS[:servers]) + if @unavailable_servers&.positive? && (Time.now - @unavailable_timeout_time) > 10 && !((@intents || 0) & INTENTS[:servers]).zero? # The server streaming timed out! LOGGER.debug("Server streaming timed out with #{@unavailable_servers} servers remaining") LOGGER.debug('Calling ready now because server loading is taking a long time. Servers may be unavailable due to an outage, or your bot is on very large servers.') 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 9692f15ad..567f60718 100644 --- a/lib/discordrb/data/message.rb +++ b/lib/discordrb/data/message.rb @@ -190,8 +190,8 @@ def reply!(content, tts: false, embed: nil, attachments: nil, allowed_mentions: end # (see Channel#send_message) - def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil) - @channel.send_message(content, tts, embed, attachments, allowed_mentions, message_reference, components) + def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil) + @channel.send_message(content, tts, embed, attachments, allowed_mentions, message_reference, components, flags) end # Edits this message to have the specified content instead. @@ -199,12 +199,13 @@ def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentio # @param new_content [String] the new content the message should have. # @param new_embeds [Hash, Discordrb::Webhooks::Embed, Array, Array, nil] The new embeds the message should have. If `nil` the message will be changed to have no embeds. # @param new_components [View, Array] The new components the message should have. If `nil` the message will be changed to have no components. + # @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) can be edited. # @return [Message] the resulting message. - def edit(new_content, new_embeds = nil, new_components = nil) + def edit(new_content, new_embeds = nil, new_components = nil, flags = nil) new_embeds = (new_embeds.instance_of?(Array) ? new_embeds.map(&:to_hash) : [new_embeds&.to_hash]).compact new_components = new_components.to_a - response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embeds, new_components) + response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embeds, new_components, flags) Message.new(JSON.parse(response), @bot) end @@ -299,10 +300,9 @@ def my_reactions # Removes embeds from the message # @return [Message] the resulting message. def suppress_embeds - Message.new( - JSON.parse(API::Channel.suppress_embeds(@bot.token, @channel.id, @id)), - @bot - ) + 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 # Reacts to a message. From 62c4d9ef5af542ff408ee66d73a4c011d019def9 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Tue, 10 Dec 2024 19:18:33 -0500 Subject: [PATCH 4/8] Rubocop --- lib/discordrb/data/message.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/discordrb/data/message.rb b/lib/discordrb/data/message.rb index 567f60718..eae21fffb 100644 --- a/lib/discordrb/data/message.rb +++ b/lib/discordrb/data/message.rb @@ -300,7 +300,7 @@ def my_reactions # Removes embeds from the message # @return [Message] the resulting message. def suppress_embeds - flags = @flags | 1 << 2 + 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 From 279dc8a3f9d37ca5821998433c763b21f4bd54f1 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Tue, 10 Dec 2024 20:08:55 -0500 Subject: [PATCH 5/8] Fix specs, and general improvements. --- lib/discordrb/bot.rb | 4 ++-- lib/discordrb/data/channel.rb | 6 +++--- lib/discordrb/data/message.rb | 9 +++++---- lib/discordrb/events/message.rb | 15 +++++++++------ spec/data/message_spec.rb | 9 +++++---- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/discordrb/bot.rb b/lib/discordrb/bot.rb index ba3068fdf..175571c0d 100644 --- a/lib/discordrb/bot.rb +++ b/lib/discordrb/bot.rb @@ -403,7 +403,7 @@ def delete_invite(code) # @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, flags = nil) + def send_message(channel, content, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) channel = channel.resolve_id debug("Sending message to #{channel} with content '#{content}'") allowed_mentions = { parse: [] } if allowed_mentions == false @@ -426,7 +426,7 @@ def send_message(channel, content, tts = false, embeds = nil, attachments = nil, # @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. - def send_temporary_message(channel, content, timeout, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil) + def send_temporary_message(channel, content, timeout, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) Thread.new do Thread.current[:discordrb_name] = "#{@current_thread}-temp-msg" diff --git a/lib/discordrb/data/channel.rb b/lib/discordrb/data/channel.rb index 00021eb79..4a49894e2 100644 --- a/lib/discordrb/data/channel.rb +++ b/lib/discordrb/data/channel.rb @@ -430,7 +430,7 @@ def slowmode? # @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, flags = nil) + def send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) @bot.send_message(@id, content, tts, embed, attachments, allowed_mentions, message_reference, components, flags) end @@ -446,7 +446,7 @@ def send_message(content, tts = false, embed = nil, attachments = nil, allowed_m # @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. - def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil) + def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) @bot.send_temporary_message(@id, content, timeout, tts, embed, attachments, allowed_mentions, message_reference, components, flags) end @@ -467,7 +467,7 @@ def send_temporary_message(content, timeout, tts = false, embed = nil, attachmen # @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, flags = nil) + def send_embed(message = '', embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) embed ||= Discordrb::Webhooks::Embed.new view = Discordrb::Webhooks::View.new diff --git a/lib/discordrb/data/message.rb b/lib/discordrb/data/message.rb index eae21fffb..2d75a4bf0 100644 --- a/lib/discordrb/data/message.rb +++ b/lib/discordrb/data/message.rb @@ -180,17 +180,18 @@ def reply(content) # @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings # @param mention_user [true, false] Whether the user that is being replied to should be pinged by the reply. # @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 (see #respond) - def reply!(content, tts: false, embed: nil, attachments: nil, allowed_mentions: {}, mention_user: false, components: nil) + def reply!(content, tts: false, embed: nil, attachments: nil, allowed_mentions: {}, mention_user: false, components: nil, flags: 0) allowed_mentions = { parse: [] } if allowed_mentions == false allowed_mentions = allowed_mentions.to_hash.transform_keys(&:to_sym) allowed_mentions[:replied_user] = mention_user - respond(content, tts, embed, attachments, allowed_mentions, self, components) + respond(content, tts, embed, attachments, allowed_mentions, self, components, flags) end # (see Channel#send_message) - def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = nil) + def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) @channel.send_message(content, tts, embed, attachments, allowed_mentions, message_reference, components, flags) end @@ -201,7 +202,7 @@ def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentio # @param new_components [View, Array] The new components the message should have. If `nil` the message will be changed to have no components. # @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) can be edited. # @return [Message] the resulting message. - def edit(new_content, new_embeds = nil, new_components = nil, flags = nil) + def edit(new_content, new_embeds = nil, new_components = nil, flags = 0) new_embeds = (new_embeds.instance_of?(Array) ? new_embeds.map(&:to_hash) : [new_embeds&.to_hash]).compact new_components = new_components.to_a diff --git a/lib/discordrb/events/message.rb b/lib/discordrb/events/message.rb index f72446691..ea0a7fe0a 100644 --- a/lib/discordrb/events/message.rb +++ b/lib/discordrb/events/message.rb @@ -18,9 +18,10 @@ module Respondable # @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, nil] A collection of components to attach to the message. + # @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set. # @return [Discordrb::Message] the message that was sent - def send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil) - channel.send_message(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 = 0) + channel.send_message(content, tts, embed, attachments, allowed_mentions, message_reference, components, flags) end # The same as {#send_message}, but yields a {Webhooks::Embed} for easy building of embedded content inside a block. @@ -32,11 +33,12 @@ 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, nil] A collection of components to attach to the 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, &block) - channel.send_embed(message, embed, attachments, tts, allowed_mentions, message_reference, components, &block) + def send_embed(message = '', embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0, &block) + channel.send_embed(message, embed, attachments, tts, allowed_mentions, message_reference, components, flags, &block) end # Sends a temporary message to the channel this message was sent in, right now. @@ -47,8 +49,9 @@ def send_embed(message = '', embed = nil, attachments = nil, tts = false, allowe # @param attachments [Array] Files that can be referenced in embeds via `attachment://file.png` # @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings # @param components [View, Array, nil] A collection of components to attach to the message. - def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, components = nil) - channel.send_temporary_message(content, timeout, tts, embed, attachments, allowed_mentions, 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, components = nil, flags = 0) + channel.send_temporary_message(content, timeout, tts, embed, attachments, allowed_mentions, components, flags) end # Adds a string to be sent after the event has finished execution. Avoids problems with rate limiting because only diff --git a/spec/data/message_spec.rb b/spec/data/message_spec.rb index 6231e3803..66180303c 100644 --- a/spec/data/message_spec.rb +++ b/spec/data/message_spec.rb @@ -252,11 +252,12 @@ let(:allowed_mentions) { instance_double('Hash', 'allowed_mentions') } let(:message_reference) { instance_double('Discordrb::Message') } let(:components) { instance_double('Discordrb::Webhooks::View') } + let(:flags) { instance_double('Integer') } it 'forwards arguments to Channel#send_message' do - expect(channel).to receive(:send_message).with(content, tts, embed, attachments, allowed_mentions, message_reference, components) + expect(channel).to receive(:send_message).with(content, tts, embed, attachments, allowed_mentions, message_reference, components, flags) - message.respond(content, tts, embed, attachments, allowed_mentions, message_reference, components) + message.respond(content, tts, embed, attachments, allowed_mentions, message_reference, components, flags) end end @@ -270,8 +271,8 @@ end it 'removes all the embeds' do - expect(Discordrb::API::Channel).to receive(:suppress_embeds) - .with(token, channel_id, message.id).and_return(message_without_embeds.to_json) + expect(Discordrb::API::Channel).to receive(:edit_message) + .and_return(message_without_embeds.to_json) new_message = message.suppress_embeds From 82aa6562cf4dfccc4829a6cb2081b01229660281 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Sun, 15 Dec 2024 20:56:11 -0500 Subject: [PATCH 6/8] Fix the specs. --- spec/data/message_spec.rb | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/spec/data/message_spec.rb b/spec/data/message_spec.rb index 66180303c..7c4ce7c91 100644 --- a/spec/data/message_spec.rb +++ b/spec/data/message_spec.rb @@ -193,13 +193,13 @@ let(:mention) { instance_double('TrueClass', 'mention') } it 'responds with a message_reference' do - expect(message).to receive(:respond).with(content, false, nil, nil, hash_including(:replied_user), message, nil) + expect(message).to receive(:respond).with(content, false, nil, nil, hash_including(:replied_user), message, nil, 0) message.reply!(content) end it 'sets replied_user in allowed_mentions' do - expect(message).to receive(:respond).with(content, false, nil, nil, { replied_user: mention }, message, nil) + expect(message).to receive(:respond).with(content, false, nil, nil, { replied_user: mention }, message, nil, 0) message.reply!(content, mention_user: mention) end @@ -208,9 +208,9 @@ let(:mention) { double('mention') } it 'sets parse to an empty array add merges the mention_user param' do - expect(message).to receive(:respond).with(content, false, nil, nil, { parse: [], replied_user: mention }, message, nil) + expect(message).to receive(:respond).with(content, false, nil, nil, { parse: [], replied_user: mention }, message, nil, 0) - message.reply!(content, allowed_mentions: false, mention_user: mention) + message.reply!(content, allowed_mentions: false, mention_user: mention, flags: 0) end end @@ -226,8 +226,8 @@ end it 'converts it to a hash to set the replied_user key' do - expect(message).to receive(:respond).with(content, false, nil, nil, hash, message, nil) - message.reply!(content, allowed_mentions: allowed_mentions, mention_user: mention_user) + expect(message).to receive(:respond).with(content, false, nil, nil, hash, message, nil, 0) + message.reply!(content, allowed_mentions: allowed_mentions, mention_user: mention_user, flags: 0) end end end @@ -260,24 +260,4 @@ message.respond(content, tts, embed, attachments, allowed_mentions, message_reference, components, flags) end end - - describe '#suppress_embeds' do - let(:message) { described_class.new(message_data, bot) } - let(:message_without_embeds) do - message_data.merge( - flags: 1 << 2, - embeds: [] - ) - end - - it 'removes all the embeds' do - expect(Discordrb::API::Channel).to receive(:edit_message) - .and_return(message_without_embeds.to_json) - - new_message = message.suppress_embeds - - expect(new_message.embeds).to be_empty - expect(new_message.flags).to eq(1 << 2) - end - end end From 99539b7bd6df1d084206e1fbf8c39433e66c7a4f Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Sun, 15 Dec 2024 20:58:32 -0500 Subject: [PATCH 7/8] Appease da rubocop. --- lib/discordrb/bot.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/discordrb/bot.rb b/lib/discordrb/bot.rb index 175571c0d..e9d58ab24 100644 --- a/lib/discordrb/bot.rb +++ b/lib/discordrb/bot.rb @@ -1221,7 +1221,7 @@ def process_token(type, token) def handle_dispatch(type, data) # Check whether there are still unavailable servers and there have been more than 10 seconds since READY - if @unavailable_servers&.positive? && (Time.now - @unavailable_timeout_time) > 10 && !((@intents || 0) & INTENTS[:servers]).zero? + if @unavailable_servers&.positive? && (Time.now - @unavailable_timeout_time) > 10 && !(@intents || 0).nobits?(INTENTS[:servers]) # The server streaming timed out! LOGGER.debug("Server streaming timed out with #{@unavailable_servers} servers remaining") LOGGER.debug('Calling ready now because server loading is taking a long time. Servers may be unavailable due to an outage, or your bot is on very large servers.') From f7073a0a0c75efd183e37b01b0f4536952c09e27 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Sun, 15 Dec 2024 21:03:22 -0500 Subject: [PATCH 8/8] Utilize :undef. --- lib/discordrb/data/message.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/discordrb/data/message.rb b/lib/discordrb/data/message.rb index 2d75a4bf0..718a0a103 100644 --- a/lib/discordrb/data/message.rb +++ b/lib/discordrb/data/message.rb @@ -302,7 +302,7 @@ def my_reactions # @return [Message] the resulting message. def suppress_embeds flags = @flags | (1 << 2) - response = API::Channel.edit_message(@bot.token, @channel.id, @id, @content, [], :undef, :undef, flags) + response = API::Channel.edit_message(@bot.token, @channel.id, @id, :undef, :undef, :undef, :undef, flags) Message.new(JSON.parse(response), @bot) end