feat: reflections for services + commands, move towards mongo

This commit is contained in:
2022-11-02 20:26:40 -05:00
parent b6f95c3ef6
commit 565b3ec456
6 changed files with 58 additions and 15 deletions

View File

@@ -43,17 +43,26 @@ repositories {
dependencies {
listOf("stdlib-jdk8", "reflect").forEach { implementation(kotlin(it)) }
// Discord
implementation("net.dv8tion:JDA:4.4.0_350")
implementation("tech.junodevs.discord:kriess:0.14.0")
// Utilities
implementation("ch.qos.logback:logback-classic:1.4.4")
implementation("org.reflections:reflections:0.10.2")
// Calendar
implementation("org.mnode.ical4j:ical4j:3.2.6")
implementation("com.squareup.okhttp3:okhttp:4.10.0")
// Utilities
// Data
implementation("org.yaml:snakeyaml:1.31")
implementation("org.litote.kmongo:kmongo:4.7.2")
implementation("com.sksamuel.hoplite:hoplite-core:2.6.5")
implementation("com.sksamuel.hoplite:hoplite-yaml:2.6.5")
implementation(kotlin("stdlib-jdk8"))
}

View File

@@ -1,5 +1,5 @@
major=0
minor=1
minor=2
patch=0
kotlin.code.style=official

View File

@@ -18,10 +18,12 @@ import net.fortuna.ical4j.model.Date
import net.fortuna.ical4j.model.DateTime
import net.fortuna.ical4j.model.Period
import net.fortuna.ical4j.model.component.VEvent
import org.reflections.Reflections
import org.slf4j.LoggerFactory
import org.yaml.snakeyaml.Yaml
import tech.junodevs.discord.kriess.impl.managers.CommandManager
import tech.junodevs.discord.kriess.menus.MenuListener
import tech.junodevs.discord.kriess.services.Service
import xyz.brettb.discord.ieeevents.commands.CommandBase
import xyz.brettb.discord.ieeevents.commands.info.HelpCommand
import xyz.brettb.discord.ieeevents.commands.info.UpcomingEventsCommand
@@ -29,6 +31,7 @@ import xyz.brettb.discord.ieeevents.commands.settings.ChangePrefixCommand
import xyz.brettb.discord.ieeevents.data.settings.IEEEventsGuildSettings
import xyz.brettb.discord.ieeevents.data.settings.IEEEventsGuildSettingsManager
import xyz.brettb.discord.ieeevents.services.StatusService
import xyz.brettb.discord.ieeevents.services.ToggleableService
import java.io.File
import java.io.FileInputStream
import java.nio.file.Files
@@ -66,7 +69,9 @@ fun main() {
// Ensure the StatusService stops correctly
Runtime.getRuntime().addShutdownHook(Thread {
StatusService.shutdown()
IEEEventsBot.services.forEach{ service ->
(service.getDeclaredField("INSTANCE")[null] as ToggleableService).shutdown()
}
})
}
@@ -139,13 +144,33 @@ object IEEEventsBot : EventListener {
/**
* The commands the bot has.
*/
val commands: List<CommandBase> = listOf(
// SetEventsChannelCommand,
HelpCommand,
ChangePrefixCommand,
UpcomingEventsCommand,
// PingCommand
)
val commands: List<Class<out CommandBase>> =
Reflections("xyz.brettb.discord.ieeevents.commands").getSubTypesOf(CommandBase::class.java)
.filterNot {
it.isMemberClass
}
.filter {
try {
it.getDeclaredField("INSTANCE")[null]; true
} catch (_: Throwable) {
false
}
}
.sortedBy { it.name }
/**
* The services the bot is using.
*/
val services: List<Class<out ToggleableService>> =
Reflections("xyz.brettb.discord.ieeevents.services").getSubTypesOf(ToggleableService::class.java)
.filter {
try {
val ts = it.getDeclaredField("INSTANCE")[null] as ToggleableService
ts.enabled
} catch (_: Throwable) {
false
}
}
/**
* Initializes the bot
@@ -180,14 +205,16 @@ object IEEEventsBot : EventListener {
// Initialize the commands
commands.forEach { command ->
commandManager.addCommand(command)
commandManager.addCommand((command.getDeclaredField("INSTANCE")[null] as CommandBase))
}
}
override fun onEvent(event: GenericEvent) {
when (event) {
is ReadyEvent -> {
StatusService.start()
services.forEach { service ->
(service.getDeclaredField("INSTANCE")[null] as ToggleableService).start()
}
}
}
}

View File

@@ -5,7 +5,7 @@ import xyz.brettb.discord.ieeevents.IEEEventsBot
import xyz.brettb.discord.ieeevents.data.settings.settings
import java.util.concurrent.TimeUnit
object EventsService : Service(TimeUnit.MINUTES.toSeconds(30), 15) {
object EventsService : ToggleableService(TimeUnit.MINUTES.toSeconds(30), 15) {
override fun execute() {
val events = IEEEventsBot.events

View File

@@ -5,7 +5,7 @@ import net.dv8tion.jda.api.entities.Activity
import tech.junodevs.discord.kriess.services.Service
import xyz.brettb.discord.ieeevents.IEEEventsBot
object StatusService : Service(60, 0) {
object StatusService : ToggleableService(60, 0) {
override fun execute() {
if (IEEEventsBot.updateStatus) {

View File

@@ -0,0 +1,7 @@
package xyz.brettb.discord.ieeevents.services
import tech.junodevs.discord.kriess.services.Service
abstract class ToggleableService(period: Long, initial: Long = period, val enabled: Boolean = true) :
Service(initial, period) {
}