qwtf_discord_bot 5.1.3 → 5.1.9

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: ea5f5046690139982d6f1d8324c4b4d312d6d21f5c5d6f3734211038454db8b7
4
- data.tar.gz: 9ce6466357c99b25415f55b0eed27edd3acd8bc4e741045bfcb3cc85cbb34509
3
+ metadata.gz: 874aab5dba875e61f9dfd5500d91a25149a7b08a8a91854402080d4775cc1b24
4
+ data.tar.gz: e68aa880053d9bffbfb563fa6bcdaa060b89d91051e6955615f89543a30e1530
5
5
  SHA512:
6
- metadata.gz: 19428a1ac12bb6da56fbebaf8e47420f49514aa579b208c8063832d41d5a8948a3dc112358a8e4c8f5ab2fa4693c6521cc89ecddf4087a02e60bcc8862c47fc9
7
- data.tar.gz: 22d259fa928093d9eec63eb1896b1e9ec7edf32e4ef8fe5e198d3b4a882c670641e9690755ed15fea49fcee9328b2999406d8a4cc9f7d8fb719c7c34ced5bfcd
6
+ metadata.gz: aae1ed1e4fa7e631a4903fb7fdfb933b51bf127ee3de0d3b8497f39d3d6d36c16dcd23db18664a724375bb454ac60bca7467ce0f34bfbcfe7c55b278ece01660
7
+ data.tar.gz: 7426320e57d72affc5170ed20a7cd4a0d0ff1711c2103cfe88c85d8c646e5900725860d4c8644653cc53ce610a8f0b18cd88e1db8550d96c80df05e271532f72
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.3
1
+ 5.1.9
@@ -11,8 +11,8 @@ class EventDecorator
11
11
  @event.channel.id
12
12
  end
13
13
 
14
- def username
15
- user.username
14
+ def display_name
15
+ user.display_name
16
16
  end
17
17
 
18
18
  def user_id
@@ -24,11 +24,17 @@ 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
30
+ end
31
+
32
+ def display_names_for(user_ids)
33
+ find_users(user_ids).map(&:display_name)
28
34
  end
29
35
 
30
- def usernames_for(user_ids)
31
- find_users(user_ids).map(&:username)
36
+ def display_name_for(user_id)
37
+ find_user(user_id).display_name
32
38
  end
33
39
 
34
40
  private
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,121 +15,129 @@ 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)
24
- else
25
- pug.join(e.user_id)
26
-
27
- message = if pug.joined_player_count == 1
28
- [
29
- "#{e.username} creates a PUG",
30
- pug.player_slots,
31
- pug.notify_roles
32
- ].join(' | ')
33
- elsif pug.slots_left.between?(1,3)
34
- [
35
- "#{e.username} joins the PUG",
36
- pug.player_slots,
37
- "#{pug.slots_left} more",
38
- pug.notify_roles
39
- ].join(' | ')
40
- else
41
- [
42
- "#{e.username} 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
- )
18
+ setup_pug(event) do |e, pug|
19
+ return message("You've already joined", e.channel) if pug.joined?(e.user_id)
54
20
 
55
- send_and_log_message(message, e.channel)
56
- end
57
- end
21
+ pug.join(e.user_id)
22
+
23
+ message = if pug.joined_player_count == 1
24
+ [
25
+ "#{e.display_name} creates a PUG",
26
+ pug.player_slots,
27
+ pug.notify_roles
28
+ ].join(' | ')
29
+ elsif pug.slots_left.between?(1, 3)
30
+ [
31
+ "#{e.display_name} joins the PUG",
32
+ pug.player_slots,
33
+ "#{pug.slots_left} more",
34
+ pug.notify_roles
35
+ ].join(' | ')
36
+ else
37
+ [
38
+ "#{e.display_name} joins the PUG",
39
+ pug.player_slots
40
+ ].join(' | ')
41
+ end
42
+
43
+ message(message, e.channel)
44
+
45
+ start_pug(pug, e) if pug.full?
58
46
  end
59
47
  end
60
48
 
61
49
  bot.command :status do |event, *args|
62
- set_pug(event) do |e, pug|
50
+ setup_pug(event) do |e, pug|
63
51
  message = if pug.active?
64
52
  [
65
- "#{e.usernames_for(pug.joined_players).join(', ')} joined",
53
+ "#{e.display_names_for(pug.joined_players).join(', ')} joined",
66
54
  pug.player_slots
67
55
  ].join(' | ')
68
56
  else
69
57
  'No PUG has been started. `!join` to create'
70
58
  end
71
59
 
72
- send_and_log_message(message, e.channel)
60
+ message(message, e.channel)
73
61
  end
74
62
  end
75
63
 
76
- bot.command :maxplayers do |event, *args|
77
- set_pug(event) do |e, pug|
78
- new_maxplayers = args[0]
64
+ bot.command :teamsize do |event, *args|
65
+ setup_pug(event) do |e, pug|
66
+ new_teamsize = args[0]
79
67
 
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
86
-
87
- send_and_log_message(message, e.channel)
88
-
89
- if pug.full?
90
- message = start_pug(
91
- pug.player_slots,
92
- e.mentions_for(pug.joined_players)
68
+ if new_teamsize
69
+ pug.teamsize = new_teamsize
70
+ message(
71
+ "Team size set to #{pug.teamsize} | #{pug.player_slots} joined",
72
+ e.channel
73
+ )
74
+ start_pug(pug, e) if pug.full?
75
+ else
76
+ message(
77
+ "Current team size is #{pug.teamsize} | #{pug.player_slots} joined",
78
+ e.channel
93
79
  )
94
-
95
- send_and_log_message(message, e.channel)
96
80
  end
97
81
  end
98
82
  end
99
83
 
100
84
  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.username} leaves the PUG | #{pug.player_slots} remain"
111
- send_and_log_message(message, e.channel)
85
+ setup_pug(event) do |e, pug|
86
+ return message(no_active_pug_message, e.channel) unless pug.active?
87
+ return message("You're not in the PUG", e.channel) unless pug.joined?(e.user_id)
88
+
89
+ pug.leave(e.user_id)
90
+
91
+ message(
92
+ "#{e.display_name} leaves the PUG | #{pug.player_slots} remain",
93
+ e.channel
94
+ )
95
+
96
+ message(end_pug_message, e.channel) unless pug.active?
97
+ end
98
+ end
99
+
100
+ bot.command :kick do |event, *args|
101
+ setup_pug(event) do |e, pug|
102
+ return message(no_active_pug_message, e.channel) unless pug.active?
103
+
104
+ args.each do |mention|
105
+ user_id = mention[3..-2].to_i
106
+ display_name = e.display_name_for(user_id)
112
107
 
113
- if pug.empty?
114
- message = end_pug(pug)
115
- send_and_log_message(message, e.channel)
108
+ unless pug.joined?(user_id)
109
+ message(
110
+ "#{display_name} isn't in the PUG",
111
+ e.channel
112
+ )
113
+
114
+ next
116
115
  end
116
+
117
+ pug.leave(user_id)
118
+
119
+ message(
120
+ "#{display_name} is kicked from the PUG | #{pug.player_slots} remain",
121
+ e.channel
122
+ )
123
+
124
+ break message(end_pug_message, e.channel) unless pug.active?
117
125
  end
118
126
  end
119
127
  end
120
128
 
121
129
  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
130
+ setup_pug(event) do |e, pug|
131
+ return message(no_active_pug_message, e.channel) unless pug.active?
132
+
133
+ pug.end_pug
128
134
 
129
- send_and_log_message(message, e.channel)
135
+ message(end_pug_message, e.channel)
130
136
  end
131
137
  end
132
138
 
133
139
  bot.command :notify do |event, *args|
134
- set_pug(event) do |e, pug|
140
+ setup_pug(event) do |e, pug|
135
141
  roles = args.join(' ')
136
142
  pug.notify_roles = roles
137
143
 
@@ -141,7 +147,7 @@ class QwtfDiscordBotPug # :nodoc:
141
147
  "Notification role set to #{roles}"
142
148
  end
143
149
 
144
- send_and_log_message(message, e.channel)
150
+ message(message, e.channel)
145
151
  end
146
152
  end
147
153
 
@@ -150,26 +156,37 @@ class QwtfDiscordBotPug # :nodoc:
150
156
 
151
157
  private
152
158
 
153
- def set_pug(event)
159
+ def setup_pug(event)
154
160
  e = EventDecorator.new(event)
155
161
  pug = Pug.for(e.channel_id)
156
162
  yield(e, pug)
163
+ nil # stop discordrb printing return value
157
164
  end
158
165
 
159
- def start_pug(player_slots, mentions)
160
- [
161
- 'Time to play!',
162
- player_slots,
163
- mentions.join(' ')
164
- ].join(' | ')
166
+ def start_pug(pug, event)
167
+ message(
168
+ [
169
+ 'Time to play!',
170
+ ['Team 1:', event.mentions_for(pug.team(1)).join(' ')].join(' '),
171
+ ['Team 2:', event.mentions_for(pug.team(2)).join(' ')].join(' ')
172
+ ].join("\n"),
173
+ event.channel
174
+ )
175
+ end
176
+
177
+ def start_pug_message(player_slots:, mentions:)
178
+ ['Time to play!', player_slots, mentions.join(' ')].join(' | ')
165
179
  end
166
180
 
167
- def end_pug(pug)
168
- pug.end_pug
181
+ def end_pug_message
169
182
  'PUG ended'
170
183
  end
171
184
 
172
- def send_and_log_message(message, channel)
185
+ def no_active_pug_message
186
+ "There's no active PUG"
187
+ end
188
+
189
+ def message(message, channel)
173
190
  channel.send_message(message) && puts(message)
174
191
  end
175
192
  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.3
4
+ version: 5.1.9
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-24 00:00:00.000000000 Z
11
+ date: 2020-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb