2
2
3
3
import net .dv8tion .jda .api .JDA ;
4
4
import net .dv8tion .jda .api .entities .channel .Channel ;
5
+ import net .dv8tion .jda .api .entities .channel .unions .AudioChannelUnion ;
6
+ import net .dv8tion .jda .api .events .guild .voice .GuildVoiceDeafenEvent ;
7
+ import net .dv8tion .jda .api .events .guild .voice .GuildVoiceMuteEvent ;
8
+ import net .dv8tion .jda .api .events .guild .voice .GuildVoiceStreamEvent ;
9
+ import net .dv8tion .jda .api .events .guild .voice .GuildVoiceUpdateEvent ;
10
+ import net .dv8tion .jda .api .events .guild .voice .GuildVoiceVideoEvent ;
5
11
import net .dv8tion .jda .api .events .interaction .ModalInteractionEvent ;
6
12
import net .dv8tion .jda .api .events .interaction .command .CommandAutoCompleteInteractionEvent ;
7
13
import net .dv8tion .jda .api .events .interaction .command .MessageContextInteractionEvent ;
16
22
import net .dv8tion .jda .api .hooks .ListenerAdapter ;
17
23
import net .dv8tion .jda .api .interactions .callbacks .IReplyCallback ;
18
24
import net .dv8tion .jda .api .interactions .components .ComponentInteraction ;
25
+ import org .jetbrains .annotations .NotNull ;
26
+ import org .jetbrains .annotations .Nullable ;
19
27
import org .jetbrains .annotations .Unmodifiable ;
20
28
import org .slf4j .Logger ;
21
29
import org .slf4j .LoggerFactory ;
32
40
import org .togetherjava .tjbot .features .UserContextCommand ;
33
41
import org .togetherjava .tjbot .features .UserInteractionType ;
34
42
import org .togetherjava .tjbot .features .UserInteractor ;
43
+ import org .togetherjava .tjbot .features .VoiceReceiver ;
35
44
import org .togetherjava .tjbot .features .componentids .ComponentId ;
36
45
import org .togetherjava .tjbot .features .componentids .ComponentIdParser ;
37
46
import org .togetherjava .tjbot .features .componentids .ComponentIdStore ;
@@ -75,6 +84,7 @@ public final class BotCore extends ListenerAdapter implements CommandProvider {
75
84
private final ComponentIdParser componentIdParser ;
76
85
private final ComponentIdStore componentIdStore ;
77
86
private final Map <Pattern , MessageReceiver > channelNameToMessageReceiver = new HashMap <>();
87
+ private final Map <Pattern , VoiceReceiver > channelNameToVoiceReceiver = new HashMap <>();
78
88
79
89
/**
80
90
* Creates a new command system which uses the given database to allow commands to persist data.
@@ -96,6 +106,13 @@ public BotCore(JDA jda, Database database, Config config) {
96
106
.forEach (messageReceiver -> channelNameToMessageReceiver
97
107
.put (messageReceiver .getChannelNamePattern (), messageReceiver ));
98
108
109
+ // Voice receivers
110
+ features .stream ()
111
+ .filter (VoiceReceiver .class ::isInstance )
112
+ .map (VoiceReceiver .class ::cast )
113
+ .forEach (voiceReceiver -> channelNameToVoiceReceiver
114
+ .put (voiceReceiver .getChannelNamePattern (), voiceReceiver ));
115
+
99
116
// Event receivers
100
117
features .stream ()
101
118
.filter (EventReceiver .class ::isInstance )
@@ -238,6 +255,76 @@ public void onMessageDelete(final MessageDeleteEvent event) {
238
255
}
239
256
}
240
257
258
+ /**
259
+ * @param joinChannel the join channel
260
+ * @param leftChannel the leave channel
261
+ * @return the join channel if not null, otherwise the leave channel, otherwise an empty
262
+ * optional
263
+ */
264
+ private Optional <Channel > calculateSubscribeTarget (@ Nullable AudioChannelUnion joinChannel ,
265
+ @ Nullable AudioChannelUnion leftChannel ) {
266
+ if (joinChannel != null ) {
267
+ return Optional .of (joinChannel );
268
+ }
269
+
270
+ return Optional .ofNullable (leftChannel );
271
+ }
272
+
273
+ @ Override
274
+ public void onGuildVoiceUpdate (@ NotNull GuildVoiceUpdateEvent event ) {
275
+ calculateSubscribeTarget (event .getChannelJoined (), event .getChannelLeft ())
276
+ .ifPresent (channel -> getVoiceReceiversSubscribedTo (channel )
277
+ .forEach (voiceReceiver -> voiceReceiver .onVoiceUpdate (event )));
278
+ }
279
+
280
+ @ Override
281
+ public void onGuildVoiceVideo (@ NotNull GuildVoiceVideoEvent event ) {
282
+ AudioChannelUnion channel = event .getVoiceState ().getChannel ();
283
+
284
+ if (channel == null ) {
285
+ return ;
286
+ }
287
+
288
+ getVoiceReceiversSubscribedTo (channel )
289
+ .forEach (voiceReceiver -> voiceReceiver .onVideoToggle (event ));
290
+ }
291
+
292
+ @ Override
293
+ public void onGuildVoiceStream (@ NotNull GuildVoiceStreamEvent event ) {
294
+ AudioChannelUnion channel = event .getVoiceState ().getChannel ();
295
+
296
+ if (channel == null ) {
297
+ return ;
298
+ }
299
+
300
+ getVoiceReceiversSubscribedTo (channel )
301
+ .forEach (voiceReceiver -> voiceReceiver .onStreamToggle (event ));
302
+ }
303
+
304
+ @ Override
305
+ public void onGuildVoiceMute (@ NotNull GuildVoiceMuteEvent event ) {
306
+ AudioChannelUnion channel = event .getVoiceState ().getChannel ();
307
+
308
+ if (channel == null ) {
309
+ return ;
310
+ }
311
+
312
+ getVoiceReceiversSubscribedTo (channel )
313
+ .forEach (voiceReceiver -> voiceReceiver .onMuteToggle (event ));
314
+ }
315
+
316
+ @ Override
317
+ public void onGuildVoiceDeafen (@ NotNull GuildVoiceDeafenEvent event ) {
318
+ AudioChannelUnion channel = event .getVoiceState ().getChannel ();
319
+
320
+ if (channel == null ) {
321
+ return ;
322
+ }
323
+
324
+ getVoiceReceiversSubscribedTo (channel )
325
+ .forEach (voiceReceiver -> voiceReceiver .onDeafenToggle (event ));
326
+ }
327
+
241
328
private Stream <MessageReceiver > getMessageReceiversSubscribedTo (Channel channel ) {
242
329
String channelName = channel .getName ();
243
330
return channelNameToMessageReceiver .entrySet ()
@@ -248,6 +335,16 @@ private Stream<MessageReceiver> getMessageReceiversSubscribedTo(Channel channel)
248
335
.map (Map .Entry ::getValue );
249
336
}
250
337
338
+ private Stream <VoiceReceiver > getVoiceReceiversSubscribedTo (Channel channel ) {
339
+ String channelName = channel .getName ();
340
+ return channelNameToVoiceReceiver .entrySet ()
341
+ .stream ()
342
+ .filter (patternAndReceiver -> patternAndReceiver .getKey ()
343
+ .matcher (channelName )
344
+ .matches ())
345
+ .map (Map .Entry ::getValue );
346
+ }
347
+
251
348
@ Override
252
349
public void onSlashCommandInteraction (SlashCommandInteractionEvent event ) {
253
350
String name = event .getName ();
0 commit comments