Skip to content

Commit

Permalink
Fix specs, and general improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Droid00000 committed Dec 11, 2024
1 parent 62c4d9e commit 279dc8a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def delete_invite(code)
# @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, 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
Expand All @@ -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<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.
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"

Expand Down
6 changes: 3 additions & 3 deletions lib/discordrb/data/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def slowmode?
# @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, 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

Expand All @@ -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<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.
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

Expand All @@ -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

Expand Down
9 changes: 5 additions & 4 deletions lib/discordrb/data/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 (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

Expand All @@ -201,7 +202,7 @@ def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentio
# @param new_components [View, Array<Hash>] 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

Expand Down
15 changes: 9 additions & 6 deletions lib/discordrb/events/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash>, 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.
Expand All @@ -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<Hash>, 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.
Expand All @@ -47,8 +49,9 @@ def send_embed(message = '', embed = nil, attachments = nil, tts = false, allowe
# @param attachments [Array<File>] 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<Hash>, 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
Expand Down
9 changes: 5 additions & 4 deletions spec/data/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 279dc8a

Please sign in to comment.