cinchrb / cinch

The IRC Bot Building Framework
http://www.rubydoc.info/gems/cinch
MIT License
1k stars 180 forks source link

`register_plugins': undefined method `plugins' while trying a simple plugin. #45

Closed wedtm closed 13 years ago

wedtm commented 13 years ago

This is when trying to import a simple plugin.

/Users/wedtm/.rvm/gems/ruby-1.9.2-p180/gems/cinch-1.1.3/lib/cinch/bot.rb:362:in `register_plugins': undefined method `plugins' for [TinyURL]:Array (NoMethodError)
    from /Users/wedtm/.rvm/gems/ruby-1.9.2-p180/gems/cinch-1.1.3/lib/cinch/bot.rb:409:in `start'
    from fred.rb:29:in `<main>'
~/Dropbox/code/fred > 

fred.rb

require 'rubygems'
#require 'bundler/setup'
#Bundler.require
require 'cinch'
require 'nokogiri'
require 'cgi'

Dir[File.join('plugins', '*.rb')].each { |file| require "./" + file }

bot = Cinch::Bot.new do
  configure do |c|
    c.server = "irc.esper.net"
    c.channels = ["#wedtm"]
    c.nick = "Fred"
    c.user = "Fred"
    c.plugins = [TinyURL]
  end

  on :message, "hello" do |m|
    m.reply "Hello, #{m.user.nick}"
  end

  on :message, /^hello (.+)/ do |m, args|
    m.reply "Hello 2, #{args[0]}"
  end
end

bot.start

plugins/tinyurl.rb

require 'open-uri'
require 'cinch'

class TinyURL
  include Cinch::Plugin

  listen_to :channel

  def shorten(url)
    url = open("http://tinyurl.com/api-create.php?url=#{URI.escape(url)}").read
    url == "Error" ? nil : url
  rescue OpenURI::HTTPError
    nil
  end

  def listen(m)
    urls = URI.extract(m.message, "http")
    short_urls = urls.map { |url| shorten(url) }.compact
    unless short_urls.empty?
      m.reply short_urls.join(", ")
    end
  end
end
dominikh commented 13 years ago

It has to be c.plugins.plugins, not c.plugins. I know, at first that seems weird, but the reason for this is that we also have c.plugins.options