try a different approach on loading stuff

This commit is contained in:
Jannik Reimers 2024-11-21 08:58:36 +01:00
parent c31f2cc149
commit be28f7ae7e
Signed by: jansel
GPG key ID: 39C62D7D5233CFD0
5 changed files with 66 additions and 25 deletions

View file

@ -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<GuildMessageChannel>(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()
}

View file

@ -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<GuildMessageChannel>(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}")
}
}
}
}
}
}

View file

@ -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()
}
}
}
}