class ProtonBot::Bot
Main bot class. Use it to create the bot
Constants
- DEFAULT_SERVER_CONFIG
Default server-config
Attributes
Public Class Methods
@yield Main bot's block. You'll use it for configuring bot.
# File lib/protonbot/bot.rb, line 6 def initialize(&block) @_log = ProtonBot::Log.new @log = @_log.wrap('main') @log.info('Hi there!') instance_exec(&block) @log.info('Processed main block') @conf = {} configure @log.info('Processed config block') Dir.mkdir('dbs/') unless File.exist?(File.expand_path('./dbs/')) @db_cross = Heliodor::DB.new("dbs/pb-cross.db", true) @parr = [] @plugins = {} plugins_load @log.info('Processed plugins block') @dbs = {} @plugs = {} @plugthrs = {} @conf['servers'].each do |k_, v_| k = k_.clone v = v_.clone @dbs[k] = Heliodor::DB.new("dbs/#{k}.db", true) unless k.nil? @plugs[k] = ProtonBot::Plug.new(self, k.clone, v.clone) begin if v['enabled'] || v['enabled'].nil? Thread.new do @plugs[k].connect! end end rescue => e @plugs[k].log_err(e) end end Signal.trap('INT') do @plugs.each do |_k, v| @log.info('Stopping...') v.write_('QUIT :Stopping...') v.running = false end @_log.stop exit end @plugs.each do |_, p| p.thrjoin end @_log.stop end
Public Instance Methods
@yield Do your config-loading here @see github.com/handicraftsman/heliodor/blob/master/config.md
Config-description
# File lib/protonbot/bot_conf.rb, line 22 def configure(&block) if block @configure_block = block else @configure_block.call if @configure_block set 'tsafe', true @conf['servers'].each do |k, v| @conf['servers'][k] = DEFAULT_SERVER_CONFIG.merge(@conf['global'].to_h).merge(v.to_h) end end end
Loads plugin from gem @param gemname [String] Name of gem @return [Bot] self
# File lib/protonbot/bot_plugins.rb, line 88 def gem(gemname) if Gem.loaded_specs[gemname] require gemname.gsub(/-/, '/') path = "#{Gem.loaded_specs[gemname].lib_dirs_glob.split(':')[0]}/" \ "#{gemname.gsub(/-/, '/')}/plugin.rb" if File.exist? path pl = pluginr(path) raise ProtonBot::PluginError, "`#{path}` did not return plugin!" unless pl.instance_of? ProtonBot::Plugin @parr << pl else raise IOError, "No such file or directory: #{path}" end else raise ArgumentError, "No such gem: #{gemname}" end self end
@param dat [Hash] Sets config variable to given value
# File lib/protonbot/bot_conf.rb, line 36 def gset(dat) raise(ArgumentError, '`dat` is not hash!') unless dat.instance_of?(Hash) @conf = dat end
# File lib/protonbot/bot.rb, line 62 def inspect %(<#ProtonBot::Bot:#{object_id.to_s(16)}>) end
Loads given plugin by name @param dat [String] Plugin name in file system @return [Bot] self
# File lib/protonbot/bot_plugins.rb, line 66 def plugin(dat) pl = nil if dat.instance_of? Array dat.each do |i| pl = pluginr(File.expand_path("plugins/#{i}/plugin.rb")) raise ProtonBot::PluginError, "`plugins/#{i}/plugin.rb` did not return plugin!" unless pl.instance_of? ProtonBot::Plugin end elsif dat.instance_of? String pl = pluginr(File.expand_path("plugins/#{dat}/plugin.rb")) raise ProtonBot::PluginError, "`plugins/#{dat}/plugin.rb` did not return plugin!" unless pl.instance_of? ProtonBot::Plugin else raise ArgumentError, 'Unknown type of `dat` plugin! Use Array or String!' end @parr << pl self end
Loads plugins by array of strings. If first char of item is period, then plugin is loaded from gem @param plugins [Array<String>] @return [Bot] self
# File lib/protonbot/bot_plugins.rb, line 111 def plugin_array(arr) arr.each do |pl| if pl[0] == '.' gem pl[1,pl.length-1] else plugin pl end end end
Use it to load external plugins. You cannot provide both arguments @param file [String] Filename @param block [Proc] @example
plugin_loader do plugin('foo') # Will load plugin from ./plugins/foo/main.rb gem('bar') # Will load plugin from path_to_gem_bar/lib/bar/plugin.rb end
@example
plugin_loader 'foo' # Will load plugin from ./plugins/foo/main.rb
# File lib/protonbot/bot_plugins.rb, line 14 def plugin_loader(file = nil, &block) raise(ArgumentError, 'Both filename and load-block are nil!') if file.nil? && block.nil? raise(ArgumentError, 'Both filename and load-block are not nil!') if !file.nil? && !block.nil? if file @plugin_loader = lambda do load(File.expand_path('./' + file)) end end @plugin_loader = block if block self end
Loads plugin by path @param path [String] Path @return [Plugin] Loaded plugin
# File lib/protonbot/bot_plugins.rb, line 124 def pluginr(path) if File.exist? path pl = eval(File.read(path), nil, %r{.*/(.+/.+)}.match(path)[1]) pl.path = File.dirname(path) pl else raise IOError, 'No such file or directory: #{path}' end end
Loads all plugins by calling `@plugin_loader` and initializing each plugin @return [Bot] self
# File lib/protonbot/bot_plugins.rb, line 42 def plugins_load core = pluginr "#{Gem.loaded_specs['protonbot'].lib_dirs_glob.split(':')[0]}/protonbot/core_plugin/plugin.rb" core.bot = self core.core = core core.launch @core = core @plugins[core.name] = core core.log = @_log.wrap("?#{core.name}") core.log.info("Started plugin `#{core.name} v#{core.version}` successfully!") @plugin_loader.call if @plugin_loader @parr.each do |pl| pl.bot = self pl.core = @plugins[core.name] pl.launch @plugins[pl.name] = pl pl.log = @_log.wrap("?#{pl.name}") pl.log.info("Started plugin `#{pl.name} v#{pl.version}` successfully!") end self end
Basically clears plugin hash @return [Bot] self
# File lib/protonbot/bot_plugins.rb, line 33 def plugins_unload @parr = [] @plugins = {} self end
Binds `val` to `key` in `@conf` @param key [Symbol] @param val [Object]
# File lib/protonbot/bot_conf.rb, line 44 def set(key, val) @conf[key.to_sym] = val end