From be28f7ae7eb1037ecb6ff8e44a5ab12b00df9ae8 Mon Sep 17 00:00:00 2001 From: Jannik Reimers Date: Thu, 21 Nov 2024 08:58:36 +0100 Subject: [PATCH] try a different approach on loading stuff --- .idea/compiler.xml | 2 +- .idea/misc.xml | 2 +- src/main/kotlin/dev/jansel/feixiao/App.kt | 27 ++----------- .../kotlin/dev/jansel/feixiao/utils/Twitch.kt | 40 +++++++++++++++++++ .../kotlin/dev/jansel/feixiao/utils/_Utils.kt | 20 ++++++++++ 5 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 src/main/kotlin/dev/jansel/feixiao/utils/Twitch.kt diff --git a/.idea/compiler.xml b/.idea/compiler.xml index ed2ab47..f4f3637 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,7 +1,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index c6d38c5..a4abeb1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/src/main/kotlin/dev/jansel/feixiao/App.kt b/src/main/kotlin/dev/jansel/feixiao/App.kt index f97e1dd..a2495b1 100644 --- a/src/main/kotlin/dev/jansel/feixiao/App.kt +++ b/src/main/kotlin/dev/jansel/feixiao/App.kt @@ -19,39 +19,20 @@ import kotlinx.coroutines.runBlocking var twitchClient: TwitchClient? = null val logger = KotlinLogging.logger { } +var botRef : ExtensibleBot? = null suspend fun main() { - val bot = ExtensibleBot(token) { + botRef = ExtensibleBot(token) { database(true) + twitch(true) dataCollectionMode = DataCollection.None extensions { add(::EventHooks) add(::StreamerCommand) } } - twitchClient = TwitchClientBuilder.builder() - .withEnableHelix(true) - .withClientId(twitchcid) - .withClientSecret(twitchcs) - .build() - twitchClient!!.eventManager.onEvent(ChannelGoLiveEvent::class.java) { - logger.info { "${it.channel.name} went live!" } - runBlocking { - launch { - val streamer = StreamerCollection().getData(it.channel.name) - val channel = bot.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}") - } - } - } - } - - 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() + } + } + } + +}