Compare commits

...

10 Commits

4 changed files with 60 additions and 21 deletions

View File

@ -1,9 +1,4 @@
# Puzzle Format
Linoleum Club Bot
This is out of date, see db.rb or 0001_create_puzzles
I hereby dedicate all copyright in this project to Jesus Christ of Nazareth, the Son of God Most High.
text, a string with the text of the puzzle
solution, a string to compare
check_solution, a string; a ruby expression that will evaluate to true or false. `discord_username` and `submission` are in scope.
One of solution or check_solution must be present.

13
db.rb
View File

@ -69,13 +69,24 @@ class Puzzle < Sequel::Model
# The text that will be sent for this puzzle's message
def get_message_text
return "**Puzzle 0x#{public_id.to_s(16)}**\n#{text}\nSubmit 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. <@&#{$puzzle_pings_role.id}>"
# 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#{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."
]
end
# Gets this puzzle's Discord message ID (cached)
def message
@message ||= $puzzle_channel.load_message message_id
end
def display_id
return public_id.to_s(16)
end
end

52
main.rb
View File

@ -55,7 +55,11 @@ def define_command text, options
$bot.application_command command_name do |event|
p "Command #{command_name} triggered"
yield event
begin
yield event
rescue => err
$study_channel.send "```rb\n#{PP.pp(err, "")}\n#{PP.pp(err.backtrace.join("\n"), "")}\n```"
end
end
end
@ -170,7 +174,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!"
@ -181,7 +185,7 @@ end
# /ping-opt-out
define_command("Ping Opt Out", []) do |event|
event.user.remove_role $puzzle_pings_role
event.respond "Removed your ping role", ephemeral: true
event.respond content: "Removed your ping role", ephemeral: true
end
# Listen for member joins and add the $puzzle_pings_role, wa-ha-ha!
$bot.member_join do |event|
@ -194,7 +198,7 @@ end
# There's probably a better way to do this re-loading
define_command "Remove commands", [] do |event|
if event.user != $me
event.respond "No you did-aint!", ephemeral: false
event.respond content: "No you did-aint!", ephemeral: false
break
end
p "Removing all commands"
@ -213,33 +217,61 @@ define_command "Solve for", [
{ type: :string, name: "user_discord_id", description: "The Discord ID of the user", required: true }
] do |event|
if event.user != $me
event.respond "This is an admin command to add the solved role to other people"
event.respond content: "This is an admin command to add the solved role to other people"
return
end
discord_id = event.options["user_discord_id"]
member = $server.member(discord_id)
if member.nil?
event.respond "No member with that ID"
event.respond content: "No member with that ID"
end
current_puzzle = Puzzle.current.first
# This is a copy-paste of in /solve, but it's simple for now
event.user.add_role $puzzle_solver_role
member.add_role $puzzle_solver_role
# Create "solve" instance
Solve.create discord_id: event.user.id, puzzle_id: current_puzzle.id
# TODO: Update "solves" count role
event.respond "Added solved info for user"
event.respond content: "Added solved info for user"
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 = text.gsub(Regexp.compile(event.options["find_regex"]), new_text)
puzzle.text = new_text
puzzle.save
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", [
{type: :string, name: "public_id", description: "The puzzle you want to see must have been assigned a public_id.", require: true}
] do |event|
if event.user != $me
event.respond "No", ephemeral: false
event.respond content: "No", ephemeral: false
break
end
@ -250,7 +282,7 @@ define_command "Preview Puzzle", [
break
end
event.channel.send puzzle.get_message_text
puzzle.get_message_text.each { |t| event.channel.send t }
event.respond content: "Puzzle text sent", ephemeral: true
end

View File

@ -35,10 +35,11 @@ class SendPuzzleJob < ActiveJob::Base
# Check if we found a puzzle
if puzzle.present?
# Send the new puzzle
msg = $puzzle_channel.send puzzle.get_message_text
# msg = $puzzle_channel.send puzzle.get_message_text
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