|
|
|
@ -2,6 +2,7 @@ package xyz.brettb.discord.ieeevents |
|
|
|
|
|
|
|
import ch.qos.logback.classic.Level |
|
|
|
import ch.qos.logback.classic.Logger |
|
|
|
import com.sksamuel.hoplite.ConfigLoader |
|
|
|
import net.dv8tion.jda.api.JDA |
|
|
|
import net.dv8tion.jda.api.JDABuilder |
|
|
|
import net.dv8tion.jda.api.Permission |
|
|
|
@ -20,25 +21,20 @@ 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 |
|
|
|
import xyz.brettb.discord.ieeevents.commands.settings.ChangePrefixCommand |
|
|
|
import xyz.brettb.discord.ieeevents.data.settings.BotConfig |
|
|
|
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 |
|
|
|
import java.time.Duration |
|
|
|
import java.time.temporal.ChronoUnit |
|
|
|
import kotlin.system.exitProcess |
|
|
|
|
|
|
|
@Suppress("HasPlatformType") |
|
|
|
val logger = LoggerFactory.getLogger(IEEEventsBot.javaClass) |
|
|
|
|
|
|
|
fun main() { |
|
|
|
@ -47,7 +43,7 @@ fun main() { |
|
|
|
IEEEventsBot.load() |
|
|
|
|
|
|
|
IEEEventsBot.JDA = JDABuilder |
|
|
|
.createDefault(IEEEventsBot.token) |
|
|
|
.createDefault(IEEEventsBot.config.token) |
|
|
|
.enableIntents(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MEMBERS) |
|
|
|
.addEventListeners(IEEEventsBot.commandManager, IEEEventsBot, MenuListener) |
|
|
|
.setChunkingFilter(ChunkingFilter.ALL) |
|
|
|
@ -69,13 +65,16 @@ fun main() { |
|
|
|
|
|
|
|
// Ensure the StatusService stops correctly |
|
|
|
Runtime.getRuntime().addShutdownHook(Thread { |
|
|
|
IEEEventsBot.services.forEach{ service -> |
|
|
|
IEEEventsBot.services.forEach { service -> |
|
|
|
(service.getDeclaredField("INSTANCE")[null] as ToggleableService).shutdown() |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* The IEEE Event Mirroring Bot |
|
|
|
*/ |
|
|
|
object IEEEventsBot : EventListener { |
|
|
|
/** |
|
|
|
* The [JDA] instance used by the bot. |
|
|
|
@ -88,32 +87,18 @@ object IEEEventsBot : EventListener { |
|
|
|
lateinit var commandManager: CommandManager<IEEEventsGuildSettings> |
|
|
|
|
|
|
|
/** |
|
|
|
* The YAML instance used. |
|
|
|
* The bot [config]uration |
|
|
|
*/ |
|
|
|
private val yaml = Yaml() |
|
|
|
lateinit var config: BotConfig |
|
|
|
|
|
|
|
/** |
|
|
|
* The bot's default prefix. |
|
|
|
*/ |
|
|
|
lateinit var prefix: String |
|
|
|
|
|
|
|
/** |
|
|
|
* The bot's login token. |
|
|
|
*/ |
|
|
|
lateinit var token: String |
|
|
|
|
|
|
|
/** |
|
|
|
* The calendar url that we're attempting to mirror between discord + the source. |
|
|
|
*/ |
|
|
|
lateinit var calendarUrl: String |
|
|
|
|
|
|
|
/** |
|
|
|
* The iCal [Calendar] object gotten from the [calendarUrl]. |
|
|
|
* The iCal [Calendar] object gotten from the [BotConfig.calendarUrl]. |
|
|
|
*/ |
|
|
|
@Suppress("MemberVisibilityCanBePrivate") |
|
|
|
val calendar: Calendar? |
|
|
|
get() = |
|
|
|
try { |
|
|
|
val inputStream = Utils.downloadCalendar(calendarUrl) |
|
|
|
val inputStream = Utils.downloadCalendar(config.calendarUrl) |
|
|
|
CalendarBuilder().build(inputStream) |
|
|
|
} catch (t: Throwable) { |
|
|
|
null |
|
|
|
@ -137,13 +122,9 @@ object IEEEventsBot : EventListener { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Is the bot going to update its status? |
|
|
|
*/ |
|
|
|
var updateStatus: Boolean = true |
|
|
|
|
|
|
|
/** |
|
|
|
* The commands the bot has. |
|
|
|
* The [commands] the bot has. |
|
|
|
*/ |
|
|
|
@Suppress("MemberVisibilityCanBePrivate") |
|
|
|
val commands: List<Class<out CommandBase>> = |
|
|
|
Reflections("xyz.brettb.discord.ieeevents.commands").getSubTypesOf(CommandBase::class.java) |
|
|
|
.filterNot { |
|
|
|
@ -159,7 +140,7 @@ object IEEEventsBot : EventListener { |
|
|
|
.sortedBy { it.name } |
|
|
|
|
|
|
|
/** |
|
|
|
* The services the bot is using. |
|
|
|
* The [services] the bot is using. |
|
|
|
*/ |
|
|
|
val services: List<Class<out ToggleableService>> = |
|
|
|
Reflections("xyz.brettb.discord.ieeevents.services").getSubTypesOf(ToggleableService::class.java) |
|
|
|
@ -185,20 +166,14 @@ object IEEEventsBot : EventListener { |
|
|
|
exitProcess(1) |
|
|
|
} |
|
|
|
|
|
|
|
val output = yaml.load(FileInputStream(configFile)) as Map<String, Any> |
|
|
|
prefix = output["prefix"].toString() |
|
|
|
token = output["token"].toString() |
|
|
|
updateStatus = output["update-status"]?.toString()?.toBoolean() ?: true |
|
|
|
calendarUrl = output["calendar-url"].toString() |
|
|
|
|
|
|
|
val logLevel = output["log-level"]?.toString() ?: "INFO" |
|
|
|
config = ConfigLoader().loadConfigOrThrow(configFile) |
|
|
|
|
|
|
|
val level = Level.toLevel(logLevel, Level.INFO) |
|
|
|
val level = Level.toLevel(config.logLevel, Level.INFO) |
|
|
|
(LoggerFactory.getLogger("ROOT") as Logger).level = level |
|
|
|
|
|
|
|
// Managers |
|
|
|
IEEEventsGuildSettingsManager.start() |
|
|
|
commandManager = CommandManager(IEEEventsGuildSettingsManager, prefix) { cEvent, t -> |
|
|
|
commandManager = CommandManager(IEEEventsGuildSettingsManager, config.prefix) { cEvent, t -> |
|
|
|
cEvent.replyError("An unknown error occurred!", { }, { }) |
|
|
|
logger.error("An uncaught exception occurred in a command: $t") |
|
|
|
} |
|
|
|
|