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

.channel_to_key(channel) ⇒ Object



62
63
64
65
# File 'lib/cinch/plugin/cooldowns.rb', line 62

def self.channel_to_key(channel)
  return channel if channel.is_a?(Symbol)
  channel[/\w+/].to_sym
end

.clean_expired_cooldowns(channel) ⇒ Object



100
101
102
103
104
105
# File 'lib/cinch/plugin/cooldowns.rb', line 100

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)


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

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



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

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

.cool?(channel, user) ⇒ Boolean

Main cooldown data check

Returns:

  • (Boolean)


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

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)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cinch/plugin/cooldowns.rb', line 83

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
37
38
# 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?

    channel = channel_to_key(m.channel.name)

    clean_expired_cooldowns(channel)

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

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

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

.first_run?(channel, user) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/cinch/plugin/cooldowns.rb', line 71

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

.message(channel, user) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cinch/plugin/cooldowns.rb', line 107

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



79
80
81
# File 'lib/cinch/plugin/cooldowns.rb', line 79

def self.purge!
  @cooldowns = {}
end

.trigger_cooldown_for(channel, user) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cinch/plugin/cooldowns.rb', line 119

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