1 class KarmaPlugin < Plugin
5 # this plugin only wants to store ints!
14 @registry.set_default(0)
16 # import if old file format found
17 oldkarma = @bot.path 'karma.rbot'
18 if File.exist? oldkarma
19 log "importing old karma data"
20 IO.foreach(oldkarma) do |line|
21 if(line =~ /^(\S+)<=>([\d-]+)$/)
24 @registry[item] = karma
33 max = @registry.values.max
34 min = @registry.values.min
35 best = @registry.to_hash.index(max)
36 worst = @registry.to_hash.index(min)
37 m.reply "#{@registry.length} items. Best: #{best} (#{max}); Worst: #{worst} (#{min})"
43 thing = m.sourcenick unless thing
45 karma = @registry[thing]
47 m.reply "karma for #{thing}: #{@registry[thing]}"
49 m.reply "#{thing} has neutral karma"
53 def setkarma(m, params)
54 thing = (params[:key] || m.sourcenick).to_s
55 @registry[thing] = params[:val].to_i
59 def help(plugin, topic="")
60 "karma module: Listens to everyone's chat. <thing>++/<thing>-- => increase/decrease karma for <thing>, karma for <thing>? => show karma for <thing>, karmastats => show stats. Karma is a community rating system - only in-channel messages can affect karma and you cannot adjust your own."
64 return unless m.public? && m.message.match(/\+\+|--/)
68 m.message.split.each_with_index do |tok, i|
69 tok.sub!(/[:,]$/, '') if i == 0
70 catch :me_if_you_can do
71 if m.channel.users[tok].nil?
72 if (tok =~ /^(?:--)(.*[^-].*)$/) || (tok =~ /^(.*[^-].*)(?:--)$/)
75 elsif (tok =~ /^(?:\+\+)(.*[^+].*)$/)||(tok =~ /^(.*[^+].*)(?:\+\+)$/)
81 if (tok =~ /^--+$/) || (tok =~ /^\+\++$/)
90 ac[arg] += (op == '--' ? -1 : 1) unless arg.downcase == m.sourcenick.downcase
97 @registry[k] += (v > 0 ? 1 : -1)
98 m.reply @bot.lang.get("thanks") if k == @bot.nick && v > 0
103 plugin = KarmaPlugin.new
105 plugin.default_auth( 'edit', false )
107 plugin.map 'karmastats', :action => 'stats'
108 plugin.map 'karma :key', :defaults => {:key => false}
109 plugin.map 'setkarma :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!'
110 plugin.map 'karma for :key'