Module: Lobstersbot::PluggableConnection

Defined in:
lib/lobstersbot/pluggable_connection.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@triggers =
[
  # The default command handler.
  [
    0,
    /\A\.(?<command>[a-z]+)\s(?<message>.+)\z/i,
    lambda do |bot, channel, nick, match|
      bot.evaluate(:"on_#{match[:command]}", channel, nick, match[:message])
    end,
  ],
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



18
19
20
# File 'lib/lobstersbot/pluggable_connection.rb', line 18

def self.included(mod)
  mod.extend(ClassMethods)
end

.triggersObject



14
15
16
# File 'lib/lobstersbot/pluggable_connection.rb', line 14

def self.triggers
  @@triggers
end

Instance Method Details

#channel_message(sender, channel, message) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lobstersbot/pluggable_connection.rb', line 42

def channel_message(sender, channel, message)
  match = nil

  _, _, handler = @@triggers.find do |trigger|
    match = message.match(trigger[1])
    !match.nil?
  end
  return unless match

  handler.call(self, channel, sender[:nick], match)
end

#did_start_upObject



32
33
34
35
36
37
38
39
40
# File 'lib/lobstersbot/pluggable_connection.rb', line 32

def did_start_up
  @memory = PStore.new(config_dir('memory.pstore'), true)

  # Sort the triggers by priority.
  @@triggers.sort! {|a, b| b[0] <=> a[0] }

  timer = Concurrent::TimerTask.new(execution_interval: 1) { evaluate(:frequently) }
  timer.execute
end

#evaluate(group, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lobstersbot/pluggable_connection.rb', line 59

def evaluate(group, *args)
  matching = public_methods.select {|m| m.to_s.start_with?(group.to_s) }

  matching.each do |match|
    slice_name = match.to_s.sub("#{group}_", '').to_sym
    @memory.transaction do
      slice = @memory[slice_name] ||= {}
      self.send(match, slice, *args)
    end
  end
end

#join_event(sender, channel) ⇒ Object



54
55
56
57
# File 'lib/lobstersbot/pluggable_connection.rb', line 54

def join_event(sender, channel)
  response_proc = ->(msg) { privmsg("#{sender[:nick]}: #{msg}", channel) }
  evaluate(:seen, sender[:nick], response_proc)
end

#respond(channel, nick, msg) ⇒ Object



28
29
30
# File 'lib/lobstersbot/pluggable_connection.rb', line 28

def respond(channel, nick, msg)
  privmsg("#{nick}: #{msg}", channel)
end