Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow supressing embeds on Message #240

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions discordrb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rubocop-rake', '~> 0.6.0'
spec.add_development_dependency 'simplecov', '~> 0.21.0'
spec.add_development_dependency 'yard', '~> 0.9.9'
spec.add_development_dependency 'pry-byebug'
end
13 changes: 13 additions & 0 deletions lib/discordrb/api/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions lib/discordrb/data/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Message
# @return [Array<Component>]
attr_reader :components

# @return [Integer] flags set on the message
attr_reader :flags

# @!visibility private
def initialize(data, bot)
@bot = bot
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -286,6 +290,13 @@ def my_reactions
@reactions.select(&:me)
end

# Removes embeds from the message
# @return [Message] the resulting message.
def suppress_embeds
response = API::Channel.suppress_embeds(@bot.token, @channel.id, @id)
Message.new(JSON.parse(response), @bot)
end

# Reacts to a message.
# @param reaction [String, #to_reaction] the unicode emoji or {Emoji}
def create_reaction(reaction)
Expand Down
20 changes: 20 additions & 0 deletions spec/data/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
SimpleCov.start

require 'json'
require 'pry-byebug'

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
Expand Down