This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
forked from twitch4j/twitch4j
/
ChatKtx.kt
77 lines (70 loc) · 3.15 KB
/
ChatKtx.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
package com.github.twitch4j.ktx.chat
import com.github.twitch4j.chat.TwitchChat
import com.github.twitch4j.chat.events.AbstractChannelEvent
import com.github.twitch4j.chat.events.channel.ChannelMessageEvent
import com.github.twitch4j.ktx.main.flowOn
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
/**
* Creates an events flow for the given channel.
*
* Disclaimer: The autoJoinAndLeave feature will automatically leave the twitch channel when you remove the collector of
* this flow. If this is a problem for you (e.g. you need to stay joined in the channel after removing a collector)
* you are better off setting autoJoinAndLeave to false, and join/leave the channel on your own.
*
* @param channel The channel to retrieve events from
* @param autoJoinAndLeave Whether we automatically join and leave the channel or not
* @return A flow object that encapsulates handling joining, listening to, and leaving a channel
*/
inline fun <reified T : AbstractChannelEvent> TwitchChat.channelEventsAsFlow(
channel: String,
autoJoinAndLeave: Boolean = true
): Flow<T> = channelEventsAsFlow(T::class.java, channel, autoJoinAndLeave)
/**
* Creates a chat message flow for the given channel.
*
* Disclaimer: The autoJoinAndLeave feature will automatically leave the twitch channel when you remove the collector of
* this flow. If this is a problem for you (e.g. you need to stay joined in the channel after removing a collector)
* you are better off setting autoJoinAndLeave to false, and join/leave the channel on your own.
*
* @param channel The channel to read chat messages from
* @param autoJoinAndLeave Whether we automatically join and leave the channel or not
* @return A flow object that encapsulates joining, listening to messages, and leaving a channel
*/
fun TwitchChat.channelChatAsFlow(
channel: String,
autoJoinAndLeave: Boolean = true
): Flow<ChannelMessageEvent> = channelEventsAsFlow(ChannelMessageEvent::class.java, channel, autoJoinAndLeave)
/**
* Creates an events flow for the given channel.
*
* Disclaimer: The autoJoinAndLeave feature will automatically leave the twitch channel when you remove the collector of
* this flow. If this is a problem for you (e.g. you need to stay joined in the channel after removing a collector)
* you are better off setting autoJoinAndLeave to false, and join/leave the channel on your own.
*
* @param klass The event class to receive
* @param channel The channel to retrieve events from
* @param autoJoinAndLeave Whether we automatically join and leave the channel or not
* @return A flow object that encapsulates handling joining, listening to, and leaving a channel
*/
fun <T : AbstractChannelEvent> TwitchChat.channelEventsAsFlow(
klass: Class<T>,
channel: String,
autoJoinAndLeave: Boolean
): Flow<T> = channelFlow {
if (autoJoinAndLeave) {
connect()
joinChannel(channel)
}
launch {
eventManager.flowOn(klass)
.filter { it.channel.name.equals(channel, true) }
.collect(::send)
}
awaitClose {
if (autoJoinAndLeave) {
leaveChannel(channel)
}
}
}