28
28
29
29
public class DynamicVoiceListener extends VoiceReceiverAdapter {
30
30
31
- private final Map <String , Predicate <String >> patterns = new HashMap <>();
31
+ private final Map <String , Predicate <String >> channelPredicates = new HashMap <>();
32
32
private static final Pattern channelTopicPattern = Pattern .compile ("(\\ s+\\ d+)$" );
33
33
private static final Map <String , Queue <GuildVoiceUpdateEvent >> eventQueues = new HashMap <>();
34
- private static final Map <String , AtomicBoolean > isEventProcessing = new HashMap <>();
34
+ private static final Map <String , AtomicBoolean > activeQueuesMap = new HashMap <>();
35
35
36
36
public DynamicVoiceListener (Config config ) {
37
37
config .getDynamicVoiceChannelPatterns ().forEach (pattern -> {
38
- patterns .put (pattern , Pattern .compile (pattern ).asMatchPredicate ());
39
- isEventProcessing .put (pattern , new AtomicBoolean (false ));
38
+ channelPredicates .put (pattern , Pattern .compile (pattern ).asMatchPredicate ());
39
+ activeQueuesMap .put (pattern , new AtomicBoolean (false ));
40
40
eventQueues .put (pattern , new LinkedList <>());
41
41
});
42
42
}
@@ -64,34 +64,34 @@ private void insertEventToQueue(GuildVoiceUpdateEvent event, String channelTopic
64
64
65
65
eventQueue .add (event );
66
66
67
- if (isEventProcessing .get (channelTopic ).get ()) {
67
+ if (activeQueuesMap .get (channelTopic ).get ()) {
68
68
return ;
69
69
}
70
70
71
71
processEventFromQueue (channelTopic );
72
72
}
73
73
74
74
private void processEventFromQueue (String channelTopic ) {
75
- AtomicBoolean processing = isEventProcessing .get (channelTopic );
75
+ AtomicBoolean activeQueueFlag = activeQueuesMap .get (channelTopic );
76
76
GuildVoiceUpdateEvent event = eventQueues .get (channelTopic ).poll ();
77
77
78
78
if (event == null ) {
79
- processing .set (false );
79
+ activeQueueFlag .set (false );
80
80
return ;
81
81
}
82
82
83
- processing .set (true );
83
+ activeQueueFlag .set (true );
84
84
85
85
handleTopicUpdate (event , channelTopic );
86
86
}
87
87
88
88
private void handleTopicUpdate (GuildVoiceUpdateEvent event , String channelTopic ) {
89
- AtomicBoolean processing = isEventProcessing .get (channelTopic );
89
+ AtomicBoolean activeQueueFlag = activeQueuesMap .get (channelTopic );
90
90
Guild guild = event .getGuild ();
91
- List <CompletableFuture <?>> tasks = new ArrayList <>();
91
+ List <CompletableFuture <?>> restActionTasks = new ArrayList <>();
92
92
93
- if (patterns .get (channelTopic ) == null ) {
94
- processing .set (false );
93
+ if (channelPredicates .get (channelTopic ) == null ) {
94
+ activeQueueFlag .set (false );
95
95
return ;
96
96
}
97
97
@@ -100,38 +100,43 @@ private void handleTopicUpdate(GuildVoiceUpdateEvent event, String channelTopic)
100
100
if (emptyChannelsCount == 0 ) {
101
101
long channelCount = getChannelCountFromTopic (guild , channelTopic );
102
102
103
- tasks .add (createVoiceChannelFromTopic (guild , channelTopic , channelCount ));
103
+ restActionTasks
104
+ .add (makeCreateVoiceChannelFromTopicFuture (guild , channelTopic , channelCount ));
104
105
} else if (emptyChannelsCount != 1 ) {
105
- tasks .addAll (removeDuplicateEmptyChannels (guild , channelTopic ));
106
- tasks .addAll (renameTopicChannels (guild , channelTopic ));
106
+ restActionTasks .addAll (makeRemoveDuplicateEmptyChannelsFutures (guild , channelTopic ));
107
+ restActionTasks .addAll (makeRenameTopicChannelsFutures (guild , channelTopic ));
107
108
}
108
109
109
- if (!tasks .isEmpty ()) {
110
- CompletableFuture .allOf (tasks .toArray (CompletableFuture []::new )).thenCompose (v -> {
111
- List <CompletableFuture <?>> renameTasks = renameTopicChannels (guild , channelTopic );
112
- return CompletableFuture .allOf (renameTasks .toArray (CompletableFuture []::new ));
113
- }).handle ((result , exception ) -> {
114
- processEventFromQueue (channelTopic );
115
- processing .set (false );
116
- return null ;
117
- });
110
+ if (!restActionTasks .isEmpty ()) {
111
+ CompletableFuture .allOf (restActionTasks .toArray (CompletableFuture []::new ))
112
+ .thenCompose (v -> {
113
+ List <CompletableFuture <?>> renameTasks =
114
+ makeRenameTopicChannelsFutures (guild , channelTopic );
115
+ return CompletableFuture .allOf (renameTasks .toArray (CompletableFuture []::new ));
116
+ })
117
+ .handle ((result , exception ) -> {
118
+ processEventFromQueue (channelTopic );
119
+ activeQueueFlag .set (false );
120
+ return null ;
121
+ });
118
122
return ;
119
123
}
120
124
121
125
processEventFromQueue (channelTopic );
122
- processing .set (false );
126
+ activeQueueFlag .set (false );
123
127
}
124
128
125
- private static CompletableFuture <? extends StandardGuildChannel > createVoiceChannelFromTopic (
129
+ private static CompletableFuture <? extends StandardGuildChannel > makeCreateVoiceChannelFromTopicFuture (
126
130
Guild guild , String channelTopic , long topicChannelsCount ) {
127
- Optional <VoiceChannel > voiceChannelOptional = getOriginalTopicChannel (guild , channelTopic );
131
+ Optional <VoiceChannel > originalTopicChannelOptional =
132
+ getOriginalTopicChannel (guild , channelTopic );
128
133
129
- if (voiceChannelOptional .isPresent ()) {
130
- VoiceChannel originalChannel = voiceChannelOptional .orElseThrow ();
134
+ if (originalTopicChannelOptional .isPresent ()) {
135
+ VoiceChannel originalTopicChannel = originalTopicChannelOptional .orElseThrow ();
131
136
132
- return originalChannel .createCopy ()
137
+ return originalTopicChannel .createCopy ()
133
138
.setName (getNumberedChannelTopic (channelTopic , topicChannelsCount + 1 ))
134
- .setPosition (originalChannel .getPositionRaw ())
139
+ .setPosition (originalTopicChannel .getPositionRaw ())
135
140
.submit ();
136
141
}
137
142
@@ -146,38 +151,39 @@ private static Optional<VoiceChannel> getOriginalTopicChannel(Guild guild,
146
151
.findFirst ();
147
152
}
148
153
149
- private List <CompletableFuture <Void >> removeDuplicateEmptyChannels (Guild guild ,
154
+ private List <CompletableFuture <Void >> makeRemoveDuplicateEmptyChannelsFutures (Guild guild ,
150
155
String channelTopic ) {
151
156
List <VoiceChannel > channelsToRemove = getVoiceChannelsFromTopic (guild , channelTopic )
152
157
.filter (channel -> channel .getMembers ().isEmpty ())
153
158
.toList ();
154
- final List <CompletableFuture <Void >> tasks = new ArrayList <>();
159
+ final List <CompletableFuture <Void >> restActionTasks = new ArrayList <>();
155
160
156
161
channelsToRemove .subList (1 , channelsToRemove .size ())
157
- .forEach (channel -> tasks .add (channel .delete ().submit ()));
162
+ .forEach (channel -> restActionTasks .add (channel .delete ().submit ()));
158
163
159
- return tasks ;
164
+ return restActionTasks ;
160
165
}
161
166
162
- private List <CompletableFuture <?>> renameTopicChannels (Guild guild , String channelTopic ) {
163
- List <VoiceChannel > channels = getVoiceChannelsFromTopic (guild , channelTopic ).toList ();
164
- List <CompletableFuture <?>> tasks = new ArrayList <>();
167
+ private List <CompletableFuture <?>> makeRenameTopicChannelsFutures (Guild guild ,
168
+ String channelTopic ) {
169
+ List <VoiceChannel > topicChannels = getVoiceChannelsFromTopic (guild , channelTopic ).toList ();
170
+ List <CompletableFuture <?>> restActionTasks = new ArrayList <>();
165
171
166
- IntStream .range (0 , channels .size ())
172
+ IntStream .range (0 , topicChannels .size ())
167
173
.asLongStream ()
168
- .mapToObj (number -> Pair .of (number + 1 , channels .get ((int ) number )))
174
+ .mapToObj (channelId -> Pair .of (channelId + 1 , topicChannels .get ((int ) channelId )))
169
175
.filter (pair -> pair .getLeft () != 1 )
170
176
.forEach (pair -> {
171
- long number = pair .getLeft ();
177
+ long channelId = pair .getLeft ();
172
178
VoiceChannel voiceChannel = pair .getRight ();
173
179
String voiceChannelNameTopic = getChannelTopic (voiceChannel .getName ());
174
180
175
- tasks .add (voiceChannel .getManager ()
176
- .setName (getNumberedChannelTopic (voiceChannelNameTopic , number ))
181
+ restActionTasks .add (voiceChannel .getManager ()
182
+ .setName (getNumberedChannelTopic (voiceChannelNameTopic , channelId ))
177
183
.submit ());
178
184
});
179
185
180
- return tasks ;
186
+ return restActionTasks ;
181
187
}
182
188
183
189
private long getChannelCountFromTopic (Guild guild , String channelTopic ) {
@@ -187,7 +193,8 @@ private long getChannelCountFromTopic(Guild guild, String channelTopic) {
187
193
private Stream <VoiceChannel > getVoiceChannelsFromTopic (Guild guild , String channelTopic ) {
188
194
return guild .getVoiceChannels ()
189
195
.stream ()
190
- .filter (channel -> patterns .get (channelTopic ).test (getChannelTopic (channel .getName ())));
196
+ .filter (channel -> channelPredicates .get (channelTopic )
197
+ .test (getChannelTopic (channel .getName ())));
191
198
}
192
199
193
200
private long getEmptyChannelsCountFromTopic (Guild guild , String channelTopic ) {
@@ -198,16 +205,16 @@ private long getEmptyChannelsCountFromTopic(Guild guild, String channelTopic) {
198
205
}
199
206
200
207
private static String getChannelTopic (String channelName ) {
201
- Matcher matcher = channelTopicPattern .matcher (channelName );
208
+ Matcher channelTopicPatternMatcher = channelTopicPattern .matcher (channelName );
202
209
203
- if (matcher .find ()) {
204
- return matcher .replaceAll ("" );
210
+ if (channelTopicPatternMatcher .find ()) {
211
+ return channelTopicPatternMatcher .replaceAll ("" );
205
212
}
206
213
207
214
return channelName ;
208
215
}
209
216
210
- private static String getNumberedChannelTopic (String channelTopic , long id ) {
211
- return String .format ("%s %d" , channelTopic , id );
217
+ private static String getNumberedChannelTopic (String channelTopic , long channelId ) {
218
+ return String .format ("%s %d" , channelTopic , channelId );
212
219
}
213
220
}
0 commit comments