Class: ProtonBot::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/protonbot/hook.rb,
lib/protonbot/core_plugin/apis/perms.rb,
lib/protonbot/core_plugin/apis/cooldowns.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



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)



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)



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)



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)



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

#cooldown!(seconds, mode = :user_and_target, verbose = true) ⇒ Hook

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



7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/protonbot/core_plugin/apis/cooldowns.rb', line 7

def cooldown!(seconds, mode = :user_and_target, verbose = true)
  self.extra[:cooldown_seconds] = seconds
  self.extra[:cooldown_verbose] = verbose
  if [:global, :user_local, :target_local, :user_and_target].include? mode
    self.extra[:cooldown_mode] = mode
  else
    self.extra[:cooldown_mode] = :user_and_target
  end

  self.chain << proc do |dat, hook|
    lu_sign =
      case hook.extra[:cooldown_mode]
      when :global
        :"last_used_#{dat[:plug].name}"
      when :user_local
        :"last_used_#{dat[:plug].name}_#{dat[:host]}"
      when :target_local
        :"last_used_#{dat[:plug].name}_#{dat[:target]}"
      when :user_and_target
        :"last_used_#{dat[:plug].name}_#{dat[:target]}_#{dat[:host]}"
      end
    if dat[:bot].core.getpermsr(dat[:plug], dat[:host]).include? 'nocooldown'
      true
    elsif hook.extra[lu_sign]
      curtime = Time.now.to_i
      cmp = hook.extra[lu_sign] - curtime + hook.extra[:cooldown_seconds]
      if (cmp) <= 0
        hook.extra[lu_sign] = curtime
        true
      else
        dat.nreply("You need to wait %B#{cmp}%N more seconds to use this!") if
          hook.extra[:cooldown_verbose]
        false
      end
    else
      hook.extra[lu_sign] = Time.now.to_i
      true
    end
  end
end

#perm!(*perms) ⇒ Hook

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



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