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



77
78
79
80
81
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 77

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], :frequency => opts[:frequency]}]).uniq
end

.commandsObject



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

def self.commands
  [{:command => "list calls", :description => "List current calls and their responses", :method => "list"},
   {:command => "add call", :description => "Add a 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 => "add random call", :description => "Add a response to display a phrase X of every 10 times, split by a | - '!add random call 5 I see what you did there... | You caught me! | Oh no I'm busted!'", :method => "add_random"},
   {:command => "remove call", :description => "Remove a call and response by call '!remove call I see what you did there...'", :method => "remove"}]
end

.descriptionObject



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

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

.listObject



73
74
75
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 73

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

.nameObject



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

def self.name
  "Call and Response"
end

.public?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 32

def self.public?
  false
end

.remove(call) ⇒ Object



83
84
85
86
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 83

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



48
49
50
51
52
53
54
55
56
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 48

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 | - '!add call When someone says this | I say this | or this'"
  end
end

#add_randomObject



58
59
60
61
62
63
64
65
66
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 58

def add_random
  message = body_without_nick_or_command("add random call")
  x, frequency, call, *response = message.split(/(\d)\s+([^|]+)\|\s+(.+)/)
  if self.class.add(:call => call.strip,:response => response.map(&:strip), :frequency => frequency.to_i)
    reply "Call and Response added."
  else
    reply "Unable to add that call. A frequency, call, and response are required, split by a | - '!add random call 5 When someone says this | I say this | or this'"
  end
end

#handleObject



4
5
6
7
8
9
10
11
12
13
14
15
# 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) }
      frequency = match[:frequency] || 1
      if Kernel.rand(frequency)+1 == frequency
        response = [match[:response]].flatten # Support old responses which are strings, not arrays
        reply response[Kernel.rand(response.size)]
      end
      return nil
    end
  end
end

#listObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ceiling_cat/plugins/call_and_response.rb', line 36

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 responses set up yet! You should add one with '!add call When someone says this | I say this | or this'"
  end
  reply messages
end

#removeObject



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

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