From 68a4b36d3901101718f6c17395a5db00e515043d Mon Sep 17 00:00:00 2001 From: NoComment Date: Tue, 14 Jan 2025 14:00:45 +0000 Subject: [PATCH] Begin adjusting to be a generalized multiserver bot. Add info commands and stuff --- .github/workflows/build.yml | 2 +- config.example.properties | 11 --- .../nocomment1105/modmailbot/ModMailBot.kt | 95 +++++++++++++++---- .../nocomment1105/modmailbot/_Constants.kt | 13 +-- .../github/nocomment1105/modmailbot/_Utils.kt | 9 +- .../extensions/commands/CloseCommands.kt | 7 +- .../extensions/commands/ReplyCommands.kt | 12 ++- .../extensions/events/MessageEditing.kt | 8 +- .../extensions/events/MessageReceiving.kt | 15 +-- .../modmailbot/strings.properties | 11 +++ 10 files changed, 123 insertions(+), 60 deletions(-) delete mode 100644 config.example.properties diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b5fb958..2f26052 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -32,4 +32,4 @@ jobs: uses: actions/upload-artifact@v4 with: name: Build Only Artifacts - path: build/libs/*[0-9].jar + path: build/libs/*[0-9]*.jar diff --git a/config.example.properties b/config.example.properties deleted file mode 100644 index ef9e92f..0000000 --- a/config.example.properties +++ /dev/null @@ -1,11 +0,0 @@ -# -- Required Properties -- # -bot_token= -mail_server_id= -main_server_id= -# -- End Required Properties -- # - -# -- Optional Properties -- # -# statusType= -# status= -# mongo_uri -# -- End Optional Properties -- # diff --git a/src/main/kotlin/io/github/nocomment1105/modmailbot/ModMailBot.kt b/src/main/kotlin/io/github/nocomment1105/modmailbot/ModMailBot.kt index 9a78c00..63f970c 100644 --- a/src/main/kotlin/io/github/nocomment1105/modmailbot/ModMailBot.kt +++ b/src/main/kotlin/io/github/nocomment1105/modmailbot/ModMailBot.kt @@ -13,32 +13,25 @@ package io.github.nocomment1105.modmailbot import dev.kord.cache.map.MapLikeCollection import dev.kord.cache.map.internal.MapEntryCache +import dev.kord.cache.map.lruLinkedHashMap import dev.kord.gateway.Intent import dev.kord.gateway.PrivilegedIntent +import dev.kord.rest.builder.message.actionRow +import dev.kord.rest.builder.message.embed import dev.kordex.core.ExtensibleBot +import dev.kordex.core.i18n.SupportedLocales +import dev.kordex.data.api.DataCollection import io.github.nocomment1105.modmailbot.extensions.commands.CloseCommands import io.github.nocomment1105.modmailbot.extensions.commands.ReplyCommands import io.github.nocomment1105.modmailbot.extensions.events.MessageEditing import io.github.nocomment1105.modmailbot.extensions.events.MessageReceiving -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import java.io.FileInputStream -import java.util.* - -val file = FileInputStream("config.properties") -val config = Properties() +import modmailbot.i18n.Translations suspend fun main() { - withContext(Dispatchers.IO) { - config.load(file) - } - val bot = ExtensibleBot(BOT_TOKEN) { - database(false) + dataCollectionMode = DataCollection.None - applicationCommands { - defaultGuild(MAIL_SERVER) - } + database(false) extensions { add(::MessageReceiving) @@ -60,20 +53,80 @@ suspend fun main() { } presence { - when (config.getProperty("statusType")) { - "playing" -> playing(config.getProperty("status")) - "watching" -> watching(config.getProperty("status")) - else -> watching("for your DMs!") - } + watching("for your DMs!") } kord { + stackTraceRecovery = true + cache { messages { cache, description -> - MapEntryCache(cache, description, MapLikeCollection.concurrentHashMap()) + // Set a max message cache size of 1000 messages to avoid creating a crazy large cache + MapEntryCache(cache, description, MapLikeCollection.lruLinkedHashMap(1000)) + } + } + } + + about { + ephemeral = false + general { + message { locale -> + embed { + title = Translations.About.embedTitle.translate() + + // TODO A logo that can go here +// thumbnail { +// url = "" +// } + + description = Translations.About.embedDesc.translate() + + field { + name = Translations.About.howSupportTitle.translate() + value = Translations.About.howSupportValue.translate() + } + + field { + name = Translations.About.version.translate() + // TODO Install Blossom and do the thing for versions + value = "" + } + + field { + name = Translations.About.usefulLinksName.translate() + value = Translations.About.usefulLinksValue.translate() + } + } + + actionRow { + // TODO All of this lmao + linkButton("") { + label = Translations.About.inviteButton.translate() + } + + linkButton("") { + label = Translations.About.privacyButton.translate() + } + + linkButton("") { + label = Translations.About.tosButton.translate() + } + } } } } + + i18n { + interactionUserLocaleResolver() + interactionGuildLocaleResolver() + + applicationCommandLocale(SupportedLocales.ENGLISH) + } + +// TODO Install Doc gen +// docGenerator { +// +// } } bot.start() diff --git a/src/main/kotlin/io/github/nocomment1105/modmailbot/_Constants.kt b/src/main/kotlin/io/github/nocomment1105/modmailbot/_Constants.kt index 7c05aea..70f8cfe 100644 --- a/src/main/kotlin/io/github/nocomment1105/modmailbot/_Constants.kt +++ b/src/main/kotlin/io/github/nocomment1105/modmailbot/_Constants.kt @@ -9,16 +9,11 @@ package io.github.nocomment1105.modmailbot -import dev.kord.common.entity.Snowflake +import dev.kordex.core.utils.env +import dev.kordex.core.utils.envOrNull /** The token of the bot. */ -val BOT_TOKEN: String = config.getProperty("bot_token") - -/** The ID of the mail server. */ -val MAIL_SERVER = Snowflake(config.getProperty("mail_server_id")) - -/** The ID of the main server. */ -val MAIN_SERVER = Snowflake(config.getProperty("main_server_id")) +val BOT_TOKEN: String = env("BOT_TOKEN") /** The URI to connect to the database. */ -val MONGO_URI = config.getProperty("mongo_uri") ?: "mongodb://localhost:27017" +val MONGO_URI = envOrNull("MONGO_URI") ?: "mongodb://localhost:27017" diff --git a/src/main/kotlin/io/github/nocomment1105/modmailbot/_Utils.kt b/src/main/kotlin/io/github/nocomment1105/modmailbot/_Utils.kt index 9afa403..0f7d354 100644 --- a/src/main/kotlin/io/github/nocomment1105/modmailbot/_Utils.kt +++ b/src/main/kotlin/io/github/nocomment1105/modmailbot/_Utils.kt @@ -25,6 +25,7 @@ import io.github.nocomment1105.modmailbot.database.collections.MetaCollection import io.github.nocomment1105.modmailbot.database.collections.OpenThreadsCollection import kotlinx.coroutines.runBlocking import kotlinx.datetime.Clock +import modmailbot.i18n.Translations import org.koin.dsl.bind /** @@ -72,14 +73,14 @@ suspend fun EmbedBuilder.messageEmbed( name = author.tag icon = author.avatar?.cdnUrl?.toUrl() } else { - name = author.asMember(MAIL_SERVER).getTopRole()!!.name + name = author.asMemberOrNull(guildId)?.getTopRole()?.name } } description = message timestamp = Clock.System.now() color = embedColor ?: DISCORD_RED footer { - text = author.asMember(guildId).getTopRole()!!.name + text = author.asMemberOrNull(guildId)?.getTopRole()?.name ?: "" } } @@ -101,11 +102,11 @@ fun EmbedBuilder.editedMessageEmbed( embedColor: Color? = null ) { field { - name = "Previous content" + name = Translations.Utils.EditedMessage.previous.translate() value = oldContent } field { - name = "New content" + name = Translations.Utils.EditedMessage.new.translate() value = newContent } timestamp = Clock.System.now() diff --git a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/CloseCommands.kt b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/CloseCommands.kt index 6b6d496..d597f7b 100644 --- a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/CloseCommands.kt +++ b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/CloseCommands.kt @@ -13,6 +13,7 @@ import dev.kord.core.behavior.channel.createMessage import dev.kord.core.entity.channel.DmChannel import dev.kord.core.entity.channel.MessageChannel import dev.kord.rest.builder.message.embed +import dev.kordex.core.checks.anyGuild import dev.kordex.core.commands.Arguments import dev.kordex.core.commands.converters.impl.coalescingOptionalDuration import dev.kordex.core.commands.converters.impl.defaultingBoolean @@ -21,7 +22,6 @@ import dev.kordex.core.extensions.Extension import dev.kordex.core.extensions.ephemeralSlashCommand import dev.kordex.core.utils.scheduling.Scheduler import dev.kordex.core.utils.scheduling.Task -import io.github.nocomment1105.modmailbot.MAIL_SERVER import io.github.nocomment1105.modmailbot.database.collections.CloseQueueCollection import io.github.nocomment1105.modmailbot.database.collections.OpenThreadsCollection import io.github.nocomment1105.modmailbot.database.collections.SentMessagesCollection @@ -48,7 +48,10 @@ class CloseCommands : Extension() { name = Translations.Commands.Close.name description = Translations.Commands.Close.description - guild(MAIL_SERVER) + check { + // Command is not to be run in DM with bot + anyGuild() + } action { val userToDm = inThreadChannel() ?: return@action diff --git a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/ReplyCommands.kt b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/ReplyCommands.kt index 7ea6352..7e1160e 100644 --- a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/ReplyCommands.kt +++ b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/commands/ReplyCommands.kt @@ -12,11 +12,11 @@ package io.github.nocomment1105.modmailbot.extensions.commands import dev.kord.core.behavior.channel.createMessage import dev.kord.rest.builder.message.embed import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.checks.anyGuild import dev.kordex.core.commands.Arguments import dev.kordex.core.commands.converters.impl.string import dev.kordex.core.extensions.Extension import dev.kordex.core.extensions.ephemeralSlashCommand -import io.github.nocomment1105.modmailbot.MAIL_SERVER import io.github.nocomment1105.modmailbot.database.collections.SentMessagesCollection import io.github.nocomment1105.modmailbot.database.entities.SentMessageData import io.github.nocomment1105.modmailbot.inThreadChannel @@ -31,7 +31,10 @@ class ReplyCommands : Extension() { name = Translations.Commands.Reply.Reply.name description = Translations.Commands.Reply.Reply.description - guild(MAIL_SERVER) + check { + // This is for the mailed to reply with not the mailer + anyGuild() + } action { val userToDm = inThreadChannel() ?: return@action @@ -69,7 +72,10 @@ class ReplyCommands : Extension() { name = Translations.Commands.Reply.Anonreply.name description = Translations.Commands.Reply.Anonreply.name - guild(MAIL_SERVER) + check { + // This is for the mailed to reply with not the mailer + anyGuild() + } action { val userToDm = inThreadChannel() ?: return@action diff --git a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageEditing.kt b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageEditing.kt index 8b3f36a..c0cb3f9 100644 --- a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageEditing.kt +++ b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageEditing.kt @@ -9,6 +9,7 @@ package io.github.nocomment1105.modmailbot.extensions.events +import dev.kord.common.entity.Snowflake import dev.kord.core.behavior.edit import dev.kord.core.behavior.getChannelOf import dev.kord.core.entity.channel.GuildMessageChannel @@ -27,7 +28,6 @@ import dev.kordex.core.extensions.event import dev.kordex.core.i18n.toKey import dev.kordex.core.i18n.types.Key import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI -import io.github.nocomment1105.modmailbot.MAIL_SERVER import io.github.nocomment1105.modmailbot.database.collections.OpenThreadsCollection import io.github.nocomment1105.modmailbot.database.collections.SentMessagesCollection import io.github.nocomment1105.modmailbot.editedMessageEmbed @@ -51,7 +51,8 @@ class MessageEditing : Extension() { val threadMessageIdToEdit = SentMessagesCollection().getInternalMessageById(userThread!!.threadId, event.messageId)!! val threadMessageToEdit = - kord.getGuildOrNull(MAIL_SERVER)!!.getChannelOf(userThread.threadId) + // TODO Add a config system to enable the mail server gotten + kord.getGuildOrNull(Snowflake(""))!!.getChannelOf(userThread.threadId) .getMessage(threadMessageIdToEdit) threadMessageToEdit.edit { @@ -112,7 +113,8 @@ class MessageEditing : Extension() { messageEmbed( modal?.newContents.toString(), user.asUser(), - MAIL_SERVER, + // TODO Add a config system to enable the mail server gotten + Snowflake(""), DISCORD_GREEN, originalSentMessage?.isAnonymous == true ) diff --git a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageReceiving.kt b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageReceiving.kt index 8c6d82d..faa4eb5 100644 --- a/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageReceiving.kt +++ b/src/main/kotlin/io/github/nocomment1105/modmailbot/extensions/events/MessageReceiving.kt @@ -9,6 +9,7 @@ package io.github.nocomment1105.modmailbot.extensions.events +import dev.kord.common.entity.Snowflake import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.createTextChannel import dev.kord.core.behavior.getChannelOf @@ -24,8 +25,6 @@ import dev.kordex.core.extensions.event import dev.kordex.core.time.TimestampType import dev.kordex.core.time.toDiscord import dev.kordex.core.utils.createdAt -import io.github.nocomment1105.modmailbot.MAIL_SERVER -import io.github.nocomment1105.modmailbot.MAIN_SERVER import io.github.nocomment1105.modmailbot.database.collections.OpenThreadsCollection import io.github.nocomment1105.modmailbot.database.collections.SentMessagesCollection import io.github.nocomment1105.modmailbot.database.entities.OpenThreadData @@ -56,7 +55,8 @@ class MessageReceiving : Extension() { if (!openThread) { // Get the mail channel - mailChannel = kord.getGuildOrNull(MAIL_SERVER)!!.createTextChannel(event.message.author!!.tag) + // TODO CONFIG + mailChannel = kord.getGuildOrNull(Snowflake(""))!!.createTextChannel(event.message.author!!.tag) // Store the users thread in the database OpenThreadsCollection().add( @@ -78,13 +78,15 @@ class MessageReceiving : Extension() { field { name = translations.nickname.translate() - value = event.message.author!!.asMember(MAIN_SERVER).nickname + // TODO CONFIG + value = event.message.author!!.asMember(Snowflake("")).nickname ?: Translations.Utils.none.translate() inline = true } field { - val roles = event.message.author!!.asMember(MAIN_SERVER).roles.toList().map { it } + // TODO CONFIG + val roles = event.message.author!!.asMember(Snowflake("")).roles.toList().map { it } name = translations.roles.translate() value = if (roles.isEmpty()) { Translations.Utils.none.translate() @@ -130,7 +132,8 @@ class MessageReceiving : Extension() { event.message.addReaction(Emojis.whiteCheckMark) } else { // Get the mail server from the config file - mailChannel = kord.getGuildOrNull(MAIL_SERVER)!!.getChannelOf( + // TODO MORE CONFIG SYSTEM LMFAO + mailChannel = kord.getGuildOrNull(Snowflake(""))!!.getChannelOf( OpenThreadsCollection().getOpenThreadsForUser(event.message.author!!.id)!!.threadId ) diff --git a/src/main/resources/translations/modmailbot/strings.properties b/src/main/resources/translations/modmailbot/strings.properties index 3614958..f3796c4 100644 --- a/src/main/resources/translations/modmailbot/strings.properties +++ b/src/main/resources/translations/modmailbot/strings.properties @@ -1,3 +1,14 @@ +about.embedTitle=Info about ModMailBot. +about.embedDesc=ModMailBot is a FOSS ModMail Bot for Discord, created by the HyacinthBots organization. Designed to be easy to use, complete with all the features you find in the bigger bots, but safe from a paywall. +about.howSupportTitle=How can I support the continued development of ModMailBot +about.howSupportValue=ModMailBot is developed primarily by NoComment#6411 in their free time. Hyacinth doesn't have the resources to invest in dedicated hosting, so financial donations via [Buy Me a Coffee](https://buymeacoffee.com/Hyacinthbots) help keep Lily afloat. Currently, we run lily on a Hetzner cloud server, which we can afford in our current situation. We also have domain costs for our website.\n\nContributions of code & documentation are also incredibly appreciated! +about.version=Version +about.usefulLinksName=Useful links +about.usefulLinksValue==Website: https://hyacinthbots.org\nGitHub: https://github.com/HyacinthBots\nBuy Me a Coffee: https://buymeacoffee.com/HyacinthBots\nTwitter: https://twitter.com/HyacinthBots\nEmail: `hyacinthbots@outlook.com`\nDiscord: https://discord.gg/hy2329f +about.inviteButton=Invite link +about.privacyButton=Privacy Policy +about.tosButton=Terms of Service + commands.close.name=close commands.close.description=Close this thread commands.close.args.delay.name=delay