Add Puzzle public id and submission instructions to puzzle message

This commit is contained in:
Matthias Portzel 2024-03-09 18:53:57 -05:00
parent c89f9cc84b
commit 85f57be385
2 changed files with 53 additions and 48 deletions

93
main.rb
View File

@ -149,6 +149,53 @@ define_command("Add Puzzle", []) do |event|
event.edit_response content: "DM'd you instructions!"
end
# Listen for DMs and treat them as puzzle submissions
$bot.message do |event|
# If this is a DM
next unless event.channel == event.user.dm
toml_text = event.content
if toml_text.starts_with? "```" and toml_text.ends_with "```"
toml_text = toml_text.sub(/\A```(?:toml)?/, "").sub(/```\z/, "").strip
end
# parse toml
begin
puzzle_data = TomlRB.parse(toml_text)
rescue => TomlRB::ParseError
# if toml parse fails, react to the message with ?
event.message.react("\u2753")
# Continue waiting for messages
next
end
# validate toml fields, sending error messages
unless puzzle_data.has_key? "puzzles" and puzzle_data["puzzles"].is_a? Array
event.channel.send "Please include your puzzle submission(s) in the `puzzles` Array.\n=> https://toml.io/en/v1.0.0#array-of-tables"
next
end
puzzle_data["puzzles"].each do |puzzle|
unless event.user == $me
# If it's not me, validate the puzzle
unless puzzle.has_key? "text" and (puzzle.has_key? "check_solution" or puzzle.has_key? "solution") and puzzle.keys.length == 2
event.channel.send "Invalid puzzle. Please include only the `text` and `solution` keys."
next
end
end
# create and save the puzzle object
p = Puzzle.new puzzle
unless p.valid?
event.channel.send "Invalid Puzzle. IDK why"
next
end
# TODO: save the user's id who submitted it
p.save
event.channel.send "Successfully added puzzle!"
end
end
# /irb-discord
$irb_sessions = []
@ -191,52 +238,6 @@ $bot.message(from: $me) do |event|
end
end
# Listen for DMs and treat them as puzzle submissions
$bot.message do |event|
# If this is a DM
next unless event.channel == event.user.dm
toml_text = event.content
if toml_text.starts_with? "```" and toml_text.ends_with "```"
toml_text = toml_text.sub(/\A```(?:toml)?/, "").sub(/```\z/, "").strip
end
# parse toml
begin
puzzle_data = TomlRB.parse(toml_text)
rescue => TomlRB::ParseError
# if toml parse fails, react to the message with ?
event.message.react("\u2753")
# Continue waiting for messages
next
end
# validate toml fields, sending error messages
unless puzzle_data.has_key? "puzzles" and puzzle_data["puzzles"].is_a? Array
event.channel.send "Please include your puzzle submission(s) in the `puzzles` Array.\n=> https://toml.io/en/v1.0.0#array-of-tables"
next
end
puzzle_data["puzzles"].each do |puzzle|
unless event.user == $me
# If it's not me, validate the puzzle
unless puzzle.has_key? "text" and (puzzle.has_key? "check_solution" or puzzle.has_key? "solution") and puzzle.keys.length == 2
event.channel.send "Invalid puzzle. Please include only the `text` and `solution` keys."
next
end
end
# create and save the puzzle object
p = Puzzle.new puzzle
unless p.valid?
event.channel.send "Invalid Puzzle. IDK why"
next
end
p.save
event.channel.send "Successfully added puzzle!"
end
end
# Get ready to send the next puzzle at the next UTC sunday
SendPuzzleJob.new.enqueue(wait_until: Date.today.next_occurring(:sunday).beginning_of_day)
# TODO: We need to run this every Sunday, not just the next one

View File

@ -14,16 +14,20 @@ class SendPuzzleJob < ActiveJob::Base
# Set the previous puzzle as used
current_puzzle = Puzzle.current.first
current_puzzle.state = "used"
last_public_id = current_puzzle.public_id
current_puzzle.save
# Get a random puzzle
# TODO: order puzzles by public_id first I guess
puzzle = Puzzle.ready.order("RANDOM()").first
if puzzle.present?
# Set its public id
puzzle.public_id = last_public_id + 1
# Send the new puzzle
# TODO: puzzle public id to 0x + hex
msg = $puzzle_channel.send puzzle.text
# TODO: Submit your answer with /solve
msg = $puzzle_channel.send "Puzzle 0x#{puzzle.public_id.to_s(16)}\n" + puzzle.text + "\nSubmit your answer with /solve in <#1140124119174754429>."
# TODO: If you have an idea for a future puzzle, use /add-puzzle
# Save message id