feat: reflections for services + commands, move towards mongo
This commit is contained in:
@@ -43,17 +43,26 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
listOf("stdlib-jdk8", "reflect").forEach { implementation(kotlin(it)) }
|
listOf("stdlib-jdk8", "reflect").forEach { implementation(kotlin(it)) }
|
||||||
|
|
||||||
|
// Discord
|
||||||
implementation("net.dv8tion:JDA:4.4.0_350")
|
implementation("net.dv8tion:JDA:4.4.0_350")
|
||||||
implementation("tech.junodevs.discord:kriess:0.14.0")
|
implementation("tech.junodevs.discord:kriess:0.14.0")
|
||||||
|
|
||||||
|
// Utilities
|
||||||
implementation("ch.qos.logback:logback-classic:1.4.4")
|
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("org.mnode.ical4j:ical4j:3.2.6")
|
||||||
|
|
||||||
implementation("com.squareup.okhttp3:okhttp:4.10.0")
|
implementation("com.squareup.okhttp3:okhttp:4.10.0")
|
||||||
|
|
||||||
// Utilities
|
// Data
|
||||||
implementation("org.yaml:snakeyaml:1.31")
|
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"))
|
implementation(kotlin("stdlib-jdk8"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
major=0
|
major=0
|
||||||
minor=1
|
minor=2
|
||||||
patch=0
|
patch=0
|
||||||
|
|
||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
|
|||||||
@@ -18,10 +18,12 @@ import net.fortuna.ical4j.model.Date
|
|||||||
import net.fortuna.ical4j.model.DateTime
|
import net.fortuna.ical4j.model.DateTime
|
||||||
import net.fortuna.ical4j.model.Period
|
import net.fortuna.ical4j.model.Period
|
||||||
import net.fortuna.ical4j.model.component.VEvent
|
import net.fortuna.ical4j.model.component.VEvent
|
||||||
|
import org.reflections.Reflections
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.yaml.snakeyaml.Yaml
|
import org.yaml.snakeyaml.Yaml
|
||||||
import tech.junodevs.discord.kriess.impl.managers.CommandManager
|
import tech.junodevs.discord.kriess.impl.managers.CommandManager
|
||||||
import tech.junodevs.discord.kriess.menus.MenuListener
|
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.CommandBase
|
||||||
import xyz.brettb.discord.ieeevents.commands.info.HelpCommand
|
import xyz.brettb.discord.ieeevents.commands.info.HelpCommand
|
||||||
import xyz.brettb.discord.ieeevents.commands.info.UpcomingEventsCommand
|
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.IEEEventsGuildSettings
|
||||||
import xyz.brettb.discord.ieeevents.data.settings.IEEEventsGuildSettingsManager
|
import xyz.brettb.discord.ieeevents.data.settings.IEEEventsGuildSettingsManager
|
||||||
import xyz.brettb.discord.ieeevents.services.StatusService
|
import xyz.brettb.discord.ieeevents.services.StatusService
|
||||||
|
import xyz.brettb.discord.ieeevents.services.ToggleableService
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
@@ -40,7 +43,7 @@ val logger = LoggerFactory.getLogger(IEEEventsBot.javaClass)
|
|||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
logger.info("IEEEvents Bot")
|
logger.info("IEEEvents Bot")
|
||||||
|
|
||||||
IEEEventsBot.load()
|
IEEEventsBot.load()
|
||||||
|
|
||||||
IEEEventsBot.JDA = JDABuilder
|
IEEEventsBot.JDA = JDABuilder
|
||||||
@@ -66,7 +69,9 @@ fun main() {
|
|||||||
|
|
||||||
// Ensure the StatusService stops correctly
|
// Ensure the StatusService stops correctly
|
||||||
Runtime.getRuntime().addShutdownHook(Thread {
|
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.
|
* The commands the bot has.
|
||||||
*/
|
*/
|
||||||
val commands: List<CommandBase> = listOf(
|
val commands: List<Class<out CommandBase>> =
|
||||||
// SetEventsChannelCommand,
|
Reflections("xyz.brettb.discord.ieeevents.commands").getSubTypesOf(CommandBase::class.java)
|
||||||
HelpCommand,
|
.filterNot {
|
||||||
ChangePrefixCommand,
|
it.isMemberClass
|
||||||
UpcomingEventsCommand,
|
}
|
||||||
// PingCommand
|
.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
|
* Initializes the bot
|
||||||
@@ -180,14 +205,16 @@ object IEEEventsBot : EventListener {
|
|||||||
|
|
||||||
// Initialize the commands
|
// Initialize the commands
|
||||||
commands.forEach { command ->
|
commands.forEach { command ->
|
||||||
commandManager.addCommand(command)
|
commandManager.addCommand((command.getDeclaredField("INSTANCE")[null] as CommandBase))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onEvent(event: GenericEvent) {
|
override fun onEvent(event: GenericEvent) {
|
||||||
when (event) {
|
when (event) {
|
||||||
is ReadyEvent -> {
|
is ReadyEvent -> {
|
||||||
StatusService.start()
|
services.forEach { service ->
|
||||||
|
(service.getDeclaredField("INSTANCE")[null] as ToggleableService).start()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import xyz.brettb.discord.ieeevents.IEEEventsBot
|
|||||||
import xyz.brettb.discord.ieeevents.data.settings.settings
|
import xyz.brettb.discord.ieeevents.data.settings.settings
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
object EventsService : Service(TimeUnit.MINUTES.toSeconds(30), 15) {
|
object EventsService : ToggleableService(TimeUnit.MINUTES.toSeconds(30), 15) {
|
||||||
|
|
||||||
override fun execute() {
|
override fun execute() {
|
||||||
val events = IEEEventsBot.events
|
val events = IEEEventsBot.events
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import net.dv8tion.jda.api.entities.Activity
|
|||||||
import tech.junodevs.discord.kriess.services.Service
|
import tech.junodevs.discord.kriess.services.Service
|
||||||
import xyz.brettb.discord.ieeevents.IEEEventsBot
|
import xyz.brettb.discord.ieeevents.IEEEventsBot
|
||||||
|
|
||||||
object StatusService : Service(60, 0) {
|
object StatusService : ToggleableService(60, 0) {
|
||||||
|
|
||||||
override fun execute() {
|
override fun execute() {
|
||||||
if (IEEEventsBot.updateStatus) {
|
if (IEEEventsBot.updateStatus) {
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user