ika_bot/lib/ika/plugin/lesson_plugin.rb

79 lines
1.6 KiB
Ruby
Raw Normal View History

2025-08-23 20:27:45 -04:00
require_relative "../../lesson"
class Plugin
class LessonPlugin < Plugin
attr_reader :lessons
def initialize(ika)
super
@lessons = {}
end
def handle_message(message)
super
Message.new(self, message).handle
end
def can_handle_message?(message)
return true if message.content == "!lesson"
return true if message.content == "!stoplesson"
2025-08-24 08:36:10 -04:00
lessons[message.channel.id]&.can_handle_message?(message)
2025-08-23 20:27:45 -04:00
end
class Message
attr_reader :plugin, :message
def initialize(plugin, message)
@plugin = plugin
@message = message
end
def content
message.content
end
def responder
message.responder
end
def respond(response)
responder.call response
end
def handle
return handle_lesson if content == "!lesson"
return handle_stoplesson if content == "!stoplesson"
lesson.handle_message(message)
end
def lesson
lsn = plugin.lessons[message.channel.id]
return lsn if lsn
lsn = Lesson.new responder: responder
plugin.lessons[message.channel.id] = lsn
end
def handle_lesson
if lesson.running?
respond "There's already a lesson running!"
else
respond "Starting a lesson now!"
lesson.start!
end
end
def handle_stoplesson
if lesson.running?
respond "Ending the current lesson"
lesson.stop!
else
respond "There's no lesson running"
end
end
end
end
end