Add lessons

This commit is contained in:
Bill Rossi 2025-08-14 20:02:00 -04:00
parent f70a16ec43
commit 4304fbfe41
2 changed files with 48 additions and 0 deletions

10
ika.rb
View File

@ -1,17 +1,27 @@
require 'discordrb' require 'discordrb'
require_relative './session' require_relative './session'
require_relative './lesson'
token = File.read("./token") token = File.read("./token")
bot = Discordrb::Bot.new(token:) bot = Discordrb::Bot.new(token:)
current_session = nil current_session = nil
current_lesson = Lesson.new
bot.message do |event| bot.message do |event|
if event.message.content == "!init" if event.message.content == "!init"
next event.respond("There's already a session running") unless current_session.nil? next event.respond("There's already a session running") unless current_session.nil?
next current_session = Session.new(event, event.message.author) next current_session = Session.new(event, event.message.author)
elsif event.message.content == "!lesson"
next event.respond("There's already a lesson running") if current_lesson.running?
event.respond("Starting a lesson now!")
next current_lesson.start!(event.channel)
elsif event.message.content == "!stoplesson"
next event.respond("There's no lesson running") unless current_lesson.running?
event.respond("Ending the current lesson!")
next current_lesson.stop!
end end
current_session&.respond_to event current_session&.respond_to event

38
lesson.rb Normal file
View File

@ -0,0 +1,38 @@
DEFAULT_LESSON_PLAN = [
[15, "login and catch up on the week"],
[45, "review chapter vocab and grammar"],
[15, "answer Seth Clydesdale Genki review questions"],
[15, "ask closing questions and have Japanese conversation"],
]
class Lesson
attr_reader :lesson_plan, :channel, :thread, :running
def initialize(lesson_plan=DEFAULT_LESSON_PLAN)
@lesson_plan = lesson_plan
@running = false
end
def stop!
thread.kill if thread
@running = false
end
def start!(channel)
stop!
@channel = channel
@thread = Thread.new do
@running = true
lesson_plan.each do |time, message|
channel.send_message "#{time} minutes to #{message}"
sleep(time * 60)
end
channel.send_message "Lesson over!"
@running = false
end
end
def running?
running
end
end