Advertisement
Namasteh

Untitled

Feb 8th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.81 KB | None | 0 0
  1. require 'cinch'
  2. require 'cinch/toolbox'
  3. require 'cinch-storage'
  4. require 'cinch/cooldown'
  5. require 'time-lord'
  6.  
  7. module Cinch
  8.   module Plugins
  9.     class Seen
  10.       include Cinch::Plugin
  11.    
  12.       class Watch < Struct.new(:nick, :time, :message)
  13.         def to_yaml
  14.           { nick: nick, time: time, message: message }
  15.         end
  16.       end
  17.      
  18.       enforce_cooldown
  19.      
  20.       set :prefix, /^~/
  21.       set :plugin_name, 'seen'
  22.       set :help, <<-USAGE.gsub(/^ {6}/, '')
  23.         Just a seen plugin!
  24.           Usage:
  25.             * ~seen <name>: Use this to see the last thing a nick said and where
  26.           USAGE
  27.       listen_to :channel
  28.      
  29.       match /seen ([^\s]+)\z/
  30.      
  31.     def initialize(*args)
  32.       super
  33.       @storage = CinchStorage.new(config[:filename] || 'seen.yml')
  34.       @storage.data ||= {}
  35.     end
  36.    
  37.     def listen(m)
  38.       channel = m.channel.name
  39.       nick = m.user.nick
  40.       @storage.data[channel] ||= {}
  41.       @storage.data[channel][nick.downcase] =
  42.         Watch.new(nick, Time.now, m.message)
  43.       @storage.synced_save(@bot)
  44.     end
  45.    
  46.     def execute(m, nick)
  47.       return if sent_via_pm?(m)
  48.       unless m.user.nick.downcase == nick.downcase
  49.         m.reply last_seen(m.channel.name, nick), true
  50.       end
  51.     end
  52.    
  53.     private
  54.  
  55.     def last_seen(channel, nick)
  56.       @storage.data[channel] ||= {}
  57.       activity = @storage.data[channel][nick.downcase]
  58.      
  59.       if activity.nil?
  60.         "I haven't seen #{nick} before, sorry!"
  61.       else
  62.         "I last saw #{activity.nick} #{activity.time.ago.to_words} " +
  63.         "saying '#{activity.message}'"
  64.       end
  65.     end
  66.    
  67.     def sent_via_pm?(m)
  68.       return false unless m.channel.nil?
  69.       m.reply 'You must use that command in the main channel.'
  70.       true
  71.     end
  72.   end
  73. end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement