Sha256: 774b0ce54f92678174123b17cc284cb4281ebf720e0c50430e7c4f1022e20b9f

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# -*- coding: utf-8 -*-
require 'cinch'
require 'cinch/cooldown'
require 'cinch/storage'

module Cinch::Plugins
  # Cinch Plugin to monitor karma and report.
  class Karma
    include Cinch::Plugin

    enforce_cooldown

    self.help = 'Use .karma <item> to see it\'s karma level. You can use ' +
                '<item>++ or <item>-- [or (something with spaces)++] to ' +
                'alter karma for an item'

    listen_to :channel

    match /karma (.+)/
    match /k (.+)/

    def initialize(*args)
      super
      @storage = Cinch::Storage.new(config[:filename] || 'yaml/karma.yml')
    end

    def listen(m)
      if m.message.match(/\S+[\+\-]{2}/)
        channel = m.channel.name

        # Scan messages for multiple karma items
        m.message.scan(/(\s|\A)(\w+|\(.+?\))(\+\+|--)(\s|\z)/).each do |karma|
          process_karma(channel, karma[1].gsub(/\(|\)/, '').downcase, karma[2])
        end

        @storage.synced_save(@bot)
      end
    end

    def execute(m, item)
      return if sent_via_pm?(m)

      channel = m.channel.name
      item.downcase!
      init_karma(channel, item)

      m.reply "Karma for #{item} is #{@storage.data[channel][item]}"
    end

    private

    def process_karma(channel, item, operation)
      # Ensure the item's Karma has been init
      init_karma(channel, item)

      case operation
      when '++'
        @storage.data[channel][item] += 1
      when '--'
        @storage.data[channel][item] -= 1
      end
    end

    def sent_via_pm?(m)
      if m.channel.nil?
        m.reply 'You must use that command in the main channel.'
        return true
      end
    end

    def init_karma(channel, item = nil)
      @storage.data[channel] ||= {}
      @storage.data[channel][item] ||= 0 unless item.nil?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinch-karma-1.0.3 lib/cinch/plugins/karma.rb