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 if(File.exist?("#{@bot.botclass}/karma.rbot"))
18 log "importing old karma data"
19 IO.foreach("#{@bot.botclass}/karma.rbot") do |line|
20 if(line =~ /^(\S+)<=>([\d-]+)$/)
23 @registry[item] = karma
26 File.delete("#{@bot.botclass}/karma.rbot")
32 max = @registry.values.max
33 min = @registry.values.min
34 best = @registry.to_hash.index(max)
35 worst = @registry.to_hash.index(min)
36 m.reply "#{@registry.length} items. Best: #{best} (#{max}); Worst: #{worst} (#{min})"
42 thing = m.sourcenick unless thing
44 karma = @registry[thing]
46 m.reply "karma for #{thing}: #{@registry[thing]}"
48 m.reply "#{thing} has neutral karma"
52 def setkarma(m, params)
53 thing = (params[:key] || m.sourcenick).to_s
54 @registry[thing] = params[:val].to_i
58 def help(plugin, topic="")
59 "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."
63 return unless m.kind_of?(PrivMessage) && m.public?
64 # in channel message, the kind we are interested in
65 if(m.message =~ /(\+\+|--)/)
66 string = m.message.sub(/\W(--|\+\+)(\(.*?\)|[^(++)(\-\-)\s]+)/, "\2\1")
68 while(string.sub!(/(\(.*?\)|[^(++)(\-\-)\s]+)(\+\+|--)/, ""))
74 key.sub!(/^\((.*)\)$/, "\1")
76 next unless(key.length > 0)
77 next if(key.downcase == m.sourcenick.downcase)
80 if key =~ /^#{@bot.nick}$/i
81 @bot.say m.replyto, @bot.lang.get("thanks")
91 plugin = KarmaPlugin.new
93 plugin.default_auth( 'edit', false )
95 plugin.map 'karmastats', :action => 'stats'
96 plugin.map 'karma :key', :defaults => {:key => false}
97 plugin.map 'setkarma :key :val', :defaults => {:key => false}, :requirements => {:val => /^-?\d+$/}, :auth_path => 'edit::set!'
98 plugin.map 'karma for :key'