muzang-plugins 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,25 @@
1
1
  require 'muzang/version'
2
2
 
3
- class Motd
4
- include Muzang::Plugins::Helpers
3
+ module Muzang
4
+ module Plugins
5
+ class Motd
6
+ include Muzang::Plugins::Helpers
5
7
 
6
- def initialize(bot)
7
- @bot = bot
8
- end
8
+ def initialize(bot)
9
+ @bot = bot
10
+ end
9
11
 
10
- def call(connection, message)
11
- on_join(connection, message) do
12
- connection.msg(message.channel, "Muzang | Version: #{Muzang::VERSION} | Plugins: #{plugins}")
13
- end
14
- end
12
+ def call(connection, message)
13
+ on_join(connection, message) do
14
+ connection.msg(message.channel, "Muzang | Version: #{Muzang::VERSION} | Plugins: #{plugins}")
15
+ end
16
+ end
15
17
 
16
- def plugins
17
- list = ""
18
- @bot.plugins.each{|plugin, instance| list << "*#{plugin}* "}
19
- list
18
+ def plugins
19
+ list = ""
20
+ @bot.plugins.each{|plugin, instance| list << "*#{plugin}* "}
21
+ list
22
+ end
23
+ end
20
24
  end
21
25
  end
@@ -1,93 +1,97 @@
1
1
  require "json"
2
2
 
3
- class NerdPursuit
4
- include Muzang::Plugins::Helpers
3
+ module Muzang
4
+ module Plugins
5
+ class NerdPursuit
6
+ include Muzang::Plugins::Helpers
5
7
 
6
- attr_accessor :questions, :quiz_time
8
+ attr_accessor :questions, :quiz_time
7
9
 
8
- def initialize(bot)
9
- @bot = bot
10
- @quiz_time = false
11
- @answers = {}
12
- create_database("nerd_pursuit.yml", Array.new, :questions)
13
- end
10
+ def initialize(bot)
11
+ @bot = bot
12
+ @quiz_time = false
13
+ @answers = {}
14
+ create_database("nerd_pursuit.yml", Array.new, :questions)
15
+ end
14
16
 
15
- def all_questions
16
- names = Dir["#{File.dirname(__FILE__)}/muzang-nerdpursuit/questions/**/*.json"]
17
- end
17
+ def all_questions
18
+ names = Dir["#{File.dirname(__FILE__)}/muzang-nerdpursuit/questions/**/*.json"]
19
+ end
18
20
 
19
- def quiz!(&block)
20
- @quiz_time = true
21
- current_question # load current question
22
- end
21
+ def quiz!(&block)
22
+ @quiz_time = true
23
+ current_question # load current question
24
+ end
23
25
 
24
- def end_quiz!
25
- @quiz_time = false
26
- @current_question = nil
27
- end
26
+ def end_quiz!
27
+ @quiz_time = false
28
+ @current_question = nil
29
+ end
28
30
 
29
- def current_question
30
- if @current_question
31
- @current_question
32
- else
33
- begin
34
- sample_question = (all_questions - questions).sample
35
- questions << sample_question
36
- save
37
- @current_question = ::JSON.parse(File.open(sample_question).read)["question"]
38
- end while(!valid?(@current_question))
39
- end
31
+ def current_question
32
+ if @current_question
33
+ @current_question
34
+ else
35
+ begin
36
+ sample_question = (all_questions - questions).sample
37
+ questions << sample_question
38
+ save
39
+ @current_question = ::JSON.parse(File.open(sample_question).read)["question"]
40
+ end while(!valid?(@current_question))
41
+ end
40
42
 
41
- @current_question
42
- end
43
+ @current_question
44
+ end
43
45
 
44
- def valid?(question)
45
- if question
46
- question["category"] and question["text"] and question["right_answer"]
47
- else
48
- false
49
- end
50
- end
46
+ def valid?(question)
47
+ if question
48
+ question["category"] and question["text"] and question["right_answer"]
49
+ else
50
+ false
51
+ end
52
+ end
51
53
 
52
- def find_winner
53
- @winner = @answers.reject do |_, answer|
54
- answer[:answer] != current_question["right_answer"][1..1]
55
- end.sort_by do |_,answer|
56
- answer[:time]
57
- end
58
- end
54
+ def find_winner
55
+ @winner = @answers.reject do |_, answer|
56
+ answer[:answer] != current_question["right_answer"][1..1]
57
+ end.sort_by do |_,answer|
58
+ answer[:time]
59
+ end
60
+ end
59
61
 
60
- def period(time)
61
- time # need for speed up tests
62
- end
62
+ def period(time)
63
+ time # need for speed up tests
64
+ end
63
65
 
64
- def call(connection, message)
65
- on_channel(message) do
66
- match(message, /^!quiz$/) do
67
- quiz!
68
- connection.msg(message.channel, "Quiz time!")
69
- EM.add_timer(period(1)) { connection.msg(message.channel, "Category: #{current_question["category"]}") }
70
- EM.add_timer(period(2)) { connection.msg(message.channel, "Question: #{current_question["text"]}") }
71
- 4.times do |time|
72
- EM.add_timer(period(2+time+1)) { connection.msg(message.channel, "Answer #{time+1}: #{current_question["a#{time+1}"]}") }
73
- end
74
- EM.add_timer(period(40)) do
75
- connection.msg(message.channel, "Right answer: #{current_question["right_answer"][1..1]}")
76
- @winner = find_winner
77
- if @winner.first && @winner.first.first
78
- connection.msg(message.channel, "The winner is... #{@winner.first.first}")
66
+ def call(connection, message)
67
+ on_channel(message) do
68
+ match(message, /^!quiz$/) do
69
+ quiz!
70
+ connection.msg(message.channel, "Quiz time!")
71
+ EM.add_timer(period(1)) { connection.msg(message.channel, "Category: #{current_question["category"]}") }
72
+ EM.add_timer(period(2)) { connection.msg(message.channel, "Question: #{current_question["text"]}") }
73
+ 4.times do |time|
74
+ EM.add_timer(period(2+time+1)) { connection.msg(message.channel, "Answer #{time+1}: #{current_question["a#{time+1}"]}") }
75
+ end
76
+ EM.add_timer(period(40)) do
77
+ connection.msg(message.channel, "Right answer: #{current_question["right_answer"][1..1]}")
78
+ @winner = find_winner
79
+ if @winner.first && @winner.first.first
80
+ connection.msg(message.channel, "The winner is... #{@winner.first.first}")
81
+ end
82
+ @answers = {}
83
+ @winner = nil
84
+ end_quiz!
85
+ end
79
86
  end
80
- @answers = {}
81
- @winner = nil
82
- end_quiz!
83
- end
84
- end
85
87
 
86
- match(message, /\d/) do |match|
87
- answer = match[0]
88
- if @quiz_time
89
- unless @answers[message.nick]
90
- @answers[message.nick] = { :answer => answer, :time => Time.now }
88
+ match(message, /\d/) do |match|
89
+ answer = match[0]
90
+ if @quiz_time
91
+ unless @answers[message.nick]
92
+ @answers[message.nick] = { :answer => answer, :time => Time.now }
93
+ end
94
+ end
91
95
  end
92
96
  end
93
97
  end
@@ -1,50 +1,54 @@
1
1
  require 'fileutils'
2
2
  require 'yaml'
3
3
 
4
- class PlusOne
5
- include Muzang::Plugins::Helpers
4
+ module Muzang
5
+ module Plugins
6
+ class PlusOne
7
+ include Muzang::Plugins::Helpers
6
8
 
7
- attr_accessor :config, :stats
9
+ attr_accessor :config, :stats
8
10
 
9
- def initialize(bot)
10
- @bot = bot
11
- create_database("stats.yml", Hash.new, :stats)
12
- end
13
-
14
- def call(connection, message)
15
- on_channel(message) do
16
- match(message, /^([^\s]*) \+1/) do |plus_for|
17
- plus_for = plus_for[1]
18
- plus_for.gsub!(":","")
19
- if filter(plus_for, message.nick)
20
- connection.msg(message.channel, "#{message.nick} write in PHP") and return
21
- end
22
-
23
- connection.msg(message.channel, "#{message.nick} gave +1 for *#{plus_for}*")
24
- @stats[plus_for] ||= 0
25
- @stats[plus_for] += 1
26
- save
11
+ def initialize(bot)
12
+ @bot = bot
13
+ create_database("stats.yml", Hash.new, :stats)
27
14
  end
28
15
 
29
- match(message, /^!stats$/) do
30
- connection.msg(message.channel, print)
16
+ def call(connection, message)
17
+ on_channel(message) do
18
+ match(message, /^([^\s]*) \+1/) do |plus_for|
19
+ plus_for = plus_for[1]
20
+ plus_for.gsub!(":","")
21
+ if filter(plus_for, message.nick)
22
+ connection.msg(message.channel, "#{message.nick} write in PHP") and return
23
+ end
24
+
25
+ connection.msg(message.channel, "#{message.nick} gave +1 for *#{plus_for}*")
26
+ @stats[plus_for] ||= 0
27
+ @stats[plus_for] += 1
28
+ save
29
+ end
30
+
31
+ match(message, /^!stats$/) do
32
+ connection.msg(message.channel, print)
33
+ end
34
+ end
31
35
  end
32
- end
33
- end
34
36
 
35
- def print
36
- message = ""
37
- stat = @stats.sort_by { |points| -points[1] }
38
- stat.each do |s|
39
- message << "*#{s[0]}* #{s[1]} | " if s[1] > 0
40
- end
37
+ def print
38
+ message = ""
39
+ stat = @stats.sort_by { |points| -points[1] }
40
+ stat.each do |s|
41
+ message << "*#{s[0]}* #{s[1]} | " if s[1] > 0
42
+ end
41
43
 
42
- message
43
- end
44
+ message
45
+ end
44
46
 
45
- def filter(plus_for, nick)
46
- if plus_for == nick || @stats[nick] == nil
47
- return true
47
+ def filter(plus_for, nick)
48
+ if plus_for == nick || @stats[nick] == nil
49
+ return true
50
+ end
51
+ end
48
52
  end
49
53
  end
50
54
  end
@@ -2,37 +2,41 @@ require "em-http-request"
2
2
  require "rss/1.0"
3
3
  require "rss/2.0"
4
4
 
5
- class Reddit
6
- include Muzang::Plugins::Helpers
7
- attr_accessor :last_update
5
+ module Muzang
6
+ module Plugins
7
+ class Reddit
8
+ include Muzang::Plugins::Helpers
9
+ attr_accessor :last_update
8
10
 
9
- def initialize(bot)
10
- @bot = bot
11
- create_database("last_update.yml", Time.now, :last_update)
12
- end
11
+ def initialize(bot)
12
+ @bot = bot
13
+ create_database("last_update.yml", Time.now, :last_update)
14
+ end
13
15
 
14
- def call(connection, message)
15
- on_join(connection, message) do
16
- EventMachine::add_periodic_timer(period) do
17
- http = EventMachine::HttpRequest.new('http://www.reddit.com/r/ruby/.rss').get
16
+ def call(connection, message)
17
+ on_join(connection, message) do
18
+ EventMachine::add_periodic_timer(period) do
19
+ http = EventMachine::HttpRequest.new('http://www.reddit.com/r/ruby/.rss').get
18
20
 
19
- http.callback {
20
- rss = RSS::Parser.parse(http.response, false)
21
- rss.items.each do |item|
22
- connection.msg(message.channel, "#{item.title} | #{item.link}") if item.date > @last_update
21
+ http.callback {
22
+ rss = RSS::Parser.parse(http.response, false)
23
+ rss.items.each do |item|
24
+ connection.msg(message.channel, "#{item.title} | #{item.link}") if item.date > @last_update
25
+ end
26
+ save(rss)
27
+ }
23
28
  end
24
- save(rss)
25
- }
29
+ end
26
30
  end
27
- end
28
- end
29
31
 
30
- def save(rss)
31
- @last_update = rss.items.max_by{|i|i.date}.date
32
- file = File.open(@config + "/last_update.yml", "w"){|f| f.write YAML::dump(@last_update)}
33
- end
32
+ def save(rss)
33
+ @last_update = rss.items.max_by{|i|i.date}.date
34
+ file = File.open(@config + "/last_update.yml", "w"){|f| f.write YAML::dump(@last_update)}
35
+ end
34
36
 
35
- def period
36
- 30
37
+ def period
38
+ 30
39
+ end
40
+ end
37
41
  end
38
42
  end
@@ -2,75 +2,93 @@ require "em-http-request"
2
2
  require "json"
3
3
  require "pstore"
4
4
 
5
- class RubyGems
6
- include Muzang::Plugins::Helpers
5
+ module Muzang
6
+ module Plugins
7
+ class RubyGems
8
+ include Muzang::Plugins::Helpers
7
9
 
8
- attr_accessor :last_gem, :store
10
+ attr_accessor :last_gem, :store
9
11
 
10
- def initialize(bot)
11
- @bot = bot
12
- @store = PStore.new("#{ENV["HOME"]}/.muzang/muzang.rubygems")
13
- @store.transaction do
14
- @store[:gems] ||= {}
15
- end
16
- end
17
-
18
- def call(connection, message)
19
- match(message, /^watch! (.*?)$/) do |match|
20
- @current_gem = match[1]
21
-
22
- @store.transaction do
23
- unless @store[:gems][@current_gem]
24
- @store[:gems][match[1]] = {:name => @current_gem}
25
- @new_gem = true
12
+ def initialize(bot)
13
+ @bot = bot
14
+ @store = PStore.new("#{ENV["HOME"]}/.muzang/muzang.rubygems")
15
+ @store.transaction do
16
+ @store[:gems] ||= {}
26
17
  end
27
18
  end
28
19
 
29
- if @new_gem
30
- http = EventMachine::HttpRequest.new("http://rubygems.org/api/v1/gems/#{@current_gem}.json").get
31
- http.callback {
32
- begin
33
- gem = JSON.parse(http.response)
34
- @store.transaction do
35
- @store[:gems][@current_gem][:version] = gem["version"]
20
+ def call(connection, message)
21
+ match(message, /^!watch (.*?)$/) do |match|
22
+ @current_gem = match[1]
23
+
24
+ @store.transaction do
25
+ unless @store[:gems][@current_gem]
26
+ @store[:gems][match[1]] = {:name => @current_gem}
27
+ @new_gem = true
36
28
  end
37
- connection.msg(message.channel, "I added gem #{@current_gem} to watchlist")
38
- connection.msg(message.channel, "Current version: #{@current_gem} (#{gem["version"]})")
39
- @new_gem = false
40
- rescue Exception
41
- connection.msg(message.channel, "Gem name is incorrect")
42
- @store.transaction{@store[:gems].delete(@current_gem)}
43
29
  end
44
- }
45
- else
46
- connection.msg(message.channel, "Gem #{@current_gem} is already observed")
47
- end
48
- end
49
30
 
50
- on_join(connection, message) do
51
- EventMachine.add_periodic_timer(period) do
52
- gems = @store.transaction{@store[:gems].values}
53
- EM::Iterator.new(gems, 1).each do |gem, iter|
54
- http = EventMachine::HttpRequest.new("http://rubygems.org/api/v1/gems/#{gem[:name]}.json").get
55
- http.callback {
56
- iter.next
57
- begin
58
- current_gem = JSON.parse(http.response)
59
- if Gem::Version.new(gem[:version]) < Gem::Version.new(current_gem["version"])
31
+ if @new_gem
32
+ http = EventMachine::HttpRequest.new("http://rubygems.org/api/v1/gems/#{@current_gem}.json").get
33
+ http.callback {
34
+ begin
35
+ gem = JSON.parse(http.response)
60
36
  @store.transaction do
61
- @store[:gems][gem[:name]][:version] = current_gem["version"]
37
+ @store[:gems][@current_gem][:version] = gem["version"]
62
38
  end
63
- connection.msg(message.channel, "New version #{gem[:name]} (#{current_gem["version"]})")
39
+ connection.msg(message.channel, "I added gem #{@current_gem} to watchlist")
40
+ connection.msg(message.channel, "Current version: #{@current_gem} (#{gem["version"]})")
41
+ @new_gem = false
42
+ rescue Exception
43
+ connection.msg(message.channel, "Gem name is incorrect")
44
+ @store.transaction{@store[:gems].delete(@current_gem)}
64
45
  end
65
- rescue
46
+ }
47
+ else
48
+ connection.msg(message.channel, "Gem #{@current_gem} is already observed")
49
+ end
50
+ end
51
+
52
+ match(message, /^!unwatch (.*?)$/) do |match|
53
+ @store.transaction do
54
+ if @store[:gems].delete(match[1])
55
+ connection.msg(message.channel, "I removed gem #{match[1]} from watchlist")
66
56
  end
67
- }
57
+ end
58
+ end
59
+
60
+ match(message, /^!cojapacze$/) do
61
+ @store.transaction do
62
+ connection.msg(message.channel, "#{@store[:gems].keys.join(', ')}")
63
+ end
64
+ end
65
+
66
+ on_join(connection, message) do
67
+ EventMachine.add_periodic_timer(period) do
68
+ gems = @store.transaction{@store[:gems].values}
69
+ EM::Iterator.new(gems, 1).each do |gem, iter|
70
+ http = EventMachine::HttpRequest.new("http://rubygems.org/api/v1/gems/#{gem[:name]}.json").get
71
+ http.callback {
72
+ iter.next
73
+ begin
74
+ current_gem = JSON.parse(http.response)
75
+ if Gem::Version.new(gem[:version]) < Gem::Version.new(current_gem["version"])
76
+ @store.transaction do
77
+ @store[:gems][gem[:name]][:version] = current_gem["version"]
78
+ end
79
+ connection.msg(message.channel, "New version #{gem[:name]} (#{current_gem["version"]})")
80
+ end
81
+ rescue
82
+ end
83
+ }
84
+ end
85
+ end
68
86
  end
69
87
  end
70
- end
71
- end
72
88
 
73
- def period
74
- 30
89
+ def period
90
+ 30
91
+ end
92
+ end
75
93
  end
76
94
  end