Class: Cogbot::Bot

Inherits:
Thor
  • Object
show all
Defined in:
lib/cogbot.rb

Instance Method Summary collapse

Instance Method Details

#restartObject



100
101
102
103
# File 'lib/cogbot.rb', line 100

def restart
  stop
  start
end

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cogbot.rb', line 19

def start

  # prepare config
  config = {}
  begin
    config = YAML::load_file(CONFIG_FILE)
  rescue Exception => e
    load "lib/cogbot/setup.rb"
    config['main'] = Cogbot::Setup.init
  end

  # prepare daemon
  Daemons.daemonize(
    :app_name => 'cogbot',
    :dir_mode => :normal,
    :log_dir => LOG_DIR,
    :log_output => true,
    :dir => File.join('/', 'tmp')
  )

  # checkout plugins
  plugins = []
  config['main']['plugins'].each do |p|
    puts p
    puts File.join(ROOT_DIR,'plugins',"#{p}.rb")
    if File.exists?(File.join(ROOT_DIR,'plugins',"#{p}.rb"))
      puts p
      require File.join(ROOT_DIR,'plugins',p)
      plugins.push Cinch::Plugins.const_get(p.camelize)
    end
  end

  # create bot
  bot = Cinch::Bot.new do
    configure do |c|
      c.server = config['main']['server']
      c.ssl.use = ( config['main']['ssl'] == 'true' )
      c.nick = config['main']['nick']
      c.user = config['main']['nick']
      c.realname = config['main']['nick']
      c.channels = config['main']['channels']
      c.options = { 'cogconf' => config }
      c.plugins.prefix = config['main']['prefix']
      c.plugins.plugins = plugins
      #c.storage.backend = Cinch::Storage::YAML
      #c.storage.basedir = File.join(ROOT_DIR,"yaml")
      #c.storage.autosave = true
    end
    on :message, "hi" do |m|
      m.reply "Hello, #{m.user.nick}"
    end
  end
  bot.loggers.debug(plugins.inspect)

  Signal.trap('TERM') { EM.stop }

  EM.run do
    EM.defer { bot.start }
    if config['server']
      EM.add_timer(3) do
        EM.start_server(
          config['server']['ip'],
          config['server']['port'],
          Server,
          bot
        )
      end
    end
  end

  bot.quit
end

#stopObject



93
94
95
96
97
# File 'lib/cogbot.rb', line 93

def stop
  pid_file = File.join('/', 'tmp', 'cogbot.pid')
  pid = File.read(pid_file).to_i if File.exist?(pid_file)
  Process.kill('TERM', pid)
end