Class: CeilingCat::Plugin::CallAndResponse

Inherits:
Base
  • Object
show all
Defined in:
lib/ceiling_cat/plugins/call_and_response.rb

Instance Attribute Summary

Attributes inherited from Base

#event

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#body, #body_without_command, #body_without_nick, #body_without_nick_or_command, #commands, #initialize, #pluralize, #reply, #room, #store, store, #user, #words

Constructor Details

This class inherits a constructor from CeilingCat::Plugin::Base

Class Method Details

.add(opts = {}) ⇒ Object



63
64
65
66
67
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 63

def self.add(opts={})
  return false unless opts[:call] && opts[:response]
  store["call_and_responses"] ||= []
  store["call_and_responses"] = (store["call_and_responses"] + [{:call => opts[:call], :response => opts[:response]}]).uniq
end

.commandsObject



14
15
16
17
18
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 14

def self.commands
  [{:command => "list calls", :description => "List current calls and their responses", :method => "list"},
   {:command => "add call", :description => "Add an response to display when a phrase is said, split by a | - '!add call I see what you did there... | You caught me! | Oh no I'm busted!'", :method => "add"},
   {:command => "remove call", :description => "Remove an call and response by call '!remove call I see what you did there...'", :method => "remove"}]
end

.descriptionObject



20
21
22
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 20

def self.description
  "Watches for certain phrases and inserts a relevant image"
end

.listObject



59
60
61
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 59

def self.list
  store["call_and_responses"] ||= []
end

.nameObject



24
25
26
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 24

def self.name
  "Call and Response"
end

.public?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 28

def self.public?
  false
end

.remove(call) ⇒ Object



69
70
71
72
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 69

def self.remove(call)
  store["call_and_responses"] ||= []
  store["call_and_responses"] = store["call_and_responses"].reject{|car| car[:call].downcase == call.downcase }
end

Instance Method Details

#addObject



44
45
46
47
48
49
50
51
52
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 44

def add
  message = body_without_nick_or_command("add call")
  call, *response = message.split("|")
  if self.class.add(:call => call.strip,:response => response.map(&:strip))
    reply "Call and Response added."
  else
    reply "Unable to add that call. A call and a response are required, split by a | - 'When someones says this | I say this | or this'"
  end
end

#handleObject



4
5
6
7
8
9
10
11
12
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 4

def handle
  if !super && event.type == :chat
    if match = self.class.list.find{|car| body =~ Regexp.new(car[:call],true) }
      response = [match[:response]].flatten # Support old responses which are strings, not arrays
      reply response[Kernel.rand(response.size)]
      return nil
    end
  end
end

#listObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 32

def list
  messages = []
  store["call_and_responses"] ||= []
  if store["call_and_responses"].size > 0
    messages << "Current Calls and Responses"
    messages += store["call_and_responses"].collect{|car| "-- #{car[:call]} | #{[car[:response]].flatten.join(" | ")}"} # Support old responses which are strings, not arrays
  else
    messages << "There are no call and respones set up yet! You should add one with '!add call When someone says this | I say this | or this'"
  end
  reply messages
end

#removeObject



54
55
56
57
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 54

def remove
  self.class.remove(body_without_nick_or_command("remove call"))
  reply "Call removed."
end