Add command architecture

This commit is contained in:
Bill Rossi 2025-08-15 17:03:12 -04:00
parent be2b905ddb
commit e59221b9ad
6 changed files with 127 additions and 15 deletions

4
src/commands.rb Normal file
View File

@ -0,0 +1,4 @@
require_relative "commands/command"
require_relative "commands/help_command"
require_relative "commands/lesson_command"
require_relative "commands/stop_lesson_command"

47
src/commands/command.rb Normal file
View File

@ -0,0 +1,47 @@
module Commands
@@commands = []
def self.register_command(command_class)
@@commands << command_class
end
def self.registered_commands
@@commands
end
class Command
def self.inherited(klass)
p klass
Commands.register_command klass
end
def self.for(ika, message)
Commands.registered_commands.find do |command_class|
command_class.matches? message.message.content
end&.new(ika, message)
end
def self.matches?(message_text)
message_text == "!#{command_string}"
end
def self.help
"!#{command_string} - #{description}"
end
def self.description
"blah blah blah"
end
attr_reader :ika, :message
def initialize(ika, message)
@ika = ika
@message = message
end
def execute
puts "This is a generic command! I can't execute anything!"
end
end
end

View File

@ -0,0 +1,16 @@
module Commands
class Help < Command
def self.command_string
"help"
end
def self.description
"Shows you a list of available commands"
end
def execute
message.respond ["I'm Ika! Here are the commands I know:",
*Commands.registered_commands.map(&:help)].join("\n")
end
end
end

View File

@ -0,0 +1,18 @@
module Commands
class LessonCommand < Command
def self.command_string
"lesson"
end
def self.description
"Starts timers for a lesson"
end
def execute
return message.respond("There's already a lesson running!") if ika.current_lesson_running?
message.respond("Starting a lesson now!")
ika.start_lesson! message
end
end
end

View File

@ -0,0 +1,18 @@
module Commands
class StopLessonCommand < Command
def self.command_string
"stoplesson"
end
def self.description
"Stops the current lesson, if one is running"
end
def execute
return message.respond("There's no lesson running") unless ika.current_lesson_running?
message.respond("Ending the current lesson!")
ika.stop_current_lesson!
end
end
end

View File

@ -1,5 +1,6 @@
require_relative './session' require_relative './session'
require_relative './lesson' require_relative './lesson'
require_relative './commands'
class Ika class Ika
attr_reader :bot attr_reader :bot
@ -24,25 +25,33 @@ class Ika
bot.run # you can run this in the background, idk bot.run # you can run this in the background, idk
end end
def end! def stop!
bot.remove_handler @message_handler if @message_handler bot.remove_handler @message_handler if @message_handler
end end
def handle_message message def current_lesson_running?
current_lesson.running?
end
def start_lesson!(message)
current_lesson.start!(message.channel)
end
def stop_current_lesson!
current_lesson.stop!
end
def handle_message(message)
if command = Commands::Command.for(self, message)
command.execute
end
return current_session&.respond_to message
if message.message.content == "!init" if message.message.content == "!init"
return message.respond("There's already a session running") unless current_session.nil? return message.respond("There's already a session running") unless current_session.nil?
return current_session = Session.new(message, event.message.author) return current_session = Session.new(message, event.message.author)
elsif message.message.content == "!lesson"
return message.respond("There's already a lesson running") if current_lesson.running?
message.respond("Starting a lesson now!")
return current_lesson.start!(message.channel)
elsif message.message.content == "!stoplesson"
return message.respond("There's no lesson running") unless current_lesson.running?
message.respond("Ending the current lesson!")
return current_lesson.stop!
end end
end end
end end