Add (untested) edit puzzle command

This commit is contained in:
Matthias Portzel 2024-03-23 02:17:01 -04:00
parent 9276a2e574
commit 37a8ab8738
3 changed files with 37 additions and 5 deletions

7
db.rb
View File

@ -70,9 +70,10 @@ class Puzzle < Sequel::Model
# The text that will be sent for this puzzle's message
def get_message_text
# Author attribution line
# Puzzle_send_job relies on the second thing returned here being the main puzzle text
author_attr = author_id != $me.id ? "*This guest puzzle was written by <@#{author_id}>*\n" : ""
return [
"**Puzzle 0x#{public_id.to_s(16)}**\n<@&#{$puzzle_pings_role.id}>\n#{author_attr}",
"**Puzzle 0x#{display_id}**\nAccess to the club has been reset. To re-enter, please solve the new puzzle. <@&#{$puzzle_pings_role.id}>\n#{author_attr}",
text,
"Submit your answer with `/solve` in <#1140124119174754429>. If you have an idea for a future puzzle, use `/add-puzzle` for information about how to create one."
]
@ -82,6 +83,10 @@ class Puzzle < Sequel::Model
def message
@message ||= $puzzle_channel.load_message message_id
end
def display_id
return public_id.to_s(16)
end
end

29
main.rb
View File

@ -170,7 +170,7 @@ $bot.message do |event|
next
end
# Save the user's id who submitted it
p.author_id = event.user.id
p.author_id = event.user.id unless p.author_id.present?
p.save
event.channel.send "Successfully added puzzle!"
@ -233,6 +233,33 @@ define_command "Solve for", [
end
# /edit-puzzle-message <public_id> <find_regex> <new_text>
define_command "Edit Puzzle", [
{ type: :string, name: "puzzle_public_id", description: "The public id of the puzzle to edit", required: true },
{ type: :string, name: "find_regex", description: "The regex to match", required: true },
{ type: :string, name: "new_text", description: "The text to replace with", required: true },
] do |event|
if event.user != $me
event.respond content: "This is an admin command for editing a puzzle"
return
end
puzzle = Puzzle.where(public_id: event.options["puzzle_public_id"]).first
if puzzle.nil?
event.respond content: "No puzzle found"
return
end
text = puzzle.text
new_text = puzzle.gsub(Regex.compile(event.options["find_regex"]), new_text)
puzzle.text = new_text
if puzzle.message_id
# TODO:
event.respond content: "Updated DB; Editing the message isn't supported yet"
else
event.respond content: "Updated content for puzzle #{puzzle.display_id}."
end
end
# /preview-puzzle <public_id>
define_command "Preview Puzzle", [

View File

@ -36,10 +36,10 @@ class SendPuzzleJob < ActiveJob::Base
if puzzle.present?
# Send the new puzzle
# msg = $puzzle_channel.send puzzle.get_message_text
puzzle.get_message_text.each {|t| $puzzle_channel.send t }
msgs = puzzle.get_message_text.map {|t| $puzzle_channel.send t }
# Save message id
# puzzle.message_id = msg.id
# Save puzzle text message id; really we should save all of them
puzzle.message_id = msgs.second.id
# Mark it as current
puzzle.state = "current"
# Save