Class: ProtonBot::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/protonbot/hook.rb,
lib/protonbot/core_plugin/apis/perms.rb

Overview

Hook. Created by plugins, it’s block is called if emitted event matches pattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, &block) ⇒ Hook

Returns a new instance of Hook.

Parameters:

  • pattern (Hash<Symbol>)
  • block (Proc)


16
17
18
19
20
21
# File 'lib/protonbot/hook.rb', line 16

def initialize(pattern, &block)
  @pattern = pattern
  @block   = block
  @extra   = {}
  @chain   = []
end

Instance Attribute Details

#blockProc (readonly)

Returns Block.

Returns:

  • (Proc)

    Block



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/protonbot/hook.rb', line 11

class ProtonBot::Hook
  attr_reader :pattern, :block, :extra, :chain

  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  def initialize(pattern, &block)
    @pattern = pattern
    @block   = block
    @extra   = {}
    @chain   = []
  end
end

#chainArray<Proc> (readonly)

Returns Proc array. These are executed before calling main block. If any returns false value, main block is not called.

Returns:

  • (Array<Proc>)

    Proc array. These are executed before calling main block. If any returns false value, main block is not called



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/protonbot/hook.rb', line 11

class ProtonBot::Hook
  attr_reader :pattern, :block, :extra, :chain

  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  def initialize(pattern, &block)
    @pattern = pattern
    @block   = block
    @extra   = {}
    @chain   = []
  end
end

#extraHash (readonly)

Returns Extra data. Usually used by hook modifiers.

Returns:

  • (Hash)

    Extra data. Usually used by hook modifiers



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/protonbot/hook.rb', line 11

class ProtonBot::Hook
  attr_reader :pattern, :block, :extra, :chain

  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  def initialize(pattern, &block)
    @pattern = pattern
    @block   = block
    @extra   = {}
    @chain   = []
  end
end

#patternHash<Symbol> (readonly)

Returns Hook’s pattern.

Returns:

  • (Hash<Symbol>)

    Hook’s pattern



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/protonbot/hook.rb', line 11

class ProtonBot::Hook
  attr_reader :pattern, :block, :extra, :chain

  # @param pattern [Hash<Symbol>]
  # @param block [Proc]
  def initialize(pattern, &block)
    @pattern = pattern
    @block   = block
    @extra   = {}
    @chain   = []
  end
end

Instance Method Details

#perm!(*perms) ⇒ Hook

Basic hook modifier for checking permissions. Added by core plugin.

Parameters:

  • perms (String)

    Permission names

Returns:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/protonbot/core_plugin/apis/perms.rb', line 5

def perm!(*perms)
  self.extra[:needed_perms] = perms
  self.chain << proc do |dat, hook|
    perms = dat[:perms]
    canrun = true
    needed = hook.extra[:needed_perms]
    not_enough = needed - perms
    available = needed - not_enough
    if not_enough == []
      canrun = true
    else
      canrun = false
      available_  = available.map{|i|'%C%GREEN' + i + '%N'}
      not_enough_ = not_enough.map{|i|'%C%RED' + i + '%N'}
      status      = (available_ + not_enough_).join(' ')
      dat.nreply("Not enough permissions to use this! (#{status})")
    end
    canrun
    #
  end
  self
end