Class: IRC::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/irc/bot.rb

Direct Known Subclasses

NewBot

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Bot

Returns a new instance of Bot.



5
6
7
# File 'lib/irc/bot.rb', line 5

def initialize message
  @message = message
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



46
47
48
# File 'lib/irc/bot.rb', line 46

def method_missing method_name, *args
  message.public_send method_name, *args
end

Class Attribute Details

.channelsObject

Returns the value of attribute channels.



51
52
53
# File 'lib/irc/bot.rb', line 51

def channels
  @channels
end

.host(_host = nil) ⇒ Object

Returns the value of attribute host.



51
52
53
# File 'lib/irc/bot.rb', line 51

def host
  @host
end

.nick(_nick = nil) ⇒ Object

Returns the value of attribute nick.



51
52
53
# File 'lib/irc/bot.rb', line 51

def nick
  @nick
end

.port(_port = nil) ⇒ Object

Returns the value of attribute port.



51
52
53
# File 'lib/irc/bot.rb', line 51

def port
  @port
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



3
4
5
# File 'lib/irc/bot.rb', line 3

def message
  @message
end

Class Method Details

.channel(*_channels) ⇒ Object



93
94
95
96
97
98
# File 'lib/irc/bot.rb', line 93

def channel *_channels
  _channels.each do |c|
    puts "channel: '#{c}'"
    (@channels ||= []) << c
  end
end

.match(regex, &block) ⇒ Object



100
101
102
# File 'lib/irc/bot.rb', line 100

def match regex, &block
  on(:privmsg, regex, &block)
end

.mention_match(regex, &block) ⇒ Object



104
105
106
107
# File 'lib/irc/bot.rb', line 104

def mention_match regex, &block
  r = /^#{@nick}[,: ]*/ + regex
  on(:privmsg, r, &block)
end

.on(action, regex = nil, &block) ⇒ Object



109
110
111
# File 'lib/irc/bot.rb', line 109

def on action, regex = nil, &block
  Callback.add(action, regex, self, &block)
end

.reset!Object



64
65
66
67
# File 'lib/irc/bot.rb', line 64

def reset!
  Callback.clear!
  @channels = []
end

.start!Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/irc/bot.rb', line 53

def start!
  @connection ||= Connection.new @host, @port

  on(:ping) { connection.pong params }

  @connection.nick @nick
  @connection.join @channels

  @connection.listen
end

.store(_store, options = nil) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/irc/bot.rb', line 84

def store _store, options = nil
  require 'irc/store'

  case _store
  when :redis
    IRC::Store.options = options
  end
end

Instance Method Details

#conjugate(noun, lookup = nil) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/irc/bot.rb', line 37

def conjugate noun, lookup = nil
  lookup ||= {
    'me'  => nick, 
    'you' => self.class.nick,
    nick  => 'you'
  }
  noun.gsub /[\w]+/i, lookup
end

#reply(something, mention = true) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/irc/bot.rb', line 17

def reply something, mention = true
  if message.middle == self.class.nick
    say something, message.nick
  elsif mention
    say "#{message.nick}, #{something}", message.middle
  else
    say something, message.middle
  end
end

#say(something, recipients = nil) ⇒ Object



13
14
15
# File 'lib/irc/bot.rb', line 13

def say something, recipients = nil
  message.connection.privmsg something, recipients || self.class.channels
end

#stopObject



9
10
11
# File 'lib/irc/bot.rb', line 9

def stop
  Thread.current.kill
end

#store(namespace = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/irc/bot.rb', line 27

def store namespace = nil
  @store ||= IRC::Store.store

  if namespace
    (@namespace ||= {})[namespace] ||= Redis::Namespace.new(namespace, redis: @store)
  else
    @store
  end
end