cinchrb / cinch

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

Problem when using unregister_plugin. #159

Closed zackp30 closed 10 years ago

zackp30 commented 10 years ago
require 'cinch'
class PluginManager
  include Cinch::Plugin
  def initialize(*args)
    super
    @plugdir = Dir.pwd + '/plugins'
  end
  hook :pre, method: :update_plugin_list
  def update_plugin_list(m)
    @plugins = bot.plugins
  end
  match /loadplug (.+)/, method: :loadplug
  match /unloadplug (.+)/, method: :unload
  match /reloadplug (.+)/, method: :reload
  def loadplug(m, plugin)
    puts "---- Requiring #{@plugdir}/#{plugin}"
    require "#{@plugdir}/#{plugin}"
    r = Kernel.const_get "#{plugin.capitalize}"
    @plugins.register_plugin r
    m.reply 'Plugin loaded.'
  end
  def unload(m, plugin)
    puts "---- Unloading #{@plugdir}/#{plugin}"
    m.reply 'Plugin unloaded.'
    @plugins.unregister_plugin Kernel.const_get "#{plugin.capitalize}"
  end
  def reload(m, plugin)
    unload m, plugin
    load m, plugin
  end
end

bot = Cinch::Bot.new do
  configure do |c|
    c.plugins.plugins = [PluginManager]
    c.server = '172.23.32.14'
    c.channels = ['#PieServer']
  end
end
bot.start

[2014/03/08 00:48:24.258] !! /home/morpheus/.rvm/gems/ruby-2.1.0/gems/cinch-2.1.0/lib/cinch/plugin_list.rb:21:inunregister_plugin': undefined method unregister' for Test:Class (NoMethodError)

dominikh commented 10 years ago

Apparently diagnosed and solved in the IRC channel, closing.

benklop commented 9 years ago

what was the solution to this?

dominikh commented 9 years ago

@benklop From the IRC logs:

2014-03-08 16:36:05 nickrw it's because you're trying to unload the class, not the instantiated plugin object 2014-03-08 16:36:33 Zackio Oh... 2014-03-08 16:36:47 nickrw I do this in my unloader: 2014-03-08 16:37:54 nickrw i = bot.plugins.find_index { |x| x.class == klass } 2014-03-08 16:37:54 nickrw bot.plugins.unregister_plugin(bot.plugins[i]) 2014-03-08 16:38:20 nickrw where 'klass' is plugin.captialize in your code 2014-03-08 17:15:29 Zackio It worked! Thanks! :D