qwtf_discord_bot 5.0.2 → 5.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 452714fe7a833e8c33d3825de55e52be1716f30fecd8c80df2705aa35df37bc7
4
- data.tar.gz: bafc402721ac62475747ffe255b7f09f851672fff58ec05e89051d765282ce5b
3
+ metadata.gz: 9a23eb8820860898165bbdf94e448a7141449fa3850a8e66f079e93ca8590f0f
4
+ data.tar.gz: 33ea995e466b5379746ee57b12e24f3337581dd0c58e4e526b30eb2a9377778d
5
5
  SHA512:
6
- metadata.gz: 17a594fdf8f231e4b067496cf59b9fdb777231ac9585c4df93df061948580100c5235bc4e7e31281a4606d22e6ca490e863aa13f2783b66292b73b599cca2bbc
7
- data.tar.gz: 8fb14f30321620378acb31553bbfb89c0432b4ab58b4f69c7f27a294db94ff26686851e819b3cb74c2125ac6c2a8b5c509204eb90f70155c2af41e869960cae7
6
+ metadata.gz: 53393cfca043b1ff72cbc2a7f9e28d9a7f4c44381990ba23edf392f23afb84c66715e5ff274573d16587797fad5c9e76739edb67039eb72106f4767faf95e368
7
+ data.tar.gz: ae90377b7c6c4592e6811a91461ab13a3e8650d17c36a67f8c45717930e2956c738123c16db087c036508a0da1898fc0d0a1f5efac71a9149ce809da8d277067
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qwtf_discord_bot (4.2.6)
4
+ qwtf_discord_bot (5.0.4)
5
5
  discordrb (~> 3.3)
6
6
  redis (~> 4.1)
7
7
  thor (~> 0.20)
@@ -9,14 +9,15 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activesupport (5.2.3)
12
+ activesupport (6.0.3.2)
13
13
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
14
  i18n (>= 0.7, < 2)
15
15
  minitest (~> 5.1)
16
16
  tzinfo (~> 1.1)
17
- coderay (1.1.2)
18
- concurrent-ruby (1.1.5)
19
- diff-lcs (1.3)
17
+ zeitwerk (~> 2.2, >= 2.2.2)
18
+ coderay (1.1.3)
19
+ concurrent-ruby (1.1.7)
20
+ diff-lcs (1.4.4)
20
21
  discordrb (3.3.0)
21
22
  discordrb-webhooks (~> 3.3.0)
22
23
  ffi (>= 1.9.24)
@@ -26,60 +27,61 @@ GEM
26
27
  websocket-client-simple (>= 0.3.0)
27
28
  discordrb-webhooks (3.3.0)
28
29
  rest-client (>= 2.1.0.rc1)
29
- domain_name (0.5.20180417)
30
+ domain_name (0.5.20190701)
30
31
  unf (>= 0.0.5, < 1.0.0)
31
32
  event_emitter (0.2.6)
32
- factory_bot (5.0.2)
33
- activesupport (>= 4.2.0)
34
- ffi (1.11.1)
33
+ factory_bot (6.1.0)
34
+ activesupport (>= 5.0.0)
35
+ ffi (1.13.1)
35
36
  http-accept (1.7.0)
36
37
  http-cookie (1.0.3)
37
38
  domain_name (~> 0.5)
38
- i18n (1.6.0)
39
+ i18n (1.8.5)
39
40
  concurrent-ruby (~> 1.0)
40
- method_source (0.9.2)
41
- mime-types (3.2.2)
41
+ method_source (1.0.0)
42
+ mime-types (3.3.1)
42
43
  mime-types-data (~> 3.2015)
43
- mime-types-data (3.2019.0331)
44
- minitest (5.11.3)
44
+ mime-types-data (3.2020.0512)
45
+ minitest (5.14.1)
45
46
  netrc (0.11.0)
46
47
  opus-ruby (1.0.1)
47
48
  ffi
48
- pry (0.12.2)
49
- coderay (~> 1.1.0)
50
- method_source (~> 0.9.0)
49
+ pry (0.13.1)
50
+ coderay (~> 1.1)
51
+ method_source (~> 1.0)
51
52
  rbnacl (3.4.0)
52
53
  ffi
53
- redis (4.1.3)
54
- rest-client (2.1.0.rc1)
54
+ redis (4.2.1)
55
+ rest-client (2.1.0)
55
56
  http-accept (>= 1.7.0, < 2.0)
56
57
  http-cookie (>= 1.0.2, < 2.0)
57
58
  mime-types (>= 1.16, < 4.0)
58
59
  netrc (~> 0.8)
59
- rspec (3.8.0)
60
- rspec-core (~> 3.8.0)
61
- rspec-expectations (~> 3.8.0)
62
- rspec-mocks (~> 3.8.0)
63
- rspec-core (3.8.1)
64
- rspec-support (~> 3.8.0)
65
- rspec-expectations (3.8.4)
60
+ rspec (3.9.0)
61
+ rspec-core (~> 3.9.0)
62
+ rspec-expectations (~> 3.9.0)
63
+ rspec-mocks (~> 3.9.0)
64
+ rspec-core (3.9.2)
65
+ rspec-support (~> 3.9.3)
66
+ rspec-expectations (3.9.2)
66
67
  diff-lcs (>= 1.2.0, < 2.0)
67
- rspec-support (~> 3.8.0)
68
- rspec-mocks (3.8.1)
68
+ rspec-support (~> 3.9.0)
69
+ rspec-mocks (3.9.1)
69
70
  diff-lcs (>= 1.2.0, < 2.0)
70
- rspec-support (~> 3.8.0)
71
- rspec-support (3.8.2)
71
+ rspec-support (~> 3.9.0)
72
+ rspec-support (3.9.3)
72
73
  thor (0.20.3)
73
74
  thread_safe (0.3.6)
74
- tzinfo (1.2.5)
75
+ tzinfo (1.2.7)
75
76
  thread_safe (~> 0.1)
76
77
  unf (0.1.4)
77
78
  unf_ext
78
- unf_ext (0.0.7.6)
79
+ unf_ext (0.0.7.7)
79
80
  websocket (1.2.8)
80
81
  websocket-client-simple (0.3.0)
81
82
  event_emitter
82
83
  websocket
84
+ zeitwerk (2.4.0)
83
85
 
84
86
  PLATFORMS
85
87
  ruby
data/README.md CHANGED
@@ -90,7 +90,8 @@ This responds to discord messages:
90
90
  - `!leave`
91
91
  - `!status`
92
92
  - `!maxplayers <no_of_players>`
93
- - `!leave`
93
+ - `!notify <roles>`
94
+ - `!end`
94
95
 
95
96
 
96
97
  #### Watcher
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.0.2
1
+ 5.1.2
@@ -0,0 +1,54 @@
1
+ class EventDecorator
2
+ def initialize(event)
3
+ @event = event
4
+ end
5
+
6
+ def channel
7
+ @event.channel
8
+ end
9
+
10
+ def channel_id
11
+ @event.channel.id
12
+ end
13
+
14
+ def username
15
+ user.username
16
+ end
17
+
18
+ def user_id
19
+ user.id
20
+ end
21
+
22
+ def users
23
+ server.users
24
+ end
25
+
26
+ def mentions_for(user_ids)
27
+ find_users(user_ids).map(&:mention)
28
+ end
29
+
30
+ def usernames_for(user_ids)
31
+ find_users(user_ids).map(&:username)
32
+ end
33
+
34
+ private
35
+
36
+ def find_users(user_ids)
37
+ user_ids.map do |user_id|
38
+ find_user(user_id)
39
+ end
40
+ end
41
+
42
+ def find_user(user_id)
43
+ users.find { |user| user.id == user_id }
44
+ end
45
+
46
+ def server
47
+ @event.server
48
+ end
49
+
50
+ def user
51
+ @event.user
52
+ end
53
+ end
54
+
@@ -0,0 +1,91 @@
1
+ class Pug
2
+ DEFAULT_MAXPLAYERS = 8
3
+
4
+ def self.for(channel_id)
5
+ new(channel_id)
6
+ end
7
+
8
+ def initialize(channel_id)
9
+ @channel_id = channel_id
10
+ end
11
+
12
+ def join(user_id)
13
+ redis.setnx(pug_key, Time.now)
14
+ redis.sadd(players_key, user_id)
15
+ end
16
+
17
+ def joined_players
18
+ redis.smembers(players_key).map(&:to_i)
19
+ end
20
+
21
+ def full?
22
+ joined_player_count >= maxplayers
23
+ end
24
+
25
+ def joined_player_count
26
+ redis.scard(players_key).to_i
27
+ end
28
+
29
+ def player_slots
30
+ "#{joined_player_count}/#{maxplayers}"
31
+ end
32
+
33
+ def slots_left
34
+ maxplayers - joined_player_count
35
+ end
36
+
37
+ def notify_roles=(roles)
38
+ redis.set(notify_roles_key, roles)
39
+ end
40
+
41
+ def notify_roles
42
+ redis.get(notify_roles_key) || "@here"
43
+ end
44
+
45
+ def maxplayers=(maxplayers)
46
+ redis.set(maxplayers_key, maxplayers)
47
+ end
48
+
49
+ def maxplayers
50
+ (redis.get(maxplayers_key) || DEFAULT_MAXPLAYERS).to_i
51
+ end
52
+
53
+ def active?
54
+ redis.get(pug_key)
55
+ end
56
+
57
+ def leave(player_id)
58
+ redis.srem(players_key, player_id)
59
+ end
60
+
61
+ def empty?
62
+ joined_player_count == 0
63
+ end
64
+
65
+ def end_pug
66
+ redis.del(pug_key)
67
+ redis.del(players_key)
68
+ end
69
+
70
+ private
71
+
72
+ def maxplayers_key
73
+ [pug_key, "maxplayers"].join(":")
74
+ end
75
+
76
+ def players_key
77
+ [pug_key, "players"].join(":")
78
+ end
79
+
80
+ def pug_key
81
+ ["pug", "channel", @channel_id].join(":")
82
+ end
83
+
84
+ def notify_roles_key
85
+ [pug_key, "role"].join(":")
86
+ end
87
+
88
+ def redis
89
+ Redis.current
90
+ end
91
+ end
@@ -1,6 +1,9 @@
1
- require 'event_wrapper'
1
+ # frozen_string_literal: true
2
2
 
3
- class QwtfDiscordBotPug
3
+ require 'pug'
4
+ require 'event_decorator'
5
+
6
+ class QwtfDiscordBotPug # :nodoc:
4
7
  include QwtfDiscordBot
5
8
 
6
9
  FOUR_HOURS = 4 * 60 * 60
@@ -14,109 +17,127 @@ class QwtfDiscordBotPug
14
17
  )
15
18
 
16
19
  bot.command :join do |event, *args|
17
- e = EventWrapper.new(event)
20
+ set_pug(event, args) do |e, pug|
21
+ pug.join(e.user_id)
22
+
23
+ message = if pug.joined_player_count == 1
24
+ [
25
+ "#{e.username} creates a PUG",
26
+ pug.player_slots,
27
+ pug.notify_roles
28
+ ].join(' | ')
29
+ elsif pug.slots_left.between?(1,3)
30
+ [
31
+ "#{e.username} joins the PUG",
32
+ pug.player_slots,
33
+ "#{pug.slots_left} more",
34
+ pug.notify_roles
35
+ ].join(' | ')
36
+ else
37
+ [
38
+ "#{e.username} joins the PUG",
39
+ pug.player_slots
40
+ ].join(' | ')
41
+ end
18
42
 
19
- redis.setnx(e.pug_key, Time.now)
20
- redis.sadd(e.players_key, e.user_id)
43
+ send_and_log_message(message, e.channel)
21
44
 
22
- message = if e.joined_player_count == e.maxplayers
23
- mentions = joined_users(e).map do |user|
24
- user.mention
25
- end
26
- "Time to play! #{mentions.join(" ")}"
27
- elsif (e.joined_player_count == 1)
28
- [
29
- "#{e.username} creates a PUG",
30
- e.player_slots,
31
- e.role,
32
- ].join(" | ")
33
- elsif e.slots_left <= 3
34
- [
35
- "#{e.username} joins the PUG",
36
- e.player_slots,
37
- "#{e.slots_left} more",
38
- e.role,
39
- ].join(" | ")
40
- else
41
- [
42
- "#{e.username} joins the PUG",
43
- e.player_slots,
44
- ].join(" | ")
45
- end
46
-
47
- send_and_log_message(message, event)
45
+ if pug.full?
46
+ message = start_pug(
47
+ pug.player_slots,
48
+ e.mentions_for(pug.joined_players)
49
+ )
50
+
51
+ send_and_log_message(message, e.channel)
52
+ end
53
+ end
48
54
  end
49
55
 
50
56
  bot.command :status do |event, *args|
51
- e = EventWrapper.new(event)
52
- usernames = joined_users(e).map(&:username)
53
-
54
- message = [
55
- "#{usernames.join(", ")} joined",
56
- e.player_slots
57
- ].join(" | ")
57
+ set_pug(event, args) do |e, pug|
58
+ message = if pug.active?
59
+ [
60
+ "#{e.usernames_for(pug.joined_players).join(', ')} joined",
61
+ pug.player_slots
62
+ ].join(' | ')
63
+ else
64
+ 'No PUG has been started. `!join` to create'
65
+ end
58
66
 
59
- send_and_log_message(message, event)
67
+ send_and_log_message(message, e.channel)
68
+ end
60
69
  end
61
70
 
62
71
  bot.command :maxplayers do |event, *args|
63
- e = EventWrapper.new(event)
64
- new_maxplayers = args[0]
65
-
66
- message = if new_maxplayers
67
- redis.set(e.maxplayers_key, new_maxplayers)
68
- "Max number of players set to #{e.maxplayers} | #{e.player_slots}"
69
- else
70
- "Current max number of players is #{e.maxplayers} | #{e.player_slots}"
71
- end
72
+ set_pug(event, args) do |e, pug|
73
+ new_maxplayers = args[0]
74
+
75
+ message = if new_maxplayers
76
+ pug.maxplayers = new_maxplayers
77
+ "Max number of players set to #{pug.maxplayers} | #{pug.player_slots} joined"
78
+ else
79
+ "Current max number of players is #{pug.maxplayers} | #{pug.player_slots} joined"
80
+ end
72
81
 
73
- send_and_log_message(message, event)
82
+ send_and_log_message(message, e.channel)
74
83
 
75
- if e.joined_player_count >= e.maxplayers
76
- mentions = joined_users(e).map do |user|
77
- user.mention
78
- end
84
+ if pug.full?
85
+ message = start_pug(
86
+ pug.player_slots,
87
+ e.mentions_for(pug.joined_players)
88
+ )
79
89
 
80
- message = "Time to play! #{mentions.join(" ")}"
81
- send_and_log_message(message, event)
90
+ send_and_log_message(message, e.channel)
91
+ end
82
92
  end
83
93
  end
84
94
 
85
95
  bot.command :leave do |event, *args|
86
- e = EventWrapper.new(event)
87
-
88
- redis.srem(e.players_key, e.user_id)
89
-
90
- message = "#{e.username} leaves the PUG | #{e.player_slots}"
91
-
92
- send_and_log_message(message, event)
93
-
94
- if e.joined_player_count == 0
95
- redis.del(e.pug_key)
96
-
97
- message = "PUG ended"
98
- send_and_log_message(message, event)
96
+ set_pug(event, args) do |e, pug|
97
+ if !pug.active?
98
+ message = "There's no active PUG to leave"
99
+ send_and_log_message(message, e.channel)
100
+ elsif !pug.joined_players.include?(e.user_id)
101
+ message = "You're not in the PUG"
102
+ send_and_log_message(message, e.channel)
103
+ else
104
+ pug.leave(e.user_id)
105
+ message = "#{e.username} leaves the PUG | #{pug.player_slots} remain"
106
+ send_and_log_message(message, e.channel)
107
+
108
+ if pug.empty?
109
+ message = end_pug(pug)
110
+ send_and_log_message(message, e.channel)
111
+ end
112
+ end
99
113
  end
100
114
  end
101
115
 
102
116
  bot.command :end do |event, *args|
103
- e = EventWrapper.new(event)
104
-
105
- redis.del(e.pug_key)
106
- redis.del(e.players_key)
117
+ set_pug(event, args) do |e, pug|
118
+ message = if !pug.active?
119
+ "There's no active PUG to end"
120
+ else
121
+ end_pug(pug)
122
+ end
107
123
 
108
- message = "PUG ended"
109
- send_and_log_message(message, event)
124
+ send_and_log_message(message, e.channel)
125
+ end
110
126
  end
111
127
 
112
- bot.command :role do |event, *args|
113
- e = EventWrapper.new(event)
114
- role = args[0]
128
+ bot.command :notify do |event, *args|
129
+ set_pug(event, args) do |e, pug|
130
+ roles = args.join(' ')
131
+ pug.notify_roles = roles
115
132
 
116
- redis.set(e.role_key, role)
133
+ message = if roles.empty?
134
+ 'Notification removed'
135
+ else
136
+ "Notification role set to #{roles}"
137
+ end
117
138
 
118
- message = "Notification role set to #{role}"
119
- send_and_log_message(message, event)
139
+ send_and_log_message(message, e.channel)
140
+ end
120
141
  end
121
142
 
122
143
  bot.run
@@ -124,16 +145,26 @@ class QwtfDiscordBotPug
124
145
 
125
146
  private
126
147
 
127
- def send_and_log_message(message, event)
128
- event.channel.send_message(message)
129
- puts message
148
+ def set_pug(event, *args)
149
+ e = EventDecorator.new(event)
150
+ pug = Pug.for(e.channel_id)
151
+ yield(e, pug)
130
152
  end
131
153
 
132
- def joined_users(event)
133
- redis.smembers(event.players_key).map do |user_id|
134
- event.users.find do |user|
135
- user.id.to_s == user_id
136
- end
137
- end
154
+ def start_pug(player_slots, mentions)
155
+ [
156
+ 'Time to play!',
157
+ player_slots,
158
+ mentions.join(' ')
159
+ ].join(' | ')
160
+ end
161
+
162
+ def end_pug(pug)
163
+ pug.end_pug
164
+ 'PUG ended'
165
+ end
166
+
167
+ def send_and_log_message(message, channel)
168
+ channel.send_message(message) && puts(message)
138
169
  end
139
170
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qwtf_discord_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.2
4
+ version: 5.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sheldon Johnson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-23 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb
@@ -104,8 +104,9 @@ files:
104
104
  - exe/qwtf_discord_bot
105
105
  - lib/emoji.rb
106
106
  - lib/endpoint.rb
107
- - lib/event_wrapper.rb
107
+ - lib/event_decorator.rb
108
108
  - lib/player.rb
109
+ - lib/pug.rb
109
110
  - lib/qstat_request.rb
110
111
  - lib/qwtf_discord_bot.rb
111
112
  - lib/qwtf_discord_bot/config.rb
@@ -1,58 +0,0 @@
1
- class EventWrapper
2
- include QwtfDiscordBot
3
-
4
- DEFAULT_MAXPLAYERS = 8
5
-
6
- def initialize(event)
7
- @event = event
8
- end
9
-
10
- def user_id
11
- @event.user.id
12
- end
13
-
14
- def username
15
- @event.user.username
16
- end
17
-
18
- def maxplayers_key
19
- [pug_key, "maxplayers"].join(":")
20
- end
21
-
22
- def maxplayers
23
- redis.setnx(maxplayers_key, DEFAULT_MAXPLAYERS)
24
- redis.get(maxplayers_key).to_i
25
- end
26
-
27
- def joined_player_count
28
- redis.scard(players_key).to_i
29
- end
30
-
31
- def slots_left
32
- maxplayers - joined_player_count
33
- end
34
-
35
- def pug_key
36
- ["pug", "channel", @event.channel.id].join(":")
37
- end
38
-
39
- def players_key
40
- [pug_key, "players"].join(":")
41
- end
42
-
43
- def player_slots
44
- "#{joined_player_count}/#{maxplayers}"
45
- end
46
-
47
- def users
48
- @event.server.users
49
- end
50
-
51
- def role_key
52
- [pug_key, "role"].join(":")
53
- end
54
-
55
- def role
56
- redis.get(role_key) || "@here"
57
- end
58
- end