cinch-cooldown 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Cinch::Cooldown
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/bhaberer/cinch-cooldown.png)](https://codeclimate.com/github/bhaberer/cinch-cooldown)
4
+
3
5
  This library is used to add a global cooldown so that users are prevented from spamming the
4
6
  channel.
5
7
 
@@ -19,7 +21,20 @@ Or install it yourself as:
19
21
 
20
22
  ## Usage
21
23
 
22
- Coming Soon. :)
24
+ Configuration Steps:
25
+
26
+ 1. You need to add the configuration to your bot in the config block. You will need to add
27
+ config info for every channel the bot is in that you want a cooldown. The `:global` is how
28
+ many seconds the bot will wait before listening to a command that prints something to the
29
+ channel, while the `:user` directive is how long. Currently the gem is simple and assumes
30
+ that the user timer will be greater than the global timer.
31
+
32
+ c.shared[:cooldown] = { :config => { '#bottest' => { :global => 10
33
+ :user => 20 } }
34
+
35
+ 2. If you are using this with my plugins, things should just work. However if you want to use
36
+ this with your own plugins, yoiu need to add a `require cinch-cooldown` to the top of said
37
+ plugin, and an `enforce_cooldown` to the plugin after the `include Cinch::Plugin` line.
23
38
 
24
39
  ## Contributing
25
40
 
@@ -17,5 +17,5 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'cinch-toolbox'
20
+ gem.add_dependency('time-lord', '>= 1.0.1')
21
21
  end
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Cooldown
3
- VERSION = "0.0.1"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  require 'cinch-cooldown/version'
2
- require 'cinch-toolbox'
2
+ require 'time-lord'
3
3
 
4
4
  module Cinch
5
5
  module Plugin
@@ -8,76 +8,104 @@ module Cinch
8
8
  hook(:pre, :for => [:match], :method => lambda {|m| cooldown_finished?(m)})
9
9
  end
10
10
 
11
- def reset_cooldown(m)
11
+ def reset_cooldown
12
12
  # TODO
13
13
  end
14
14
  end
15
15
 
16
16
  def cooldown_finished?(m)
17
+ # return if we don't have a cooldown config
17
18
  return true unless shared[:cooldown] && shared[:cooldown][:config]
18
19
  synchronize(:cooldown) do
19
- return true if m.channel.nil?
20
+ # return if we don't have a channel (i.e. user is pming the bot)
21
+ return false if m.channel.nil?
20
22
 
21
23
  channel = m.channel.name
22
- nick = m.user.nick
24
+ user = m.user.nick
23
25
 
24
- return true unless shared[:cooldown][:config][channel]
26
+ # Make sure the configuration is sane.
27
+ return true if configuration_broken?(channel)
25
28
 
26
- unless shared[:cooldown].key?(channel)
27
- shared[:cooldown][channel] = { 'global' => Time.now, nick => Time.now }
28
- return true
29
- end
29
+ # Make sure it's not the first command
30
+ return true if first_run?(channel, user)
31
+
32
+ # Check if timers are finished
33
+ return true if cooldowns_finished?(channel, user)
34
+
35
+ # Handle unfinished cooldowns here
36
+ m.user.notice cooldown_message(channel, user)
37
+ return false
38
+ end
39
+ end
40
+
41
+ def configuration_broken?(channel)
42
+ # return if the config doesn't smell right for this channel
43
+ return true unless shared[:cooldown][:config].key?(channel) &&
44
+ shared[:cooldown][:config][channel].key?(:global) &&
45
+ shared[:cooldown][:config][channel].key?(:user)
46
+
47
+ end
30
48
 
31
- if channel_cooldown_finished?(channel)
32
- # Global cd is up, check per user
33
- if shared[:cooldown][channel].key?(nick)
34
- # User's in the config, check time
35
- if user_cooldown_finished?(channel, nick)
36
- shared[:cooldown][channel]['global'] = Time.now
37
- shared[:cooldown][channel][nick] = Time.now
38
- return true
39
- end
40
- else
41
- # User's not used bot before
42
- shared[:cooldown][channel][nick] = Time.now
43
- shared[:cooldown][channel]['global'] = Time.now
49
+ def first_run?(channel, user)
50
+ unless shared[:cooldown].key?(channel)
51
+ trigger_cooldown_for(channel, user)
52
+ return true
53
+ end
54
+ end
55
+
56
+ def cooldowns_finished?(channel, user)
57
+ # Normal usage stuff starts here, check and see if the channel time is up
58
+ if channel_cooldown_finished?(channel)
59
+ # channel cd is up, check per user by checking if the user's even triggered a cd yet
60
+ if shared[:cooldown][channel].key?(user)
61
+ # User's in the config, check time
62
+ if user_cooldown_finished?(channel, user)
63
+ # Their time's up, run the command
64
+ trigger_cooldown_for(channel, user)
44
65
  return true
45
66
  end
67
+ else
68
+ # User's not used bot before, run the command
69
+ trigger_cooldown_for(channel, user)
70
+ return true
46
71
  end
47
-
48
- message = "Sorry, you'll have to wait "
49
- unless channel_cooldown_finished?(channel)
50
- message << "#{Cinch::Toolbox.time_format(channel_time_remaining(channel))} before I can talk in the channel again, and "
51
- end
52
- message << "#{Cinch::Toolbox.time_format(user_time_remaining(channel, nick))} before your nick can use any commands."
53
-
54
- m.user.notice message
55
- return false
56
72
  end
57
73
  end
58
74
 
59
- def user_cooldown_finished?(chan, user)
60
- shared[:cooldown][:config][chan]['user'] < user_time_elapsed(chan, user)
75
+ def cooldown_message(channel, user)
76
+ message = ['Sorry, you\'ll have to wait']
77
+ unless channel_cooldown_finished?(channel)
78
+ message << TimeLord::Period.new(cooldown_channel_expire_time(channel), Time.now).to_words
79
+ message << 'before I can talk in the channel again, and'
80
+ end
81
+ message << TimeLord::Period.new(cooldown_user_expire_time(channel, user), Time.now).to_words
82
+ message << 'before you can use any commands.'
83
+
84
+ return message.join(' ')
61
85
  end
62
86
 
63
- def channel_cooldown_finished?(chan)
64
- shared[:cooldown][:config][chan]['global'] < channel_time_elapsed(chan)
87
+ def trigger_cooldown_for(channel, user)
88
+ shared[:cooldown][channel] = { :global => Time.now, user => Time.now }
65
89
  end
66
90
 
67
- def channel_time_remaining(chan)
68
- shared[:cooldown][:config][chan]['global'] - channel_time_elapsed(chan)
91
+ def cooldown_channel_expire_time(channel)
92
+ shared[:cooldown][channel][:global] + shared[:cooldown][:config][channel][:global]
69
93
  end
70
94
 
71
- def user_time_remaining(chan, user)
72
- shared[:cooldown][:config][chan]['user'] - user_time_elapsed(chan, user)
95
+ def cooldown_user_expire_time(channel, user)
96
+ shared[:cooldown][channel][user] + shared[:cooldown][:config][channel][:user]
73
97
  end
74
98
 
75
- def channel_time_elapsed(chan)
76
- (Time.now - shared[:cooldown][chan]['global']).floor
99
+ def user_cooldown_finished?(channel, user)
100
+ cooldown = shared[:cooldown][:config][channel][:user]
101
+ elapsed = Time.now - shared[:cooldown][channel][user]
102
+ return cooldown < elapsed
77
103
  end
78
104
 
79
- def user_time_elapsed(chan, user)
80
- (Time.now - shared[:cooldown][chan][user]).floor
105
+ def channel_cooldown_finished?(channel)
106
+ cooldown = shared[:cooldown][:config][channel][:global]
107
+ elapsed = Time.now - shared[:cooldown][channel][:global]
108
+ return cooldown < elapsed
81
109
  end
82
110
  end
83
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-cooldown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,16 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-02 00:00:00.000000000 Z
12
+ date: 2013-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: cinch-toolbox
15
+ name: time-lord
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 1.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 1.0.1
30
30
  description: This gem allows you to set a shared timer across plugins that are configured
31
31
  to respect it.
32
32
  email: