Come up with a basic plugin outline
This commit is contained in:
parent
f6839fe42a
commit
f72eeb3528
32
lib/drill.rb
Normal file
32
lib/drill.rb
Normal file
@ -0,0 +1,32 @@
|
||||
class Drill
|
||||
attr_reader :channel
|
||||
|
||||
def initialize(channel)
|
||||
@channel = channel
|
||||
@started = false
|
||||
end
|
||||
|
||||
def start!
|
||||
@started = true
|
||||
end
|
||||
|
||||
def started?
|
||||
@started
|
||||
end
|
||||
|
||||
def exists?
|
||||
true
|
||||
end
|
||||
|
||||
class Empty < Drill
|
||||
def initialize
|
||||
super(nil)
|
||||
end
|
||||
|
||||
def start!; end
|
||||
|
||||
def exists?
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
73
lib/drill_plugin.rb
Normal file
73
lib/drill_plugin.rb
Normal file
@ -0,0 +1,73 @@
|
||||
require_relative "drill"
|
||||
|
||||
class DrillPlugin
|
||||
COMMANDS = %w[
|
||||
init
|
||||
start
|
||||
end
|
||||
]
|
||||
|
||||
attr_reader :bot, :drills
|
||||
|
||||
def initialize(bot)
|
||||
@bot = bot
|
||||
@drills = Hash.new { Drill::Empty.new }
|
||||
end
|
||||
|
||||
def handle_message(message)
|
||||
puts "[#{self.class.name}] Handling \"#{message.content}\" from #{message.author.display_name}"
|
||||
command = strip_prefix(message.content).split.first
|
||||
p command
|
||||
if command.nil? || command == "" || command == "help" || command == "!drill"
|
||||
handle_help message
|
||||
elsif COMMANDS.include? command
|
||||
drill = drills[message.channel.id]
|
||||
send("handle_#{command}", message, drill)
|
||||
else
|
||||
message.respond "Unknown command #{command}"
|
||||
end
|
||||
end
|
||||
|
||||
def handle_init(message, drill)
|
||||
if drill.exists?
|
||||
message.respond "You've already got a drill running in this channel"
|
||||
else
|
||||
drills[message.channel.id] = Drill.new message.channel
|
||||
message.respond "New drill created!"
|
||||
end
|
||||
end
|
||||
|
||||
def handle_start(message, drill)
|
||||
if drill.started?
|
||||
message.respond "This drill has already started!"
|
||||
elsif drill.exists?
|
||||
drill.start!
|
||||
message.respond "Drill started!"
|
||||
else
|
||||
message.respond "You have to `!drill init` before you can start the drill"
|
||||
end
|
||||
end
|
||||
|
||||
def handle_end(message, drill)
|
||||
if !drill.exists?
|
||||
message.respond "There's no drill to end"
|
||||
elsif drill.started?
|
||||
drills.delete message.channel.id
|
||||
message.respond "Ending the drill now!"
|
||||
else
|
||||
message.respond "You have to `!drill start` before you can end the drill"
|
||||
end
|
||||
end
|
||||
|
||||
def handle_help(message)
|
||||
message.respond "Insert help message here"
|
||||
end
|
||||
|
||||
def strip_prefix(content)
|
||||
if content.start_with? "!drill"
|
||||
content.sub("!drill ", "")
|
||||
else
|
||||
content
|
||||
end
|
||||
end
|
||||
end
|
@ -1,11 +1,13 @@
|
||||
require_relative './session'
|
||||
require_relative './lesson'
|
||||
require_relative './commands'
|
||||
require_relative './drill_plugin'
|
||||
|
||||
class Ika
|
||||
attr_reader :bot
|
||||
attr_reader :lessons, :sessions
|
||||
attr_accessor :current_lesson, :current_session
|
||||
attr_reader :drill_plugin
|
||||
|
||||
def initialize(bot)
|
||||
@bot = bot
|
||||
@ -13,6 +15,7 @@ class Ika
|
||||
@sessions = {}
|
||||
@current_session = nil
|
||||
@current_lesson = Lesson.new
|
||||
@drill_plugin = DrillPlugin.new(self)
|
||||
end
|
||||
|
||||
def start!
|
||||
@ -46,6 +49,10 @@ class Ika
|
||||
command.execute
|
||||
end
|
||||
|
||||
if message.content.start_with? "!drill"
|
||||
drill_plugin.handle_message message
|
||||
end
|
||||
|
||||
return current_session&.respond_to message
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user