cinch-cooldown 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -1
- data/cinch-cooldown.gemspec +1 -1
- data/lib/cinch-cooldown/version.rb +1 -1
- data/lib/cinch-cooldown.rb +71 -43
- metadata +5 -5
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
|
-
|
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
|
|
data/cinch-cooldown.gemspec
CHANGED
data/lib/cinch-cooldown.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'cinch-cooldown/version'
|
2
|
-
require '
|
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
|
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
|
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
|
-
|
24
|
+
user = m.user.nick
|
23
25
|
|
24
|
-
|
26
|
+
# Make sure the configuration is sane.
|
27
|
+
return true if configuration_broken?(channel)
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
60
|
-
|
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
|
64
|
-
shared[:cooldown][:
|
87
|
+
def trigger_cooldown_for(channel, user)
|
88
|
+
shared[:cooldown][channel] = { :global => Time.now, user => Time.now }
|
65
89
|
end
|
66
90
|
|
67
|
-
def
|
68
|
-
shared[:cooldown][:config][
|
91
|
+
def cooldown_channel_expire_time(channel)
|
92
|
+
shared[:cooldown][channel][:global] + shared[:cooldown][:config][channel][:global]
|
69
93
|
end
|
70
94
|
|
71
|
-
def
|
72
|
-
shared[:cooldown][:config][
|
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
|
76
|
-
|
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
|
80
|
-
|
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
|
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-
|
12
|
+
date: 2013-05-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
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:
|
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:
|
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:
|