Class: ProtonBot::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/protonbot/plugin.rb

Overview

Plugin object. Use it to alter bot’s behavior

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Plugin

Returns a new instance of Plugin.

Parameters:

  • block (Proc)


26
27
28
29
30
# File 'lib/protonbot/plugin.rb', line 26

def initialize(&block)
  @hooks = []
  @block = block
  @numeric = ProtonBot::Numeric
end

Instance Attribute Details

#botBot

Returns Bot.

Returns:

  • (Bot)

    Bot



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

#corePlugin (readonly)

Returns Core plugin.

Returns:



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

#descriptionString (readonly)

Returns Plugin’s description.

Returns:

  • (String)

    Plugin’s description



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

#hooksArray<Hook> (readonly)

Returns Plugin’s hooks.

Returns:

  • (Array<Hook>)

    Plugin’s hooks



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

#logObject

Returns the value of attribute log.



23
24
25
# File 'lib/protonbot/plugin.rb', line 23

def log
  @log
end

#LogLogWrapper

Returns Log.

Returns:



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

#nameString (readonly)

Returns Plugin’s name.

Returns:

  • (String)

    Plugin’s name



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

#pathString

Returns Path.

Returns:

  • (String)

    Path



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



22
23
24
# File 'lib/protonbot/plugin.rb', line 22

def plugins
  @plugins
end

#versionString (readonly)

Returns Plugin’s version.

Returns:

  • (String)

    Plugin’s version



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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protonbot/plugin.rb', line 21

class ProtonBot::Plugin
  attr_reader :name, :version, :description, :plugins, :hooks, :core
  attr_accessor :bot, :log, :path

  # @param block [Proc]
  def initialize(&block)
    @hooks = []
    @block = block
    @numeric = ProtonBot::Numeric
  end

  # Starts plugin
  # @return [Plugin] self
  def launch
    @plugins = @bot.plugins
    @core    = @bot.plugins['core']
    instance_exec(&@block)
    raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
      @name
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
      @description
    log.info("Started plugin `#{@name} v#{@version}` successfully!")
  end

  # Creates hook and returns it
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def hook(pattern = {}, &block)
    h = ProtonBot::Hook.new(pattern, &block)
    @hooks << h
    h
  end

  # Shortcut for `hook(type: :command, ...) {}`
  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  # @return [Hook] hook
  def cmd(pattern = {}, &block)
    hook(pattern.merge(type: :command), &block)
  end

  # Emits passed event
  # @param dat [Hash<Symbol>]
  def emit(dat = {})
    dat[:plug].emit(dat) if dat[:plug]
  end

  # Emits passed event in other thread
  # @param dat [Hash<Symbol>]
  def emitt(dat = {})
    dat[:plug].emitt(dat) if dat[:plug]
  end

  # Runs given file (.rb is appended automatically)
  # @param fname [String]
  def run(fname)
    if @name == 'Core'
      instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
        "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
    else
      instance_exec do
        instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
      end
    end
  end

  # Shortcut for `define_singleton_method(...)`
  # @param name [String,Symbol]
  # @param block [Proc]
  def fun(name, &block)
    define_singleton_method(name, &block)
  end

  # @return [String] Output
  def inspect
    %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
  end
end

Instance Method Details

#cmd(pattern = {}, &block) ⇒ Hook

Shortcut for ‘hook(type: :command, …) {}`

Parameters:

  • pattern (Hash<Symbol>) (defaults to: {})
  • block (Proc)

Returns:



61
62
63
# File 'lib/protonbot/plugin.rb', line 61

def cmd(pattern = {}, &block)
  hook(pattern.merge(type: :command), &block)
end

#emit(dat = {}) ⇒ Object

Emits passed event

Parameters:

  • dat (Hash<Symbol>) (defaults to: {})


67
68
69
# File 'lib/protonbot/plugin.rb', line 67

def emit(dat = {})
  dat[:plug].emit(dat) if dat[:plug]
end

#emitt(dat = {}) ⇒ Object

Emits passed event in other thread

Parameters:

  • dat (Hash<Symbol>) (defaults to: {})


73
74
75
# File 'lib/protonbot/plugin.rb', line 73

def emitt(dat = {})
  dat[:plug].emitt(dat) if dat[:plug]
end

#fun(name, &block) ⇒ Object

Shortcut for ‘define_singleton_method(…)`

Parameters:

  • name (String, Symbol)
  • block (Proc)


93
94
95
# File 'lib/protonbot/plugin.rb', line 93

def fun(name, &block)
  define_singleton_method(name, &block)
end

#hook(pattern = {}, &block) ⇒ Hook

Creates hook and returns it

Parameters:

  • pattern (Hash<Symbol>) (defaults to: {})
  • block (Proc)

Returns:



51
52
53
54
55
# File 'lib/protonbot/plugin.rb', line 51

def hook(pattern = {}, &block)
  h = ProtonBot::Hook.new(pattern, &block)
  @hooks << h
  h
end

#inspectString

Returns Output.

Returns:

  • (String)

    Output



98
99
100
# File 'lib/protonbot/plugin.rb', line 98

def inspect
  %(<#ProtonBot::Plugin:#{object_id.to_s(16)} @name=#{name} @version=#{version}>)
end

#launchPlugin

Starts plugin

Returns:

Raises:

  • (ProtonBot::PluginError)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/protonbot/plugin.rb', line 34

def launch
  @plugins = @bot.plugins
  @core    = @bot.plugins['core']
  instance_exec(&@block)
  raise ProtonBot::PluginError, 'Plugin-name is not set!' unless
    @name
  raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
    @description
  raise ProtonBot::PluginError, 'Plugin-description is not set!' unless
    @description
  log.info("Started plugin `#{@name} v#{@version}` successfully!")
end

#run(fname) ⇒ Object

Runs given file (.rb is appended automatically)

Parameters:

  • fname (String)


79
80
81
82
83
84
85
86
87
88
# File 'lib/protonbot/plugin.rb', line 79

def run(fname)
  if @name == 'Core'
    instance_eval File.read("#{Gem.loaded_specs['protonbot'].lib_dirs_glob}/" \
      "protonbot/core_plugin/#{fname}.rb"), "#{name}/#{fname}.rb"
  else
    instance_exec do
      instance_eval File.read(File.expand_path("#{path}/#{fname}.rb")), "#{name}/#{fname}.rb"
    end
  end
end