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

Refactor Discord bot logic to split response messages in paragraphs #113

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions lib/bas/bot/write_media_review_in_discord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def read
def process
return { success: { review_added: nil } } if unprocessable_response

response = Utils::Discord::Request.write_media_text(params)
response = Utils::Discord::Request.split_paragraphs(params)

if response.code == 200
if !response.empty?
{ success: { message_id: read_response.data["message_id"], property: read_response.data["property"] } }
else
{ error: { message: response.parsed_response, status_code: response.code } }
{ error: { message: "Response is empty" } }
end
end

Expand All @@ -88,27 +88,11 @@ def conditions

def params
{
body:,
body: read_response.data["review"],
secret_token: process_options[:secret_token],
message_id: read_response.data["message_id"],
channel_id: read_response.data["channel_id"]
}
end

def body
{ content: "#{toggle_title}\n\n#{read_response.data["review"]}\n\n#{mention_content}" }
end

def mention_content
author_name = read_response.data["author"]
"<@#{author_name}>"
end

def toggle_title
case read_response.data["media_type"]
when "images" then "Image review results"
when "paragraph" then "Text review results"
end
end
end
end
17 changes: 15 additions & 2 deletions lib/bas/utils/discord/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,23 @@ def self.get_discord_images(message)
}
end

def self.write_media_text(params)
def self.write_media_text(params, combined_paragraphs)
url_message = URI.parse("#{DISCORD_BASE_URL}/channels/#{params[:channel_id]}/messages")
headers = headers(params[:secret_token])
HTTParty.post(url_message, { body: params[:body].to_json, headers: })
body = { content: combined_paragraphs }.to_json

HTTParty.post(url_message, { body:, headers: })
end

def self.split_paragraphs(params)
paragraphs = params[:body].split("--PARAGRAPH--").map(&:strip).reject(&:empty?)

paragraphs.each_slice(2) do |paragraph|
next if paragraph.empty?

combined_paragraphs = paragraph.join("\n\n")
write_media_text(params, combined_paragraphs)
end
end

def self.headers(secret_token)
Expand Down
8 changes: 3 additions & 5 deletions spec/bas/bot/write_media_review_in_discord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"message_id" => "1285685692772646922", "channel_id" => "1285685692772646933", "media_type" => "images" }
end

let(:error_response) { { "object" => "error", "status" => 404, "message" => "not found" } }
let(:error_response) { { "object" => "error", "message" => "Response is empty" } }

let(:response) { double("https_response") }

Expand All @@ -99,13 +99,11 @@
end

it "returns an error hash with the error message" do
allow(response).to receive(:code).and_return(404)

allow(response).to receive(:parsed_response).and_return(error_response)
allow(Utils::Discord::Request).to receive(:split_paragraphs).and_return([])

processed = @bot.process

expect(processed).to eq({ error: { message: error_response, status_code: 404 } })
expect(processed).to eq({ error: { message: "Response is empty" } })
end
end

Expand Down
Loading