qwtf_discord_bot 5.3.5 → 5.4.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: 1e4ea9a3df5bcc8c8cb14ba1d22a0708bdf8c4a994531d4c06fb8bbc75b33410
4
- data.tar.gz: 1568efc1c854b8d4d43d60fd0caa48b0e52e4b09ca04336b6b8c1969090fb146
3
+ metadata.gz: a0ecc26e71f4325677ded724c87d1f3496d1207d2cbd662146c09caba5c0d397
4
+ data.tar.gz: 3b5392f1a58fbbad660b17f2d88f8f8d29220e637069a353745963fc264ad0f4
5
5
  SHA512:
6
- metadata.gz: 50d22f311c0c29ce8df6f1a9c473559812a60d7602f79aa2a9976ffec4a44b6e2f096d6c7c7ac45d390569854393ea14b4ca2afa7f6b7213ba093330221bf48e
7
- data.tar.gz: cb34ac9dd1c0f5ff46c09700c1998c8e0cae344cc4b0944a40e2c8fad25bb46cfe3db8ab202e0079f20d982d1dfe74382cf01efe152484abc7f652a83008316f
6
+ metadata.gz: 45a536a9334a9caf56358b0b66aeffcdf9fc419e57e5ef09b33e0994b510e398bbeed76c9ba61d7513cc22dd4c3ebeca9c34e33de20aa70b8c5c340ae46c29d3
7
+ data.tar.gz: '018d569f3f282455e0e22cf73dac28f4dfb716242911a4130a7edefe41d598709da09d05efd086de5c41078eceb7ca8a4130f124926118f359480094c73b9015'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.3.5
1
+ 5.4.2
@@ -13,12 +13,24 @@ class QwtfDiscordBotPug # :nodoc:
13
13
  token: QwtfDiscordBot.config.token,
14
14
  client_id: QwtfDiscordBot.config.client_id,
15
15
  help_command: false,
16
- prefix: '!'
16
+ prefix: proc do |message|
17
+ match = /^\!(\w+)(.*)/.match(message.content)
18
+ if match
19
+ first = match[1]
20
+ rest = match[2]
21
+ # Return the modified string with the first word lowercase:
22
+ "#{first.downcase}#{rest}"
23
+ end
24
+ end
17
25
  )
18
26
 
27
+ bot.command :help do |event, *args|
28
+ "Pug commands: `!status`, `!join`, `!team <team_no>`, `!unteam`, `!leave`, `!kick <@player>`, `!win <team_no>`, `!draw`, `!end`, `!teamsize <no_of_players>`, `!addmap <map_name>`, `!removemap <map_name>`, `!maps`, `!map <map_name>`, `!notify <@role>`"
29
+ end
30
+
19
31
  bot.command :join do |event, *args|
20
32
  setup_pug(event) do |e, pug|
21
- return send_msg("You've already joined", e.channel) if pug.joined?(e.user_id)
33
+ return send_embedded_message("You've already joined", e.channel) if pug.joined?(e.user_id)
22
34
 
23
35
  join_pug(e, pug)
24
36
  start_pug(pug, e) if pug.full?
@@ -27,9 +39,9 @@ class QwtfDiscordBotPug # :nodoc:
27
39
 
28
40
  bot.command :status do |event, *args|
29
41
  setup_pug(event) do |e, pug|
30
- return send_msg('No PUG has been started. `!join` to create', e.channel) unless pug.active?
42
+ return send_embedded_message('No PUG has been started. `!join` to create', e.channel) unless pug.active?
31
43
 
32
- send_msg(
44
+ send_embedded_message(
33
45
  [
34
46
  "#{pug.player_slots} joined",
35
47
  "Map: #{pug.game_map}",
@@ -42,15 +54,15 @@ class QwtfDiscordBotPug # :nodoc:
42
54
 
43
55
  bot.command :teamsize do |event, *args|
44
56
  setup_pug(event) do |e, pug|
45
- return send_msg("Team size is #{pug.teamsize}", e.channel) unless args.any?
57
+ return send_embedded_message("Team size is #{pug.teamsize}", e.channel) unless args.any?
46
58
 
47
59
  new_teamsize = args[0].to_i
48
- return send_msg('Team size should be a number higher than 0', e.channel) unless new_teamsize > 0
60
+ return send_embedded_message('Team size should be a number higher than 0', e.channel) unless new_teamsize > 0
49
61
 
50
62
  if new_teamsize
51
63
  pug.teamsize = new_teamsize
52
64
 
53
- send_msg(
65
+ send_embedded_message(
54
66
  [
55
67
  "Team size set to #{pug.teamsize}",
56
68
  "#{pug.player_slots} joined"
@@ -60,7 +72,7 @@ class QwtfDiscordBotPug # :nodoc:
60
72
 
61
73
  start_pug(pug, e) if pug.full?
62
74
  else
63
- send_msg(
75
+ send_embedded_message(
64
76
  [
65
77
  "Current team size is #{pug.teamsize}",
66
78
  "#{pug.player_slots} joined"
@@ -73,8 +85,8 @@ class QwtfDiscordBotPug # :nodoc:
73
85
 
74
86
  bot.command :leave do |event, *args|
75
87
  setup_pug(event) do |e, pug|
76
- return send_msg(no_active_pug_message, e.channel) unless pug.active?
77
- return send_msg("You're not in the PUG", e.channel) unless pug.joined?(e.user_id)
88
+ return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
89
+ return send_embedded_message("You're not in the PUG", e.channel) unless pug.joined?(e.user_id)
78
90
 
79
91
  pug.leave(e.user_id)
80
92
 
@@ -85,7 +97,7 @@ class QwtfDiscordBotPug # :nodoc:
85
97
 
86
98
  snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
87
99
 
88
- send_msg(
100
+ send_embedded_message(
89
101
  snippets.join(MSG_SNIPPET_DELIMITER),
90
102
  e.channel
91
103
  )
@@ -96,12 +108,12 @@ class QwtfDiscordBotPug # :nodoc:
96
108
 
97
109
  bot.command :kick do |event, *args|
98
110
  setup_pug(event) do |e, pug|
99
- return send_msg("Kick who? e.g. `!kick @#{e.display_name}`", e.channel) unless args.any?
100
- return send_msg(no_active_pug_message, e.channel) unless pug.active?
111
+ return send_embedded_message("Kick who? e.g. `!kick @#{e.display_name}`", e.channel) unless args.any?
112
+ return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
101
113
 
102
114
  args.each do |arg|
103
115
  unless arg.match(/<@!\d+>/)
104
- send_msg("#{arg} isn't a valid mention", e.channel)
116
+ send_embedded_message("#{arg} isn't a valid mention", e.channel)
105
117
  next
106
118
  end
107
119
 
@@ -109,7 +121,7 @@ class QwtfDiscordBotPug # :nodoc:
109
121
  display_name = e.display_name_for(user_id) || arg
110
122
 
111
123
  unless pug.joined?(user_id)
112
- send_msg("#{display_name} isn't in the PUG", e.channel)
124
+ send_embedded_message("#{display_name} isn't in the PUG", e.channel)
113
125
  next
114
126
  end
115
127
 
@@ -122,7 +134,7 @@ class QwtfDiscordBotPug # :nodoc:
122
134
 
123
135
  snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1
124
136
 
125
- send_msg(
137
+ send_embedded_message(
126
138
  snippets.join(MSG_SNIPPET_DELIMITER),
127
139
  e.channel
128
140
  )
@@ -134,21 +146,20 @@ class QwtfDiscordBotPug # :nodoc:
134
146
 
135
147
  bot.command :team do |event, *args|
136
148
  setup_pug(event) do |e, pug|
137
- return send_msg("Which team? E.G. `!team 1`", e.channel) unless args.any?
138
-
139
- team_no = args[0].to_i
140
- return send_msg("Choose a team between 0 and 4", e.channel) unless team_no.between?(0, 4)
149
+ return send_embedded_message("Which team? E.G. `!team 1`", e.channel) unless args.any?
150
+ return send_embedded_message("Choose a team between 0 and 2", e.channel) unless ["0", "1", "2"].any?(args.first)
141
151
 
152
+ team_no = args.first.to_i
142
153
  pug_already_full = pug.full?
143
154
 
144
155
  if args.count == 1
145
156
  user_id = e.user_id
146
- return send_msg("You're already in team #{team_no}", e.channel) if pug.team(team_no).include?(user_id)
157
+ return send_embedded_message("You're already in team #{team_no}", e.channel) if pug.team(team_no).include?(user_id)
147
158
 
148
159
  join_pug(e, pug) unless pug.joined?(user_id)
149
160
  pug.join_team(team_no: team_no, player_id: user_id)
150
161
 
151
- send_msg(
162
+ send_embedded_message(
152
163
  [
153
164
  "#{e.display_name} joins team #{team_no}",
154
165
  "#{pug.team_player_count(team_no)}/#{pug.teamsize}"
@@ -157,7 +168,7 @@ class QwtfDiscordBotPug # :nodoc:
157
168
  else
158
169
  args[1..-1].each do |mention|
159
170
  unless mention.match(/<@!\d+>/)
160
- send_msg("#{arg} isn't a valid mention", e.channel)
171
+ send_embedded_message("#{arg} isn't a valid mention", e.channel)
161
172
  next
162
173
  end
163
174
 
@@ -165,13 +176,13 @@ class QwtfDiscordBotPug # :nodoc:
165
176
  display_name = e.display_name_for(user_id) || arg
166
177
 
167
178
  unless pug.joined?(user_id)
168
- send_msg("#{display_name} isn't in the PUG", e.channel)
179
+ send_embedded_message("#{display_name} isn't in the PUG", e.channel)
169
180
  next
170
181
  end
171
182
 
172
183
  pug.join_team(team_no: team_no, player_id: user_id)
173
184
 
174
- send_msg(
185
+ send_embedded_message(
175
186
  [
176
187
  "#{display_name} joins team #{team_no}",
177
188
  "#{pug.team_player_count(team_no)}/#{pug.teamsize}"
@@ -187,26 +198,30 @@ class QwtfDiscordBotPug # :nodoc:
187
198
  bot.command :unteam do |event, *args|
188
199
  setup_pug(event) do |e, pug|
189
200
  user_id = e.user_id
190
- return send_msg('No PUG has been started. `!join` to create', e.channel) unless pug.active?
191
- return send_msg("You aren't in this PUG", e.channel) unless pug.joined?(user_id)
192
- return send_msg("You aren't in a team", e.channel) if pug.team(0).include?(user_id)
201
+ return send_embedded_message('No PUG has been started. `!join` to create', e.channel) unless pug.active?
202
+ return send_embedded_message("You aren't in this PUG", e.channel) unless pug.joined?(user_id)
203
+ return send_embedded_message("You aren't in a team", e.channel) if pug.team(0).include?(user_id)
193
204
 
194
205
  pug.join_team(team_no: 0, player_id: user_id)
195
- send_msg("#{e.display_name} has no team", e.channel)
206
+ send_embedded_message("#{e.display_name} has no team", e.channel)
196
207
  end
197
208
  end
198
209
 
199
210
  bot.command :win do |event, *args|
200
211
  setup_pug(event) do |e, pug|
201
- return send_msg(no_active_pug_message, e.channel) unless pug.active?
212
+ return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
202
213
 
203
214
  winning_team_no = args[0]
204
215
 
205
- return send_msg("Not a valid team", e.channel) unless pug.team(winning_team_no).any?
216
+ return send_embedded_message("Not a valid team", e.channel) unless pug.team(winning_team_no).any?
206
217
 
207
- actual_teams = pug.teams.reject { |k| k == "0" }
218
+ if pug.actual_teams.count < 2
219
+ return send_embedded_message(
220
+ "There must be at least two teams with players to submit a result", e.channel
221
+ )
222
+ end
208
223
 
209
- team_results = actual_teams.inject({}) do |teams, (name, player_ids)|
224
+ team_results = pug.actual_teams.inject({}) do |teams, (name, player_ids)|
210
225
  players = player_ids.inject({}) do |memo, id|
211
226
  memo.merge({ id => e.display_name_for(id) })
212
227
  end
@@ -219,7 +234,11 @@ class QwtfDiscordBotPug # :nodoc:
219
234
  {
220
235
  match: {
221
236
  map: pug.game_map,
222
- teams: team_results
237
+ teams: team_results,
238
+ discord_channel: {
239
+ channel_id: e.channel_id,
240
+ name: "#{e.channel.server.name} ##{e.channel.name}"
241
+ }
223
242
  }
224
243
  }.to_json
225
244
  )
@@ -234,17 +253,21 @@ class QwtfDiscordBotPug # :nodoc:
234
253
  # e.display_name_for(player_id)
235
254
  # end
236
255
 
237
- send_msg("Team #{winning_team_no} wins", e.channel)
256
+ send_embedded_message("Team #{winning_team_no} wins", e.channel)
238
257
  end
239
258
  end
240
259
 
241
260
  bot.command :draw do |event, *args|
242
261
  setup_pug(event) do |e, pug|
243
- return send_msg(no_active_pug_message, e.channel) unless pug.active?
262
+ return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
244
263
 
245
- actual_teams = pug.teams.reject! { |k| k == "0" }
264
+ if pug.actual_teams.count < 2
265
+ return send_embedded_message(
266
+ "There must be at least two teams with players to submit a result", e.channel
267
+ )
268
+ end
246
269
 
247
- team_results = actual_teams.inject({}) do |teams, (name, player_ids)|
270
+ team_results = pug.actual_teams.inject({}) do |teams, (name, player_ids)|
248
271
  players = player_ids.inject({}) do |memo, id|
249
272
  memo.merge({ id => e.display_name_for(id) })
250
273
  end
@@ -256,18 +279,22 @@ class QwtfDiscordBotPug # :nodoc:
256
279
  {
257
280
  match: {
258
281
  map: pug.game_map,
259
- teams: team_results
282
+ teams: team_results,
283
+ discord_channel: {
284
+ channel_id: e.channel_id,
285
+ name: "#{e.channel.server.name} ##{e.channel.name}"
286
+ }
260
287
  }
261
288
  }.to_json
262
289
  )
263
290
 
264
- send_msg("Match drawn", e.channel)
291
+ send_embedded_message("Match drawn", e.channel)
265
292
  end
266
293
  end
267
294
 
268
295
  bot.command :end do |event, *args|
269
296
  setup_pug(event) do |e, pug|
270
- return send_msg(no_active_pug_message, e.channel) unless pug.active?
297
+ return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
271
298
 
272
299
  end_pug(pug, e.channel)
273
300
  end
@@ -276,47 +303,47 @@ class QwtfDiscordBotPug # :nodoc:
276
303
  bot.command :addmap do |event, *args|
277
304
  setup_pug(event) do |e, pug|
278
305
  maps = args
279
- return send_msg("What map? e.g. `!addmap 2fort5r`", e.channel) unless maps.any?
306
+ return send_embedded_message("What map? e.g. `!addmap 2fort5r`", e.channel) unless maps.any?
280
307
 
281
308
  pug.add_maps(maps)
282
- send_msg("#{maps.join(', ')} added to maps", e.channel)
309
+ send_embedded_message("#{maps.join(', ')} added to maps", e.channel)
283
310
  end
284
311
  end
285
312
 
286
313
  bot.command :removemap do |event, *args|
287
314
  setup_pug(event) do |e, pug|
288
315
  maps = args
289
- return send_msg("What map? e.g. `!removemap 2fort5r`", e.channel) unless maps.any?
316
+ return send_embedded_message("What map? e.g. `!removemap 2fort5r`", e.channel) unless maps.any?
290
317
 
291
318
  pug.remove_maps(maps)
292
- send_msg("#{maps.join(', ')} removed from maps", e.channel)
319
+ send_embedded_message("#{maps.join(', ')} removed from maps", e.channel)
293
320
  end
294
321
  end
295
322
 
296
323
  bot.command :maps do |event, *args|
297
324
  setup_pug(event) do |e, pug|
298
325
  maps = pug.maps
299
- return send_msg('No maps have been added. `!addmap`', e.channel) unless maps.any?
326
+ return send_embedded_message('No maps have been added. `!addmap`', e.channel) unless maps.any?
300
327
 
301
- send_msg(maps.join(', '), e.channel)
328
+ send_embedded_message(maps.join(', '), e.channel)
302
329
  end
303
330
  end
304
331
 
305
332
  bot.command :map do |event, *args|
306
333
  setup_pug(event) do |e, pug|
307
334
  maps = pug.maps
308
- return send_msg('No maps have been added. `!addmap`', e.channel) unless maps.any?
309
- return send_msg(no_active_pug_message, e.channel) unless pug.active?
335
+ return send_embedded_message('No maps have been added. `!addmap`', e.channel) unless maps.any?
336
+ return send_embedded_message(no_active_pug_message, e.channel) unless pug.active?
310
337
 
311
338
  if args.empty?
312
- return send_msg('No map has been set for the current PUG', e.channel) unless pug.game_map
313
- send_msg("Current map is #{pug.game_map}", e.channel)
339
+ return send_embedded_message('No map has been set for the current PUG', e.channel) unless pug.game_map
340
+ send_embedded_message("Current map is #{pug.game_map}", e.channel)
314
341
  else
315
342
  game_map = args.first
316
- return send_msg("#{game_map} isn't in the map list. `!addmap` to add it.", e.channel) unless maps.include?(game_map)
343
+ return send_embedded_message("#{game_map} isn't in the map list. `!addmap` to add it.", e.channel) unless maps.include?(game_map)
317
344
 
318
345
  pug.game_map = game_map
319
- send_msg("Map set to #{game_map}", e.channel)
346
+ send_embedded_message("Map set to #{game_map}", e.channel)
320
347
  end
321
348
  end
322
349
  end
@@ -332,7 +359,7 @@ class QwtfDiscordBotPug # :nodoc:
332
359
  "Notification role set to #{roles}"
333
360
  end
334
361
 
335
- send_msg(msg, e.channel)
362
+ send_embedded_message(msg, e.channel)
336
363
  end
337
364
  end
338
365
 
@@ -355,7 +382,7 @@ class QwtfDiscordBotPug # :nodoc:
355
382
  snippets << "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left.between?(1, 3)
356
383
  end
357
384
 
358
- send_msg(snippets.join(MSG_SNIPPET_DELIMITER), e.channel)
385
+ send_embedded_message(snippets.join(MSG_SNIPPET_DELIMITER), e.channel)
359
386
  end
360
387
 
361
388
  def setup_pug(event)
@@ -366,6 +393,16 @@ class QwtfDiscordBotPug # :nodoc:
366
393
  end
367
394
 
368
395
  def start_pug(pug, event)
396
+ if !pug.actual_teams.any?
397
+ teams = get_fair_teams(pug.joined_players)
398
+
399
+ teams.each do |team_no, player_ids|
400
+ player_ids.each do |player_id|
401
+ pug.join_team(team_no: team_no, player_id: player_id)
402
+ end
403
+ end
404
+ end
405
+
369
406
  pug_teams = pug.teams.map do |team_no, player_ids|
370
407
  team_mentions = player_ids.map do |player_id|
371
408
  event.mention_for(player_id)
@@ -383,7 +420,7 @@ class QwtfDiscordBotPug # :nodoc:
383
420
  pug_teams
384
421
  ].join("\n")
385
422
 
386
- send_msg(msg, event.channel)
423
+ send_embedded_message(msg, event.channel)
387
424
  end
388
425
 
389
426
  def pug_teams_message(pug, event)
@@ -413,15 +450,17 @@ class QwtfDiscordBotPug # :nodoc:
413
450
 
414
451
  def end_pug(pug, channel_id)
415
452
  pug.end_pug
416
- send_msg('PUG ended', channel_id)
453
+ send_embedded_message('PUG ended', channel_id)
417
454
  end
418
455
 
419
456
  def no_active_pug_message
420
457
  "There's no active PUG"
421
458
  end
422
459
 
423
- def send_msg(message, channel)
424
- channel.send_message(message) && puts(message)
460
+ def send_embedded_message(message, channel)
461
+ embed = Discordrb::Webhooks::Embed.new
462
+ embed.description = message
463
+ channel.send_embed(nil, embed) && puts(message)
425
464
  end
426
465
 
427
466
  def post_results(json)
@@ -432,4 +471,15 @@ class QwtfDiscordBotPug # :nodoc:
432
471
  http.request(req)
433
472
  end
434
473
  end
474
+
475
+ def get_fair_teams(players)
476
+ uri = URI("#{ENV['RATINGS_API_URL']}fair_teams/new")
477
+ params = { 'players[]' => players }
478
+ uri.query = URI.encode_www_form(params)
479
+ req = Net::HTTP::Get.new(uri)
480
+ res = Net::HTTP.start(uri.hostname, uri.port) do |http|
481
+ http.request(req)
482
+ end
483
+ JSON.parse(res.body).first.to_h
484
+ end
435
485
  end
@@ -7,6 +7,10 @@ class QwtfDiscordBotServer
7
7
  prefix: '!'
8
8
  )
9
9
 
10
+ bot.command :help do |event, *args|
11
+ "Server commands: `!active`, `!all`, `!server <address>`"
12
+ end
13
+
10
14
  bot.command :server do |event, *args|
11
15
  if args.empty?
12
16
  message = 'Provide a server address e.g. `!server ' \
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.3.5
4
+ version: 5.4.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-10-07 00:00:00.000000000 Z
11
+ date: 2020-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb