1
0
This commit is contained in:
Jannik Reimers 2024-11-21 08:58:36 +01:00
parent feb82122b8
commit e05bd9fe9c
Signed by: jansel
GPG Key ID: 39C62D7D5233CFD0
3 changed files with 66 additions and 55 deletions

View File

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

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