From ad8247c69089c976f2b8724af14d2555891c68e9 Mon Sep 17 00:00:00 2001 From: Brett Bender Date: Thu, 3 Nov 2022 17:03:02 -0500 Subject: [PATCH] feat: mongo database, seteventchannel command --- gradle.properties | 2 +- .../xyz/brettb/discord/ieeevents/IEEEvents.kt | 20 ++++++++- .../settings/SetEventChannelCommand.kt | 42 +++++++++++++++++++ .../data/settings/IEEEventsGuildSettings.kt | 4 +- .../settings/IEEEventsGuildSettingsManager.kt | 42 ++++++++++++++----- 5 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/xyz/brettb/discord/ieeevents/commands/settings/SetEventChannelCommand.kt diff --git a/gradle.properties b/gradle.properties index e447623..9d713f6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ major=0 -minor=4 +minor=5 patch=0 kotlin.code.style=official diff --git a/src/main/kotlin/xyz/brettb/discord/ieeevents/IEEEvents.kt b/src/main/kotlin/xyz/brettb/discord/ieeevents/IEEEvents.kt index e7b7247..9d325c5 100644 --- a/src/main/kotlin/xyz/brettb/discord/ieeevents/IEEEvents.kt +++ b/src/main/kotlin/xyz/brettb/discord/ieeevents/IEEEvents.kt @@ -2,6 +2,8 @@ package xyz.brettb.discord.ieeevents import ch.qos.logback.classic.Level import ch.qos.logback.classic.Logger +import com.mongodb.client.MongoClient +import com.mongodb.client.MongoDatabase import com.sksamuel.hoplite.ConfigLoader import net.dv8tion.jda.api.JDA import net.dv8tion.jda.api.JDABuilder @@ -20,6 +22,7 @@ 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.litote.kmongo.KMongo import org.reflections.Reflections import org.slf4j.LoggerFactory import tech.junodevs.discord.kriess.impl.managers.CommandManager @@ -95,10 +98,20 @@ object IEEEventsBot : EventListener { lateinit var commandManager: CommandManager /** - * The bot [config]uration + * The bot [config] */ lateinit var config: BotConfig + /** + * The [MongoClient] used by the bot + */ + lateinit var dbClient: MongoClient + + /** + * The [MongoDatabase] used by the bot + */ + lateinit var database: MongoDatabase + /** * The iCal [Calendar] object gotten from the [BotConfig.calendarUrl]. */ @@ -176,9 +189,14 @@ object IEEEventsBot : EventListener { config = ConfigLoader().loadConfigOrThrow(configFile) + // Loger val level = Level.toLevel(config.logLevel, Level.INFO) (LoggerFactory.getLogger("ROOT") as Logger).level = level + // Database + dbClient = KMongo.createClient(config.database.url) + database = dbClient.getDatabase(config.database.name) + // Managers IEEEventsGuildSettingsManager.start() commandManager = CommandManager(IEEEventsGuildSettingsManager, config.prefix) { cEvent, t -> diff --git a/src/main/kotlin/xyz/brettb/discord/ieeevents/commands/settings/SetEventChannelCommand.kt b/src/main/kotlin/xyz/brettb/discord/ieeevents/commands/settings/SetEventChannelCommand.kt new file mode 100644 index 0000000..a74238d --- /dev/null +++ b/src/main/kotlin/xyz/brettb/discord/ieeevents/commands/settings/SetEventChannelCommand.kt @@ -0,0 +1,42 @@ +package xyz.brettb.discord.ieeevents.commands.settings + +import net.dv8tion.jda.api.Permission +import tech.junodevs.discord.kriess.command.CommandEvent +import xyz.brettb.discord.ieeevents.commands.CommandBase +import xyz.brettb.discord.ieeevents.commands.CommandCategories +import xyz.brettb.discord.ieeevents.data.settings.IEEEventsGuildSettingsManager + +/** + * The [SetEventChannelCommand] allows you to change the bots' prefix in a given server. + */ +@Suppress("unused") +object SetEventChannelCommand : CommandBase( + "seteventchannel", + "Set the channel for events to be posted to", + CommandCategories.SETTINGS, + listOf("sec"), + "[new_channel:channel]", + true, + false, + listOf(Permission.MANAGE_SERVER) +) { + + override fun handle(event: CommandEvent) { + val newChannel = if (event.arguments.channel("new_channel") == null) { + event.textChannel + } else { + event.arguments.channel("new_channel")!! + } + + if (newChannel.guild.idLong != event.guild.idLong) { + event.replyError("Can't set the event channel to a channel in another guild!") + return + } + + IEEEventsGuildSettingsManager.editSettings(event.guild) { + eventChannelID = newChannel.idLong + event.reply(":white_check_mark: Updated event channel to ${newChannel.asMention}") + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettings.kt b/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettings.kt index 591d370..f1f4f97 100644 --- a/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettings.kt +++ b/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettings.kt @@ -18,7 +18,7 @@ class IEEEventsGuildSettings( /** * The ID of the channel where the events should be sent. */ - @Suppress("MemberVisibilityCanBePrivate") val eventChannelID: Long? = null, + var eventChannelID: Long? = null, /** * Should we mirror events to this guild from the calendar? */ @@ -33,7 +33,7 @@ class IEEEventsGuildSettings( */ @Suppress("unused") val eventChannel: TextChannel? - get() = if (eventChannelID != null) IEEEventsBot.JDA.getTextChannelById(eventChannelID) else null + get() = if (eventChannelID != null) IEEEventsBot.JDA.getTextChannelById(eventChannelID!!) else null override fun toMap(): Map { return mapOf( diff --git a/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettingsManager.kt b/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettingsManager.kt index 4f80760..9aa2726 100644 --- a/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettingsManager.kt +++ b/src/main/kotlin/xyz/brettb/discord/ieeevents/data/settings/IEEEventsGuildSettingsManager.kt @@ -1,20 +1,40 @@ package xyz.brettb.discord.ieeevents.data.settings -import tech.junodevs.discord.kriess.impl.managers.GuildSettingsManager +import com.mongodb.client.MongoCollection +import net.dv8tion.jda.api.entities.Guild +import org.litote.kmongo.eq +import org.litote.kmongo.findOne +import org.litote.kmongo.getCollection +import org.litote.kmongo.updateOne +import tech.junodevs.discord.kriess.managers.GuildSettingsManager +import xyz.brettb.discord.ieeevents.IEEEventsBot +import java.util.concurrent.CompletableFuture -object IEEEventsGuildSettingsManager : GuildSettingsManager() { +object IEEEventsGuildSettingsManager : GuildSettingsManager { - override fun createAbsentInstance(guildId: Long): IEEEventsGuildSettings { - return IEEEventsGuildSettings(guildId) + private lateinit var settings: MongoCollection + + override fun start() { + settings = IEEEventsBot.database.getCollection() + } + + override fun editSettings(guild: Guild, action: IEEEventsGuildSettings.() -> Unit) { + getSettingsFor(guild).thenAccept { + action.invoke(it) + + settings.updateOne(IEEEventsGuildSettings::guildid eq guild.idLong, it) + } } - override fun createInstance(guildId: Long, properties: Map): IEEEventsGuildSettings { - return IEEEventsGuildSettings( - guildId, - properties["prefix"] as String?, - properties["eventChannelID"] as Long?, - properties["mirrorEvents"] as Boolean? ?: false - ) + override fun getSettingsFor(guild: Guild): CompletableFuture { + val future = CompletableFuture() + + settings.findOne(IEEEventsGuildSettings::guildid eq guild.idLong) + ?: settings.insertOne(IEEEventsGuildSettings(guildid = guild.idLong)) + + future.complete(settings.findOne(IEEEventsGuildSettings::guildid eq guild.idLong)!!) + + return future } } \ No newline at end of file