Advertisement
Namasteh

Untitled

Feb 9th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.59 KB | None | 0 0
  1. require 'cinch'
  2. require 'yaml'
  3.  
  4. module Cinch
  5.   module Plugins
  6.     class Quotes
  7.       include Cinch::Plugin
  8.      
  9.       set :prefix, /^~/
  10.  
  11.       match /addquote (.+)/i,  method: :addquote
  12.       match /quote (.+)/i,     method: :quote
  13.       match "quote",           method: :quote
  14.  
  15.       def initialize(*args)
  16.         super
  17.  
  18.         @quotes_file = config[:quotes_file]
  19.       end
  20.  
  21.       def addquote(m, quote)
  22.         # make the quote
  23.         new_quote = { "quote" => quote, "added_by" => m.user.nick, "created_at" => Time.now, "deleted" => false }
  24.  
  25.         # add it to the list
  26.         existing_quotes = get_quotes || []
  27.         existing_quotes << new_quote
  28.  
  29.         # find the id of the new quote and set it based on where it was placed in the quote list
  30.         new_quote_index = existing_quotes.index(new_quote)
  31.         existing_quotes[new_quote_index]["id"] = new_quote_index + 1
  32.  
  33.         # write it to the file
  34.         output = File.new(@quotes_file, 'w')
  35.         output.puts YAML.dump(existing_quotes)
  36.         output.close
  37.  
  38.         # send reply that quote was added
  39.         m.reply "#{m.user.nick}: Quote successfully added as ##{new_quote_index + 1}."
  40.       end
  41.  
  42.       def quote(m, search = nil)
  43.         quotes = get_quotes.delete_if{ |q| q["deleted"] == true }
  44.         if search.nil? # we are pulling random
  45.           quote = quotes.sample
  46.           m.reply "#{m.user.nick}: ##{quote["id"]} - #{quote["quote"]}"
  47.         elsif search.to_i != 0 # then we are searching by id
  48.           quote = quotes.find{|q| q["id"] == search.to_i }
  49.           if quote.nil?
  50.             m.reply "#{m.user.nick}: No quotes found."
  51.           else
  52.             m.reply "#{m.user.nick}: ##{quote["id"]} - #{quote["quote"]}"
  53.           end
  54.         else
  55.           quotes.keep_if{ |q| q["quote"].downcase.include?(search.downcase) }
  56.           if quotes.empty?
  57.             m.reply "#{m.user.nick}: No quotes found."
  58.           else
  59.             quote = quotes.first
  60.             m.reply "#{m.user.nick}: ##{quote["id"]} - #{quote["quote"]}"
  61.             m.reply "The search term also matched on quote IDs: #{quotes.map{|q| q["id"]}.join(", ")}" if quotes.size > 1
  62.           end
  63.         end
  64.       end
  65.  
  66.       #--------------------------------------------------------------------------------
  67.       # Protected
  68.       #--------------------------------------------------------------------------------
  69.      
  70.       protected
  71.  
  72.       def get_quotes
  73.         output = File.new(@quotes_file, 'r')
  74.         quotes = YAML.load(output.read)
  75.         output.close
  76.  
  77.         quotes
  78.       end
  79.  
  80.     end
  81.   end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement