qwtf_discord_bot 5.1.4 → 5.1.11

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: 2764268ae2786e3217f973d14b910bbed79dd4b1d4b822ebaee31b4f555c9945
4
- data.tar.gz: ec2f3fd80b0eaaa222ca9cac071763ef3220190991f94e37a112025002b76cec
3
+ metadata.gz: 9f929cda85f6a53ffc63d04e3a85b6ca4bcbcadf72b2c0ab70eab8a1a549843a
4
+ data.tar.gz: 45b0e4b9bba67fdfb8139d3904176ce3443d43ea7cb74c750588f4eb6ea1fb8d
5
5
  SHA512:
6
- metadata.gz: 7d86a1207ec73b9225c0a72d530000a6e7ce3297e490bbb914dd45f181be7b1a214c67242b35274545c1f92c4d11911c42e5b6434fc5ecc1dddae33303bfff72
7
- data.tar.gz: b654a7a3c136c4847dd9aefc0f4e7e695b7f7dc4a8a8ad521535096d10385c17397f2324bb0a65393da99207934f719523b00a9a1754dd76a368d5bd0001ce75
6
+ metadata.gz: cca2b1392fbbdca102e22ea27e79774da07478e5cfa2a9a2ee14c4b6ae48ae1ea3c8e4c8906461d123a1f32f3e2e0cef519f107a3b7abf42d1853a5e870bf342
7
+ data.tar.gz: d29bd7a09c9ffed879429965497118d0f63332bac8084c83ea498601d924d11227adad67694de73f97658b73ed9c7c90236fc855bb8c82a809a522fe0ecb78f6
data/README.md CHANGED
@@ -89,8 +89,9 @@ This responds to discord messages:
89
89
  - `!join`
90
90
  - `!leave`
91
91
  - `!status`
92
- - `!maxplayers <no_of_players>`
93
- - `!notify <roles>`
92
+ - `!kick <@player>`
93
+ - `!teamsize <no_of_players>`
94
+ - `!notify <@role @role2>`
94
95
  - `!end`
95
96
 
96
97
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.1.4
1
+ 5.1.11
@@ -24,13 +24,19 @@ class EventDecorator
24
24
  end
25
25
 
26
26
  def mentions_for(user_ids)
27
- find_users(user_ids).map(&:mention)
27
+ find_users(user_ids).map do |user|
28
+ user&.mention
29
+ end
28
30
  end
29
31
 
30
32
  def display_names_for(user_ids)
31
33
  find_users(user_ids).map(&:display_name)
32
34
  end
33
35
 
36
+ def display_name_for(user_id)
37
+ find_user(user_id) && find_user(user_id).display_name
38
+ end
39
+
34
40
  private
35
41
 
36
42
  def find_users(user_ids)
data/lib/pug.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  class Pug
2
- DEFAULT_MAXPLAYERS = 8
2
+ DEFAULT_teamsize = 4
3
+ NO_OF_TEAMS = 2
3
4
 
4
5
  def self.for(channel_id)
5
6
  new(channel_id)
@@ -18,6 +19,15 @@ class Pug
18
19
  redis.smembers(players_key).map(&:to_i)
19
20
  end
20
21
 
22
+ def team(no)
23
+ index = no - 1
24
+ joined_players.each_slice(teamsize).to_a[index]
25
+ end
26
+
27
+ def teamsize=(teamsize)
28
+ redis.set(teamsize_key, teamsize)
29
+ end
30
+
21
31
  def full?
22
32
  joined_player_count >= maxplayers
23
33
  end
@@ -42,12 +52,12 @@ class Pug
42
52
  redis.get(notify_roles_key) || "@here"
43
53
  end
44
54
 
45
- def maxplayers=(maxplayers)
46
- redis.set(maxplayers_key, maxplayers)
55
+ def teamsize=(teamsize)
56
+ redis.set(teamsize_key, teamsize)
47
57
  end
48
58
 
49
- def maxplayers
50
- (redis.get(maxplayers_key) || DEFAULT_MAXPLAYERS).to_i
59
+ def teamsize
60
+ (redis.get(teamsize_key) || DEFAULT_teamsize).to_i
51
61
  end
52
62
 
53
63
  def active?
@@ -56,10 +66,7 @@ class Pug
56
66
 
57
67
  def leave(player_id)
58
68
  redis.srem(players_key, player_id)
59
- end
60
-
61
- def empty?
62
- joined_player_count == 0
69
+ end_pug if empty?
63
70
  end
64
71
 
65
72
  def end_pug
@@ -67,10 +74,22 @@ class Pug
67
74
  redis.del(players_key)
68
75
  end
69
76
 
77
+ def joined?(player_id)
78
+ joined_players.include?(player_id)
79
+ end
80
+
81
+ def maxplayers
82
+ teamsize * NO_OF_TEAMS
83
+ end
84
+
70
85
  private
71
86
 
72
- def maxplayers_key
73
- [pug_key, "maxplayers"].join(":")
87
+ def empty?
88
+ joined_player_count.zero?
89
+ end
90
+
91
+ def teamsize_key
92
+ [pug_key, "teamsize"].join(":")
74
93
  end
75
94
 
76
95
  def players_key
@@ -85,6 +104,10 @@ class Pug
85
104
  [pug_key, "role"].join(":")
86
105
  end
87
106
 
107
+ def teamsize_key
108
+ [pug_key, "teamsize"].join(":")
109
+ end
110
+
88
111
  def redis
89
112
  Redis.current
90
113
  end
@@ -6,8 +6,6 @@ require 'event_decorator'
6
6
  class QwtfDiscordBotPug # :nodoc:
7
7
  include QwtfDiscordBot
8
8
 
9
- FOUR_HOURS = 4 * 60 * 60
10
-
11
9
  def run
12
10
  bot = Discordrb::Commands::CommandBot.new(
13
11
  token: QwtfDiscordBot.config.token,
@@ -17,131 +15,143 @@ class QwtfDiscordBotPug # :nodoc:
17
15
  )
18
16
 
19
17
  bot.command :join do |event, *args|
20
- set_pug(event) do |e, pug|
21
- if pug.joined_players.include?(e.user_id)
22
- message = "You've already joined"
23
- send_and_log_message(message, e.channel)
18
+ setup_pug(event) do |e, pug|
19
+ return send_msg("You've already joined", e.channel) if pug.joined?(e.user_id)
20
+
21
+ pug.join(e.user_id)
22
+
23
+ if pug.joined_player_count == 1
24
+ snippets = ["#{e.display_name} creates a PUG", pug.player_slots, pug.notify_roles]
24
25
  else
25
- pug.join(e.user_id)
26
-
27
- message = if pug.joined_player_count == 1
28
- [
29
- "#{e.display_name} creates a PUG",
30
- pug.player_slots,
31
- pug.notify_roles
32
- ].join(' | ')
33
- elsif pug.slots_left.between?(1,3)
34
- [
35
- "#{e.display_name} joins the PUG",
36
- pug.player_slots,
37
- "#{pug.slots_left} more",
38
- pug.notify_roles
39
- ].join(' | ')
40
- else
41
- [
42
- "#{e.display_name} joins the PUG",
43
- pug.player_slots
44
- ].join(' | ')
45
- end
46
-
47
- send_and_log_message(message, e.channel)
48
-
49
- if pug.full?
50
- message = start_pug(
51
- pug.player_slots,
52
- e.mentions_for(pug.joined_players)
53
- )
54
-
55
- send_and_log_message(message, e.channel)
56
- end
26
+ snippets = ["#{e.display_name} joins the PUG", pug.player_slots]
27
+ snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left.between?(1, 3)
57
28
  end
29
+
30
+ send_msg(snippets.join(' | '), e.channel)
31
+
32
+ start_pug(pug, e) if pug.full?
58
33
  end
59
34
  end
60
35
 
61
36
  bot.command :status do |event, *args|
62
- set_pug(event) do |e, pug|
63
- message = if pug.active?
64
- [
65
- "#{e.display_names_for(pug.joined_players).join(', ')} joined",
66
- pug.player_slots
67
- ].join(' | ')
68
- else
69
- 'No PUG has been started. `!join` to create'
70
- end
71
-
72
- send_and_log_message(message, e.channel)
37
+ setup_pug(event) do |e, pug|
38
+ msg = if pug.active?
39
+ [
40
+ "#{e.display_names_for(pug.joined_players).join(', ')} joined",
41
+ pug.player_slots
42
+ ].join(' | ')
43
+ else
44
+ 'No PUG has been started. `!join` to create'
45
+ end
46
+
47
+ send_msg(msg, e.channel)
73
48
  end
74
49
  end
75
50
 
76
- bot.command :maxplayers do |event, *args|
77
- set_pug(event) do |e, pug|
78
- new_maxplayers = args[0]
79
-
80
- message = if new_maxplayers
81
- pug.maxplayers = new_maxplayers
82
- "Max number of players set to #{pug.maxplayers} | #{pug.player_slots} joined"
83
- else
84
- "Current max number of players is #{pug.maxplayers} | #{pug.player_slots} joined"
85
- end
51
+ bot.command :teamsize do |event, *args|
52
+ setup_pug(event) do |e, pug|
53
+ new_teamsize = args[0]
86
54
 
87
- send_and_log_message(message, e.channel)
55
+ if new_teamsize
56
+ pug.teamsize = new_teamsize
88
57
 
89
- if pug.full?
90
- message = start_pug(
91
- pug.player_slots,
92
- e.mentions_for(pug.joined_players)
58
+ send_msg(
59
+ "Team size set to #{pug.teamsize} | #{pug.player_slots} joined",
60
+ e.channel
93
61
  )
94
62
 
95
- send_and_log_message(message, e.channel)
63
+ start_pug(pug, e) if pug.full?
64
+ else
65
+ send_msg(
66
+ "Current team size is #{pug.teamsize} | #{pug.player_slots} joined",
67
+ e.channel
68
+ )
96
69
  end
97
70
  end
98
71
  end
99
72
 
100
73
  bot.command :leave do |event, *args|
101
- set_pug(event) do |e, pug|
102
- if !pug.active?
103
- message = "There's no active PUG to leave"
104
- send_and_log_message(message, e.channel)
105
- elsif !pug.joined_players.include?(e.user_id)
106
- message = "You're not in the PUG"
107
- send_and_log_message(message, e.channel)
108
- else
109
- pug.leave(e.user_id)
110
- message = "#{e.display_name} leaves the PUG | #{pug.player_slots} remain"
111
- send_and_log_message(message, e.channel)
74
+ setup_pug(event) do |e, pug|
75
+ return send_msg(no_active_pug_message, e.channel) unless pug.active?
76
+ return send_msg("You're not in the PUG", e.channel) unless pug.joined?(e.user_id)
77
+
78
+ pug.leave(e.user_id)
79
+
80
+ snippets = [
81
+ "#{e.display_name} leaves the PUG",
82
+ "#{pug.player_slots} remain"
83
+ ]
84
+
85
+ snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
86
+
87
+ send_msg(
88
+ snippets.join(' | '),
89
+ e.channel
90
+ )
91
+
92
+ send_msg(end_pug_message, e.channel) unless pug.active?
93
+ end
94
+ end
112
95
 
113
- if pug.empty?
114
- message = end_pug(pug)
115
- send_and_log_message(message, e.channel)
96
+ bot.command :kick do |event, *args|
97
+ setup_pug(event) do |e, pug|
98
+ return send_msg(no_active_pug_message, e.channel) unless pug.active?
99
+
100
+ args.each do |arg|
101
+ unless arg.match(/<@!\d+>/)
102
+ send_msg("#{arg} isn't a valid mention", e.channel)
103
+ next
116
104
  end
105
+
106
+ user_id = arg[3..-2].to_i
107
+ display_name = e.display_name_for(user_id) || arg
108
+
109
+ unless pug.joined?(user_id)
110
+ send_msg("#{display_name} isn't in the PUG", e.channel)
111
+ next
112
+ end
113
+
114
+ pug.leave(user_id)
115
+
116
+ snippets = [
117
+ "#{display_name} is kicked from the PUG",
118
+ "#{pug.player_slots} remain"
119
+ ]
120
+
121
+ snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
122
+
123
+ send_msg(
124
+ snippets.join(' | '),
125
+ e.channel
126
+ )
127
+
128
+ break send_msg(end_pug_message, e.channel) unless pug.active?
117
129
  end
118
130
  end
119
131
  end
120
132
 
121
133
  bot.command :end do |event, *args|
122
- set_pug(event) do |e, pug|
123
- message = if !pug.active?
124
- "There's no active PUG to end"
125
- else
126
- end_pug(pug)
127
- end
128
-
129
- send_and_log_message(message, e.channel)
134
+ setup_pug(event) do |e, pug|
135
+ return send_msg(no_active_pug_message, e.channel) unless pug.active?
136
+
137
+ pug.end_pug
138
+
139
+ send_msg(end_pug_message, e.channel)
130
140
  end
131
141
  end
132
142
 
133
143
  bot.command :notify do |event, *args|
134
- set_pug(event) do |e, pug|
144
+ setup_pug(event) do |e, pug|
135
145
  roles = args.join(' ')
136
146
  pug.notify_roles = roles
137
147
 
138
- message = if roles.empty?
139
- 'Notification removed'
140
- else
141
- "Notification role set to #{roles}"
142
- end
148
+ msg = if roles.empty?
149
+ 'Notification removed'
150
+ else
151
+ "Notification role set to #{roles}"
152
+ end
143
153
 
144
- send_and_log_message(message, e.channel)
154
+ send_msg(msg, e.channel)
145
155
  end
146
156
  end
147
157
 
@@ -150,26 +160,36 @@ class QwtfDiscordBotPug # :nodoc:
150
160
 
151
161
  private
152
162
 
153
- def set_pug(event)
163
+ def setup_pug(event)
154
164
  e = EventDecorator.new(event)
155
165
  pug = Pug.for(e.channel_id)
156
166
  yield(e, pug)
167
+ nil # stop discordrb printing return value
157
168
  end
158
169
 
159
- def start_pug(player_slots, mentions)
160
- [
170
+ def start_pug(pug, event)
171
+ msg = [
161
172
  'Time to play!',
162
- player_slots,
163
- mentions.join(' ')
164
- ].join(' | ')
173
+ ['Team 1:', event.mentions_for(pug.team(1)).join(' ')].join(' '),
174
+ ['Team 2:', event.mentions_for(pug.team(2)).join(' ')].join(' ')
175
+ ].join("\n")
176
+
177
+ send_msg(msg, event.channel)
165
178
  end
166
179
 
167
- def end_pug(pug)
168
- pug.end_pug
180
+ def start_pug_send_msg(player_slots:, mentions:)
181
+ ['Time to play!', player_slots, mentions.join(' ')].join(' | ')
182
+ end
183
+
184
+ def end_pug_message
169
185
  'PUG ended'
170
186
  end
171
187
 
172
- def send_and_log_message(message, channel)
188
+ def no_active_pug_message
189
+ "There's no active PUG"
190
+ end
191
+
192
+ def send_msg(message, channel)
173
193
  channel.send_message(message) && puts(message)
174
194
  end
175
195
  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.1.4
4
+ version: 5.1.11
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-25 00:00:00.000000000 Z
11
+ date: 2020-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb