Skip to content

Commit

Permalink
Discord: Update BUR formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
nottalulah committed Oct 11, 2024
1 parent 04e90d2 commit 9026ad2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
14 changes: 14 additions & 0 deletions app/logical/discord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ def do_count(event, *args)
event << "Error fetching count"
end
end

def do_pendingburs(event, *args)
burs = BulkUpdateRequest.pending.order(created_at: :asc).includes(:user, forum_post: [:votes]).take(10)
if burs.present?
rows = burs.map do |bur|
["BUR ##{bur.id}", bur.user.pretty_name, bur.forum_topic.title, bur.forum_post.formatted_votes, bur.expires_at.strftime("%F")]
end
table = Terminal::Table.new(headings: ["ID", "User", "Title", "Votes", "Expires"], rows: rows)
event << "```\n#{table}\n```"
else
event << "No pending BURs."
end
end
end

class Bot
Expand All @@ -158,6 +171,7 @@ def register_commands
@bot.command(:eval, &method(:do_eval))
@bot.command(:count, &method(:do_count))
@bot.command(:sql, &method(:do_sql))
@bot.command(:pendingburs, &method(:do_pendingburs))

@@messages.each do |name, regex|
@bot.message(contains: regex, &method(:"do_#{name}"))
Expand Down
18 changes: 14 additions & 4 deletions app/models/bulk_update_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def is_rejected?
status == "rejected"
end

def expires_at
date = created_at + 46.days
Time.new(date.year, date.month, date.day, 0, 0, 0)
end

concerning :DiscordMethods do
def discord_author
Discordrb::Webhooks::EmbedAuthor.new(name: "@#{user.pretty_name}", url: user.discord_url)
Expand All @@ -146,16 +151,21 @@ def discord_color
end
end

def discord_title
forum_topic.title
end

def discord_body
script
dtext = DText.new(processor.to_dtext).to_markdown
last_index = dtext[0..1950].rindex("\n")
omitted = dtext[last_index..].count("\n")
"#{dtext[0..last_index]}\n[omitted #{omitted} lines]"
end

def discord_footer
votes = forum_post.votes.map(&:vote_type).tally
vote_info = "#{votes['up'].presence || 0}#{votes['down'].presence || 0}#{votes['meh'].presence || 0}∅"
timestamp = "#{created_at.strftime("%F")}"

Discordrb::Webhooks::EmbedFooter.new(text: "#{vote_info} | #{timestamp}")
Discordrb::Webhooks::EmbedFooter.new(text: "#{forum_post.formatted_votes} | #{timestamp}")
end
end

Expand Down
8 changes: 8 additions & 0 deletions app/models/forum_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def voted?(user, score)
votes.exists?(creator_id: user.id, score: score)
end

def tallied_votes
votes.map(&:vote_type).tally
end

def formatted_votes
"#{tallied_votes['up'].presence || 0}#{tallied_votes['down'].presence || 0}#{tallied_votes['meh'].presence || 0}∅"
end

def validate_deletion_of_original_post
if is_original_post? && is_deleted? && !topic.is_deleted?
errors.add(:base, "Can't delete original post without deleting the topic first")
Expand Down

0 comments on commit 9026ad2

Please sign in to comment.