From e59221b9add5c970556d1b8e726ab070c4bcb5a6 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Fri, 15 Aug 2025 17:03:12 -0400 Subject: [PATCH] Add command architecture --- src/commands.rb | 4 +++ src/commands/command.rb | 47 +++++++++++++++++++++++++++++ src/commands/help_command.rb | 16 ++++++++++ src/commands/lesson_command.rb | 18 +++++++++++ src/commands/stop_lesson_command.rb | 18 +++++++++++ src/ika.rb | 39 +++++++++++++++--------- 6 files changed, 127 insertions(+), 15 deletions(-) create mode 100644 src/commands.rb create mode 100644 src/commands/command.rb create mode 100644 src/commands/help_command.rb create mode 100644 src/commands/lesson_command.rb create mode 100644 src/commands/stop_lesson_command.rb diff --git a/src/commands.rb b/src/commands.rb new file mode 100644 index 0000000..68965b2 --- /dev/null +++ b/src/commands.rb @@ -0,0 +1,4 @@ +require_relative "commands/command" +require_relative "commands/help_command" +require_relative "commands/lesson_command" +require_relative "commands/stop_lesson_command" diff --git a/src/commands/command.rb b/src/commands/command.rb new file mode 100644 index 0000000..cfdc7b6 --- /dev/null +++ b/src/commands/command.rb @@ -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 diff --git a/src/commands/help_command.rb b/src/commands/help_command.rb new file mode 100644 index 0000000..aa9501f --- /dev/null +++ b/src/commands/help_command.rb @@ -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 diff --git a/src/commands/lesson_command.rb b/src/commands/lesson_command.rb new file mode 100644 index 0000000..2b849a6 --- /dev/null +++ b/src/commands/lesson_command.rb @@ -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 diff --git a/src/commands/stop_lesson_command.rb b/src/commands/stop_lesson_command.rb new file mode 100644 index 0000000..ee0d571 --- /dev/null +++ b/src/commands/stop_lesson_command.rb @@ -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 diff --git a/src/ika.rb b/src/ika.rb index 78a10bd..788b0d3 100644 --- a/src/ika.rb +++ b/src/ika.rb @@ -1,5 +1,6 @@ require_relative './session' - require_relative './lesson' +require_relative './lesson' +require_relative './commands' class Ika attr_reader :bot @@ -24,25 +25,33 @@ class Ika bot.run # you can run this in the background, idk end - def end! + def stop! bot.remove_handler @message_handler if @message_handler end - def handle_message message - if message.message.content == "!init" - return message.respond("There's already a session running") unless current_session.nil? + def current_lesson_running? + current_lesson.running? + end - 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? + def start_lesson!(message) + current_lesson.start!(message.channel) + end - 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? + def stop_current_lesson! + current_lesson.stop! + end - message.respond("Ending the current lesson!") - return 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" + return message.respond("There's already a session running") unless current_session.nil? + + return current_session = Session.new(message, event.message.author) + end end end