diff --git a/src/main/kotlin/dev/jansel/feixiao/App.kt b/src/main/kotlin/dev/jansel/feixiao/App.kt index 299c7a5..3bf0d24 100644 --- a/src/main/kotlin/dev/jansel/feixiao/App.kt +++ b/src/main/kotlin/dev/jansel/feixiao/App.kt @@ -3,17 +3,13 @@ */ package dev.jansel.feixiao -import com.github.philippheuer.events4j.reactor.ReactorEventHandler import com.github.twitch4j.TwitchClient import com.github.twitch4j.TwitchClientBuilder import com.github.twitch4j.events.ChannelGoLiveEvent import dev.jansel.feixiao.database.collections.StreamerCollection import dev.jansel.feixiao.extensions.EventHooks import dev.jansel.feixiao.extensions.StreamerCommand -import dev.jansel.feixiao.utils.database -import dev.jansel.feixiao.utils.token -import dev.jansel.feixiao.utils.twitchcid -import dev.jansel.feixiao.utils.twitchcs +import dev.jansel.feixiao.utils.* import dev.kord.core.entity.channel.GuildMessageChannel import dev.kordex.core.ExtensibleBot import dev.kordex.core.i18n.SupportedLocales @@ -22,11 +18,13 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking var twitchClient: TwitchClient? = null -val logger = KotlinLogging.logger { } +val logger = KotlinLogging.logger { } +var botRef : ExtensibleBot? = null suspend fun main() { - val bot = ExtensibleBot(token) { + botRef = ExtensibleBot(token) { database(true) + twitch(true) extensions { add(::EventHooks) add(::StreamerCommand) @@ -35,55 +33,8 @@ suspend fun main() { applicationCommandLocale(SupportedLocales.ENGLISH, SupportedLocales.GERMAN) } } - twitchClient = TwitchClientBuilder.builder() - .withEnableHelix(true) - .withClientId(twitchcid) - .withClientSecret(twitchcs) - .withDefaultEventHandler(ReactorEventHandler::class.java) - .build() - twitchClient!!.eventManager.onEvent(ChannelGoLiveEvent::class.java) { - logger.info { "${it.channel.name} went live!" } - runBlocking { - launch { - val streamer = StreamerCollection().getData(it.channel.name) - for (server in streamer!!.servers) { - val channel = bot.kordRef.getChannelOf(server.channelId) - val role = server.roleId - val livemessage = server.liveMessage - - if (role != null) { - if (livemessage != null) { - channel?.createMessage( - livemessage - .replace("{name}", it.channel.name) - .replace("{category}", it.stream.gameName) - .replace("{title}", it.stream.title) - .replace("{url}", "https://twitch.tv/${it.channel.name}") - .replace("{role}", "<@&$role>") - ) - } else { - channel?.createMessage("<@&$role> https://twitch.tv/${it.channel.name} went live streaming ${it.stream.gameName}: ${it.stream.title}") - } - } else { - if (livemessage != null) { - channel?.createMessage( - livemessage - .replace("{name}", it.channel.name) - .replace("{category}", it.stream.gameName) - .replace("{title}", it.stream.title) - .replace("{url}", "https://twitch.tv/${it.channel.name}") - ) - } else { - channel?.createMessage("https://twitch.tv/${it.channel.name} went live streaming ${it.stream.gameName}: ${it.stream.title}") - } - } - } - } - } - } - - bot.start() + botRef!!.start() } diff --git a/src/main/kotlin/dev/jansel/feixiao/utils/Twitch.kt b/src/main/kotlin/dev/jansel/feixiao/utils/Twitch.kt new file mode 100644 index 0000000..6327551 --- /dev/null +++ b/src/main/kotlin/dev/jansel/feixiao/utils/Twitch.kt @@ -0,0 +1,40 @@ +package dev.jansel.feixiao.utils + +import com.github.twitch4j.TwitchClientBuilder +import com.github.twitch4j.events.ChannelGoLiveEvent +import dev.jansel.feixiao.botRef +import dev.jansel.feixiao.database.collections.StreamerCollection +import dev.jansel.feixiao.twitchClient +import dev.kord.core.entity.channel.GuildMessageChannel +import dev.kordex.core.koin.KordExKoinComponent +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking + +class Twitch : KordExKoinComponent { + suspend fun init() { + twitchClient = TwitchClientBuilder.builder() + .withEnableHelix(true) + .withClientId(twitchcid) + .withClientSecret(twitchcs) + .build() + + twitchClient!!.eventManager.onEvent(ChannelGoLiveEvent::class.java) { + dev.jansel.feixiao.logger.info { "${it.channel.name} went live!" } + runBlocking { + launch { + val streamer = StreamerCollection().getData(it.channel.name) + val channel = botRef!!.kordRef.getChannelOf(streamer!!.servers.first().channelId) + val role = streamer.servers.first().roleId + if (role != null) { + channel?.createMessage("<@&$role> https://twitch.tv/${it.channel.name} went live streaming ${it.stream.gameName}: ${it.stream.title}") + } else { + channel?.createMessage("${it.channel.name} went live: ${it.stream.title}") + } + } + } + } + } + + + +} diff --git a/src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt b/src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt index ca0dc30..a7c9ec5 100644 --- a/src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt +++ b/src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt @@ -1,12 +1,17 @@ package dev.jansel.feixiao.utils +import com.github.twitch4j.TwitchClientBuilder +import com.github.twitch4j.events.ChannelGoLiveEvent import dev.jansel.feixiao.database.Database import dev.jansel.feixiao.database.collections.MetaCollection import dev.jansel.feixiao.database.collections.StreamerCollection +import dev.jansel.feixiao.twitchClient import dev.kord.common.entity.Snowflake +import dev.kord.core.entity.channel.GuildMessageChannel import dev.kordex.core.builders.ExtensibleBotBuilder import dev.kordex.core.utils.env import dev.kordex.core.utils.loadModule +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import org.koin.dsl.bind @@ -37,3 +42,18 @@ suspend inline fun ExtensibleBotBuilder.database(migrate: Boolean) { } } } + +suspend inline fun ExtensibleBotBuilder.twitch(active: Boolean) { + hooks { + beforeKoinSetup { + loadModule { + single { Twitch() } bind Twitch::class + } + + if (active) { + Twitch().init() + } + } + } + +}