Advertisement
Guest User

Untitled

a guest
Oct 29th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. require 'cinch'
  2. require 'open-uri'
  3. require 'nokogiri'
  4. require 'cgi'
  5.  
  6. class Google
  7. include Cinch::Plugin
  8. match /google (.+)/
  9.  
  10. def search(query)
  11. url = "http://www.google.com/search?q=#{CGI.escape(query)}"
  12. res = Nokogiri::HTML(open(url)).at("h3.r")
  13.  
  14. title = res.text
  15. link = res.at('a')[:href]
  16. desc = res.at("./following::div").children.first.text
  17. CGI.unescape_html "#{title} - #{desc} (#{link})"
  18. rescue
  19. "No results found"
  20. end
  21.  
  22. def execute(m, query)
  23. m.reply(search(query))
  24. end
  25. end
  26.  
  27. class UrbanDictionary
  28. include Cinch::Plugin
  29.  
  30. match /urban (.+)/
  31. def lookup(word)
  32. url = "http://www.urbandictionary.com/define.php?term=#{CGI.escape(word)}"
  33. CGI.unescape_html Nokogiri::HTML(open(url)).at("div.definition").text.gsub(/\s+/, ' ') rescue nil
  34. end
  35.  
  36. def execute(m, word)
  37. m.reply(lookup(word) || "No results found", true)
  38. end
  39. end
  40.  
  41. class JoinPart
  42. include Cinch::Plugin
  43.  
  44. match /join (.+)/, method: :join
  45. match /part(?: (.+))?/, method: :part
  46.  
  47. def initialize(*args)
  48. super
  49.  
  50. @admins = ["injekt", "DominikH"]
  51. end
  52.  
  53. def check_user(user)
  54. user.refresh # be sure to refresh the data, or someone could steal
  55. # the nick
  56. @admins.include?(user.authname)
  57. end
  58.  
  59. def join(m, channel)
  60. return unless check_user(m.user)
  61. Channel(channel).join
  62. end
  63.  
  64. def part(m, channel)
  65. return unless check_user(m.user)
  66. channel ||= m.channel
  67. Channel(channel).part if channel
  68. end
  69. end
  70.  
  71. bot = Cinch::Bot.new do
  72. configure do |c|
  73. c.server = "irc.sinsira.net"
  74. c.channels = ["#test"]
  75. c.nick = "Eve"
  76. c.user = "Eve"
  77. c.realname = "Eve 1.a"
  78. c.plugins.plugins = [Google]
  79. c.plugins.plugins = [UrbanDictionary]
  80. c.plugins.plugins = [JoinPart]
  81. end
  82.  
  83. on :message, "hello" do |m|
  84. m.reply "Hello, #{m.user.nick}"
  85. end
  86. on :action, "kicks the bot" do |m|
  87. m.reply "Ouch! Stop kicking me :(", true
  88. m.reply "I thought you loved me D:", true
  89. end
  90. end
  91.  
  92. bot.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement