Class: QwtfDiscordBotPug

Inherits:
Object
  • Object
show all
Includes:
QwtfDiscordBot
Defined in:
lib/qwtf_discord_bot/qwtf_discord_bot_pug.rb

Overview

:nodoc:

Constant Summary collapse

MSG_SNIPPET_DELIMITER =
' ยท '
TEAM_NAMES =
{ 1 => "Blue", 2 => "Red" }
TEN_MINUTES =
10 * 60
VALID_MENTION =
/<@!?\d+>/
COMMANDS =
<<~MESSAGE
  `!status` Shows who has joined
  `!join [@player1] [@player2]` Join PUG. Can also join other players
  `!leave` Leave PUG
  `!kick <@player> [@player2]` Kick one or more other players
  `!team <team_no> [@player1] [@player2]` Join team
  `!unteam [@player1] [@player2]` Leave team and go to front of queue
  `!choose [n]` Choose fair teams. Pass number for nth fairest team
  `!shuffle` Choose random teams.
  `!win <team_no>` Report winning team
  `!draw` Report draw
  `!end` End PUG. Kicks all players
  `!teamsize <no_of_players>` Set number of players in a team
  `!addmap <map_name>` Add map to map list
  `!removemap <map_name>` Remove map from map list
  `!maps` Show map list
  `!map [map_name]` Show or set map
  `!notify <@role>` Set @role for alerts
MESSAGE
HELP =
{ commands: COMMANDS, footer: "!command <required> [optional]" }

Constants included from QwtfDiscordBot

QwtfDiscordBot::VERSION

Instance Method Summary collapse

Methods included from QwtfDiscordBot

config, config_file, #redis

Instance Method Details

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/qwtf_discord_bot/qwtf_discord_bot_pug.rb', line 37

def run
  bot = Discordrb::Commands::CommandBot.new(
    token: QwtfDiscordBot.config.token,
    client_id: QwtfDiscordBot.config.client_id,
    help_command: false,
    prefix: proc do |message|
      match = /^\!(\w+)(.*)/.match(message.content)
      if match
        first = match[1]
        rest = match[2]
        # Return the modified string with the first word lowercase:
        "#{first.downcase}#{rest}"
      end
    end
  )

  bot.command :help do |event, *args|
    send_embedded_message(
      description: HELP[:commands],
      channel: event.channel
    ) do |embed|
      embed.footer = Discordrb::Webhooks::EmbedFooter.new(
        text: HELP[:footer]
      )
    end
  end

  bot.command :join do |event, *args|
    setup_pug(event) do |e, pug|
      if args.empty?
        if pug.joined?(e.user_id)
          return send_embedded_message(
            description: "You've already joined",
            channel: e.channel
          )
        end

        join_pug(e, pug)
      else
        errors = []
        joiners = []

        args.each do |mention|
          if !mention.match(VALID_MENTION)
            errors << "#{mention} isn't a valid mention"
            next
          end

          user_id = mention_to_user_id(mention)
          display_name = e.display_name_for(user_id) || mention

          if pug.joined?(user_id)
            errors << "#{display_name} is already in this PUG"
            next
          end

          pug.join(user_id)
          joiners << display_name
        end

        message = ""
        description = []

        if pug.total_player_count == 0
          message = "#{pug.notify_roles} PUG started"
          description << "#{e.display_name} creates a PUG"
        elsif pug.slots_left.between?(1, 3)
          message = "#{pug.slots_left} more #{pug.notify_roles}"
        end

        if joiners.any?
          description << [
            joiners.to_sentence,
            joiners.count == 1 ? "joins" : "join",
            "the PUG"
          ].join(" ")
        end

        description << [
          pug.total_player_count,
          pug.maxplayers
        ].join("/")

        send_embedded_message(
          message: message,
          description: [errors, description.join(MSG_SNIPPET_DELIMITER)].join("\n"),
          channel: e.channel
        )
      end

      start_pug(pug, e) if pug.has_exactly_maxplayers?
    end
  end

  bot.command :choose do |event, *args|
    setup_pug(event) do |e, pug|
      if !pug.full?
        return send_embedded_message(
          description: "Not enough players, reduce !teamsize",
          channel: event.channel
        )
      end

      if args.any? && args.first.to_i < 1
        return send_embedded_message(
          description: "Choose a number higher than 0; e.g. `!choose 2`",
          channel: e.channel
        )
      end

      iteration = if args.any?
                    args.first.to_i - 1
                  else
                    0
                  end

      message_obj = choose_teams(pug: pug, event: e, iteration: iteration)
      status(pug: pug, event: e, message_obj: message_obj) if message_obj
    end
  end

  bot.command :shuffle do |event|
    setup_pug(event) do |e, pug|
      if !pug.full?
        return send_embedded_message(
          description: "Not enough players, reduce !teamsize",
          channel: event.channel
        )
      end

      message_obj = choose_teams(pug: pug, event: e)
      status(pug: pug, event: e, message_obj: message_obj) if message_obj
    end
  end

  bot.command :status do |event, *args|
    setup_pug(event) do |e, pug|
      if !pug.active?
        return send_embedded_message(
          description: "No PUG has been started. `!join` to create",
          channel: e.channel
        )
      end

      status(pug: pug, event: e)
    end
  end

  bot.command :teamsize do |event, *args|
    setup_pug(event) do |e, pug|
      unless args.any?
        return send_embedded_message(
          description: [
            "Each team has #{pug.teamsize} players",
            "#{pug.player_slots} joined"
          ].join(MSG_SNIPPET_DELIMITER),
          channel: e.channel
        )
      end

      new_teamsize = args[0].to_i

      if new_teamsize < 1
        return send_embedded_message(
          description: "Team size should be 1 or more",
          channel: e.channel
        )
      end

      pug.teamsize = new_teamsize

      send_embedded_message(
        description: [
          "Each team has #{pug.teamsize} players",
          "#{pug.player_slots} joined"
        ].join(MSG_SNIPPET_DELIMITER),
        channel: e.channel
      )

      start_pug(pug, e) if pug.full?
    end
  end

  bot.command :leave do |event, *args|
    setup_pug(event) do |e, pug|
      unless pug.active?
        return send_embedded_message(
          description: no_active_pug_message,
          channel: e.channel
        )
      end

      unless pug.joined?(e.user_id)
        return send_embedded_message(
          description: "You're not in the PUG",
          channel: e.channel
        )
      end

      pug.leave(e.user_id)

      snippets = [
        "#{e.display_name} leaves the PUG",
        "#{pug.player_slots} remain"
      ]

      message = "#{pug.slots_left} more #{pug.notify_roles}" if pug.slots_left == 1

      send_embedded_message(
        message: message,
        description: snippets.join(MSG_SNIPPET_DELIMITER),
        channel: e.channel
      )

      end_pug(pug, e.channel) if pug.empty?
    end
  end

  bot.command :kick do |event, *args|
    setup_pug(event) do |e, pug|
      unless args.any?
        return send_embedded_message(
          description: "Kick who? e.g. `!kick @#{e.display_name}`",
          channel: e.channel
        )
      end

      unless pug.active?
        return send_embedded_message(
          description: no_active_pug_message,
          channel: e.channel
        )
      end

      errors = []
      kickees = []

      args.each do |mention|
        if !mention.match(VALID_MENTION)
          errors << "#{mention} isn't a valid mention"
          next
        end

        user_id = mention_to_user_id(mention)
        display_name = e.display_name_for(user_id) || mention

        unless pug.joined?(user_id)
          errors << "#{display_name} isn't in the PUG"
          next
        end

        pug.leave(user_id)

        kickees << display_name
      end

      message = ""
      description = []

      if pug.slots_left == 1
        message = "#{pug.slots_left} more #{pug.notify_roles}"
      end

      if kickees.any?
        description << [
          kickees.to_sentence,
          kickees.count == 1 ? "is" : "are",
          "kicked from the PUG"
        ].join(" ")
      end

      description << [
        [pug.total_player_count, pug.maxplayers].join("/"),
        "remain"
      ].join(" ")

      description = [errors, description.join(MSG_SNIPPET_DELIMITER)].join("\n")

      send_embedded_message(
        message: message,
        description: description,
        channel: e.channel
      )

      end_pug(pug, e.channel) if pug.empty?
    end
  end

  bot.command :team do |event, *args|
    setup_pug(event) do |e, pug|
      if args.empty?
        return send_embedded_message(
          description: "Which team? E.G. `!team 1`",
          channel: e.channel
        )
      end

      if ["1", "2"].none?(args.first)
        return send_embedded_message(
          description: "Choose `!team 1`, `!team 2`, or `!unteam` to leave team",
          channel: e.channel
        )
      end

      team_no = args.first.to_i
      pug_already_full = pug.full?

      if args.count == 1
        user_id = e.user_id

        if pug.team(team_no).include?(user_id)
          return send_embedded_message(
            description: "You're already in #{TEAM_NAMES[team_no]}",
            channel: e.channel
          )
        end

        pug.join_team(team_no: team_no, player_id: user_id)

        send_embedded_message(
          description: [
            "#{e.display_name} joins #{TEAM_NAMES[team_no]}",
            "#{pug.team_player_count(team_no)}/#{pug.teamsize}"
          ].join(MSG_SNIPPET_DELIMITER),
          channel: e.channel
        )
      else
        errors = []
        teamers = []

        args[1..-1].each do |mention|
          if !mention.match(VALID_MENTION)
            errors << "#{mention} isn't a valid mention"
            next
          end

          user_id = mention_to_user_id(mention)
          display_name = e.display_name_for(user_id) || mention
          pug.join_team(team_no: team_no, player_id: user_id)

          teamers << display_name
        end

        description = errors << [
          [
            teamers.to_sentence,
            teamers.count == 1 ? "joins" : "join",
            TEAM_NAMES[team_no]
          ].join(" "),
          [
            pug.team_player_count(team_no),
            pug.teamsize
          ].join("/")
        ].join(MSG_SNIPPET_DELIMITER)

        send_embedded_message(
          description: description.join("\n"),
          channel: e.channel
        )
      end

      start_pug(pug, e) if !pug_already_full && pug.has_exactly_maxplayers?
    end
  end

  bot.command :unteam do |event, *args|
    setup_pug(event) do |e, pug|
      user_id = e.user_id

      if !pug.active?
        return send_embedded_message(
          description: 'No PUG has been started. `!join` to create',
          channel: e.channel
        )
      end

      if args.empty?
        if !pug.joined?(user_id)
          return send_embedded_message(
            description: "You aren't in this PUG",
            channel: e.channel
          )
        end

        if !pug.teamed_players.include?(user_id)
          return send_embedded_message(
            description: "You aren't in a team",
            channel: e.channel
          )
        end

        pug.unteam(user_id)

        send_embedded_message(
          description: "#{e.display_name} leaves team",
          channel: e.channel
        )
      else
        errors = []
        unteamers = []

        args.each do |mention|
          if !mention.match(VALID_MENTION)
            errors << "#{mention} isn't a valid mention"
            next
          end

          user_id = mention_to_user_id(mention)
          display_name = e.display_name_for(user_id) || mention

          if !pug.joined?(user_id)
            errors << "#{display_name} isn't in this PUG"
            next
          end

          pug.unteam(user_id)

          unteamers << display_name
        end

        description = errors << [
          unteamers.to_sentence,
          unteamers.count == 1 ? "goes" : "go",
          "into the queue"
        ].join(" ")

        send_embedded_message(
          description: description.join("\n"),
          channel: e.channel
        )
      end
    end
  end

  bot.command :win do |event, *args|
    setup_pug(event) do |e, pug|
      unless args.any?
        return send_embedded_message(
          description: "Specify winning team; e.g. `!win 1`",
          channel: e.channel
        )
      end

      unless pug.active?
        return send_embedded_message(
          description: no_active_pug_message,
          channel: e.channel
        )
      end

      if !pug.full?
        return send_embedded_message(
          description: "Can't report unless PUG is full",
          channel: event.channel
        )
      end

      if !pug.equal_number_of_players_on_each_team?
        return send_embedded_message(
          description: "Can't report unless teams have same number of players",
          channel: event.channel
        )
      end

      unless ["1", "2"].any?(args.first)
        return send_embedded_message(
          description: "Invalid team number",
          channel: e.channel
        )
      end

      if pug.last_result_time && pug.last_result_time > ten_minutes_ago
        return send_embedded_message(
          description: "A match was reported less than 10 minutes ago",
          channel: event.channel
        )
      end

      winning_team_no = args.first.to_i

      if pug.teams.count < 2
        return send_embedded_message(
          description: "There must be at least two teams with players to submit a result",
          channel: e.channel
        )
      end

      team_results = pug.teams.inject({}) do |teams, (name, player_ids)|
        players = player_ids.inject({}) do |memo, id|
          memo.merge({ id => e.display_name_for(id) })
        end

        result = winning_team_no.to_i == name.to_i ? 1 : -1
        teams.merge({ name => { players: players, result: result } })
      end

      id = report(
        pug,
        {
          match: {
            map: pug.game_map,
            teams: team_results,
            discord_channel: {
              channel_id: e.channel_id,
              name: "#{e.channel.server.name} ##{e.channel.name}"
            }
          }
        }.to_json
      ).body

      send_embedded_message(
        description: "#{TEAM_NAMES[winning_team_no]} wins game ##{id}. `!choose` again. [Results](#{discord_channel_leaderboard_url(e.channel.id)})",
        channel: e.channel
      )
    end
  end

  bot.command :draw do |event, *args|
    setup_pug(event) do |e, pug|
      unless pug.active?
        return send_embedded_message(
          description: no_active_pug_message,
          channel: e.channel
        )
      end

      if !pug.full?
        return send_embedded_message(
          description: "Can't report unless PUG is full",
          channel: event.channel
        )
      end

      if !pug.equal_number_of_players_on_each_team?
        return send_embedded_message(
          description: "Can't report unless teams have same number of players",
          channel: event.channel
        )
      end

      if pug.teams.count < 2
        return send_embedded_message(
          description: "There must be at least two teams with players to submit a result",
          channel: e.channel
        )
      end

      if pug.last_result_time && pug.last_result_time > ten_minutes_ago
        time_ago = Time.now.to_i - pug.last_result_time

        return send_embedded_message(
          description: "A match was reported less than 10 minutes ago",
          channel: event.channel
        )
      end

      team_results = pug.teams.inject({}) do |teams, (name, player_ids)|
        players = player_ids.inject({}) do |memo, id|
          memo.merge({ id => e.display_name_for(id) })
        end

      teams.merge({ name => { players: players, result: 0 } })
      end

      id = report(
        pug,
        {
          match: {
            map: pug.game_map,
            teams: team_results,
            discord_channel: {
              channel_id: e.channel_id,
              name: "#{e.channel.server.name} ##{e.channel.name}"
            }
          }
        }.to_json
      ).body

      send_embedded_message(
        description: "Match ##{id} drawn. `!choose` again. [Results](#{discord_channel_leaderboard_url(e.channel.id)})",
        channel: e.channel
      )
    end
  end

  bot.command :end do |event, *args|
    setup_pug(event) do |e, pug|
      unless pug.active?
        return send_embedded_message(
          description: no_active_pug_message,
          channel: e.channel
        )
      end

      end_pug(pug, e.channel)
    end
  end

  bot.command :addmap do |event, *args|
    setup_pug(event) do |e, pug|
      maps = args

      unless maps.any?
        return send_embedded_message(
          description: "What map? e.g. `!addmap 2fort5r`",
          channel: e.channel
        )
      end

      pug.add_maps(maps)

      send_embedded_message(
        description: "#{maps.join(', ')} added to maps",
        channel: e.channel)
    end
  end

  bot.command :removemap do |event, *args|
    setup_pug(event) do |e, pug|
      maps = args

      unless maps.any?
        return send_embedded_message(
          description: "What map? e.g. `!removemap 2fort5r`",
          channel: e.channel
        )
      end

      pug.remove_maps(maps)

      send_embedded_message(
        description: "#{maps.join(', ')} removed from maps",
        channel: e.channel
      )
    end
  end

  bot.command :maps do |event, *args|
    setup_pug(event) do |e, pug|
      maps = pug.maps
      unless maps.any?
        return send_embedded_message(
          description: 'No maps have been added. `!addmap`',
          channel: e.channel
        )
      end

      send_embedded_message(
        description: maps.join(', '),
        channel: e.channel
      )
    end
  end

  bot.command :map do |event, *args|
    setup_pug(event) do |e, pug|
      maps = pug.maps

      unless maps.any?
        return send_embedded_message(
          description: 'No maps have been added. `!addmap`',
          channel: e.channel
        )
      end

      unless pug.active?
        return send_embedded_message(
          description: no_active_pug_message,
          channel: e.channel
        )
      end

      if args.empty?
        unless pug.game_map
          return send_embedded_message(
            description: 'No map has been set for the current PUG',
            channel: e.channel
          )
        end

        send_embedded_message(
          description: "Current map is #{pug.game_map}",
          channel: e.channel
        )
      else
        game_map = args.first

        unless maps.include?(game_map)
          return send_embedded_message(
            description: "#{game_map} isn't in the map list. `!addmap` to add it.",
            channel: e.channel
          )
        end

        pug.game_map = game_map

        send_embedded_message(
          description: "Map set to #{game_map}",
          channel: e.channel
        )
      end
    end
  end

  bot.command :notify do |event, *args|
    setup_pug(event) do |e, pug|
      roles = args.join(' ')
      pug.notify_roles = roles

      msg = if roles.empty?
              'Notification removed'
            else
              "Notification role set to #{roles}"
            end

      send_embedded_message(
        description: msg,
        channel: e.channel
      )
    end
  end

  bot.run
end