Class: Muzang::Plugins::PlusOne

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/muzang-plugins/muzang-plusone.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ PlusOne

Returns a new instance of PlusOne.



11
12
13
14
# File 'lib/muzang-plugins/muzang-plusone.rb', line 11

def initialize(bot)
  @bot = bot
  create_database("stats.yml", Hash.new, :stats)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/muzang-plugins/muzang-plusone.rb', line 9

def config
  @config
end

#statsObject

Returns the value of attribute stats.



9
10
11
# File 'lib/muzang-plugins/muzang-plusone.rb', line 9

def stats
  @stats
end

Instance Method Details

#call(connection, message) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/muzang-plugins/muzang-plusone.rb', line 16

def call(connection, message)
  on_channel(message) do
    match(message, /^([^\s]*) \+1/) do |plus_for|
      plus_for = plus_for[1]
      plus_for.gsub!(":","")
      if filter(plus_for, message.nick)
        connection.msg(message.channel, "#{message.nick} write in PHP") and return
      end

      connection.msg(message.channel, "#{message.nick} gave +1 for *#{plus_for}*")
      @stats[plus_for] ||= 0
      @stats[plus_for]  += 1
      save
    end

    match(message, /^!stats$/) do
      connection.msg(message.channel, print)
    end
  end
end

#filter(plus_for, nick) ⇒ Object



47
48
49
50
51
# File 'lib/muzang-plugins/muzang-plusone.rb', line 47

def filter(plus_for, nick)
  if plus_for == nick || @stats[nick] == nil
    return true
  end
end


37
38
39
40
41
42
43
44
45
# File 'lib/muzang-plugins/muzang-plusone.rb', line 37

def print
  message = ""
  stat = @stats.sort_by { |points| -points[1] }
  stat.each do |s|
    message << "*#{s[0]}* #{s[1]} | " if s[1] > 0
  end

  message
end