Module: Cinch::Plugin::Cooldowns

Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugin/cooldowns.rb

Overview

Class for managing the cooldown objects.

Class Method Summary collapse

Class Method Details

.clean_expired_cooldowns(channel) ⇒ Object



93
94
95
96
97
98
# File 'lib/cinch/plugin/cooldowns.rb', line 93

def self.clean_expired_cooldowns(channel)
  return unless @cooldowns.key?(channel)
  @cooldowns[channel].each_pair do |key, cooldown|
    @cooldowns[channel].delete(key) if cooldown.cooled_down?
  end
end

.config_broken?(channel) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
# File 'lib/cinch/plugin/cooldowns.rb', line 51

def self.config_broken?(channel)
  # return true if the config doesn't have needed info this channel
  return true unless @config.key?(channel) &&
                     config_for(channel).key?(:global) &&
                     config_for(channel).key?(:user)
  # otherwise abort cooldown enforcement
  false
end

.config_for(chan) ⇒ Object



60
61
62
# File 'lib/cinch/plugin/cooldowns.rb', line 60

def self.config_for(chan)
  @config[chan]
end

.cool?(channel, user) ⇒ Boolean

Main cooldown data check

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cinch/plugin/cooldowns.rb', line 39

def self.cool?(channel, user)
  # Make sure the configuration is sane.
  return true if config_broken?(channel) ||
                 # Make sure it's not the first command
                 first_run?(channel, user) ||
                 # Check if timers are finished
                 cooldowns_finished?(channel, user)

  # Otherwise trigger cooldown
  false
end

.cooldowns_finished?(channel, user) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cinch/plugin/cooldowns.rb', line 76

def self.cooldowns_finished?(channel, user)
  # Chuck all the cooldowns that have expired
  clean_expired_cooldowns(channel)

  # if the channel's cooldown is up
  if @cooldowns[channel][:global].nil?
    # And their cd's up, or they've not used bot before, run the command
    if @cooldowns[channel][user].nil?
      # trigger a new cooldown
      trigger_cooldown_for(channel, user)
      # and run the command
      return true
    end
  end
  false
end

.finished?(m, shared, bot) ⇒ Boolean

Main method called by the hook

Returns:

  • (Boolean)


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
# File 'lib/cinch/plugin/cooldowns.rb', line 12

def self.finished?(m, shared, bot)
  return unless shared.is_a?(Hash)
  @config     = shared[:config]
  @cooldowns  = shared[:cooldowns] if shared.key?(:cooldowns)

  # Don't run if we there's no cooldown config
  return true unless @config

  bot.synchronize(:cooldown) do
    # Avoid cooldown if we don't have a channel
    #   (i.e. user is pming the bot)
    return true if m.channel.nil?

    clean_expired_cooldowns(m.channel.name)

    # return true if the cooldowns have expired
    return true if cool?(m.channel.name, m.user.nick)

    # Otherwise message the user about unfinished cooldowns
    m.user.notice message(m.channel.name, m.user.nick)

    # and return false so the command gets dropped by the hook
    false
  end
end

.first_run?(channel, user) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/cinch/plugin/cooldowns.rb', line 64

def self.first_run?(channel, user)
  unless @cooldowns.key?(channel)
    trigger_cooldown_for(channel, user)
    return true
  end
  false
end

.message(channel, user) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cinch/plugin/cooldowns.rb', line 100

def self.message(channel, user)
  cds = []
  if @cooldowns[channel].key?(:global)
    chan_exp = @cooldowns[channel][:global].time_till_expire_in_words
    cds << "#{chan_exp} before I can talk in the channel again"
  elsif @cooldowns[channel].key?(user)
    user_exp = @cooldowns[channel][user].time_till_expire_in_words
    cds << "#{user_exp} before you can use any commands"
  end
  ['Sorry, cooldown is in effect:', cds.join(', and ')].join(' ')
end

.purge!Object



72
73
74
# File 'lib/cinch/plugin/cooldowns.rb', line 72

def self.purge!
  @cooldowns = {}
end

.trigger_cooldown_for(channel, user) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cinch/plugin/cooldowns.rb', line 112

def self.trigger_cooldown_for(channel, user)
  # Make sure the channel array has been init
  @cooldowns[channel] ||= {}

  # Create a cooldown for the channel
  @cooldowns[channel][:global] =
    Cooldown.new(@config[channel][:global])
  # Create a cooldown for the user
  @cooldowns[channel][user] =
    Cooldown.new(@config[channel][:user])

  warn "[[ Cooldown Triggered for user: #{user} ]]"
end