Streamers are now saved by both name and id, and on startup updates to the new name if there is one.
All checks were successful
Build & Publish / build (push) Successful in 4m52s
All checks were successful
Build & Publish / build (push) Successful in 4m52s
This commit is contained in:
parent
b680ce4ef4
commit
836e938dce
@ -3,6 +3,7 @@ package dev.jansel.feixiao.database
|
||||
import dev.jansel.feixiao.database.collections.MetaCollection
|
||||
import dev.jansel.feixiao.database.entities.MetaData
|
||||
import dev.jansel.feixiao.database.migrations.v1
|
||||
import dev.jansel.feixiao.database.migrations.v2
|
||||
import dev.kordex.core.koin.KordExKoinComponent
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import org.koin.core.component.inject
|
||||
@ -35,6 +36,7 @@ object Migrator : KordExKoinComponent {
|
||||
try {
|
||||
when (nextVersion) {
|
||||
1 -> ::v1
|
||||
2 -> ::v2
|
||||
else -> break
|
||||
}(db.mongo)
|
||||
|
||||
|
@ -3,6 +3,7 @@ package dev.jansel.feixiao.database.collections
|
||||
import dev.jansel.feixiao.database.Database
|
||||
import dev.jansel.feixiao.database.entities.Server
|
||||
import dev.jansel.feixiao.database.entities.StreamerData
|
||||
import dev.jansel.feixiao.utils.getTwitchIdByName
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import dev.kordex.core.koin.KordExKoinComponent
|
||||
import org.koin.core.component.inject
|
||||
@ -35,7 +36,7 @@ class StreamerCollection : KordExKoinComponent {
|
||||
)
|
||||
} else {
|
||||
collection.insertOne(
|
||||
StreamerData(streamerName, listOf(Server(guildId, channelId, roleId, liveMessage)))
|
||||
StreamerData(streamerName, getTwitchIdByName(streamerName), listOf(Server(guildId, channelId, roleId, liveMessage)))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import kotlinx.serialization.Serializable
|
||||
@Serializable
|
||||
data class StreamerData(
|
||||
val name: String,
|
||||
val id: String?,
|
||||
val servers: List<Server>
|
||||
)
|
||||
|
||||
|
16
src/main/kotlin/dev/jansel/feixiao/database/migrations/v2.kt
Normal file
16
src/main/kotlin/dev/jansel/feixiao/database/migrations/v2.kt
Normal file
@ -0,0 +1,16 @@
|
||||
package dev.jansel.feixiao.database.migrations
|
||||
|
||||
import dev.jansel.feixiao.database.entities.StreamerData
|
||||
import dev.jansel.feixiao.utils.getTwitchIdByName
|
||||
import org.litote.kmongo.coroutine.CoroutineDatabase
|
||||
import org.litote.kmongo.eq
|
||||
import org.litote.kmongo.setValue
|
||||
|
||||
suspend fun v2(db: CoroutineDatabase) {
|
||||
db.getCollection<StreamerData>("streamerData").findOne(StreamerData::id eq null)?.let {
|
||||
db.getCollection<StreamerData>("streamerData").updateOne(
|
||||
StreamerData::name eq it.name,
|
||||
setValue(StreamerData::id, getTwitchIdByName(it.name))
|
||||
)
|
||||
}
|
||||
}
|
@ -4,6 +4,8 @@ import dev.jansel.feixiao.database.collections.StreamerCollection
|
||||
import dev.jansel.feixiao.database.entities.StreamerData
|
||||
import dev.jansel.feixiao.logger
|
||||
import dev.jansel.feixiao.twitchClient
|
||||
import dev.jansel.feixiao.utils.getTwitchIdByName
|
||||
import dev.jansel.feixiao.utils.getTwitchNameById
|
||||
import dev.jansel.feixiao.utils.tchannelid
|
||||
import dev.jansel.feixiao.utils.tserverid
|
||||
import dev.kord.core.behavior.getChannelOf
|
||||
@ -12,6 +14,7 @@ import dev.kord.core.event.gateway.ReadyEvent
|
||||
import dev.kordex.core.extensions.Extension
|
||||
import dev.kordex.core.extensions.event
|
||||
import org.litote.kmongo.eq
|
||||
import org.litote.kmongo.setValue
|
||||
|
||||
class EventHooks : Extension() {
|
||||
override val name = "eventhooks"
|
||||
@ -27,8 +30,10 @@ class EventHooks : Extension() {
|
||||
// check every entry in the database and enable the stream event listener if a server is listening to the streamer
|
||||
StreamerCollection().collection.find().toList().forEach {
|
||||
if (it.servers.isNotEmpty()) {
|
||||
twitchClient!!.clientHelper.enableStreamEventListener(it.name)
|
||||
logger.info { "Enabled stream event listener for ${it.name}" }
|
||||
val currentName = getTwitchNameById(it.id!!)
|
||||
twitchClient!!.clientHelper.enableStreamEventListener(currentName)
|
||||
logger.info { "Enabled stream event listener for $currentName" }
|
||||
StreamerCollection().collection.updateOne(StreamerData::name eq it.name, setValue(StreamerData::name, currentName))
|
||||
} else {
|
||||
logger.info { "No servers are listening to ${it.name}, deleting from the database..." }
|
||||
StreamerCollection().collection.deleteMany(StreamerData::name eq it.name)
|
||||
|
@ -3,6 +3,7 @@ package dev.jansel.feixiao.utils
|
||||
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.kordex.core.builders.ExtensibleBotBuilder
|
||||
import dev.kordex.core.utils.env
|
||||
@ -52,3 +53,23 @@ suspend inline fun ExtensibleBotBuilder.twitch(active: Boolean) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getTwitchNameById(id: String): String? {
|
||||
val resultList = twitchClient!!.helix?.getUsers(null, listOf(id), null)?.execute()
|
||||
resultList?.users?.forEach { user ->
|
||||
if (user.id == id) {
|
||||
return user.displayName
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun getTwitchIdByName(name: String): String? {
|
||||
val resultList = twitchClient!!.helix?.getUsers(null, null, listOf(name))?.execute()
|
||||
resultList?.users?.forEach { user ->
|
||||
if (user.displayName == name) {
|
||||
return user.id
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user