Skip to content

Commit

Permalink
Begin adjusting to be a generalized multiserver bot. Add info command…
Browse files Browse the repository at this point in the history
…s and stuff
  • Loading branch information
NoComment1105 committed Jan 14, 2025
1 parent efbb758 commit 68a4b36
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 0 additions & 11 deletions config.example.properties

This file was deleted.

95 changes: 74 additions & 21 deletions src/main/kotlin/io/github/nocomment1105/modmailbot/ModMailBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
13 changes: 4 additions & 9 deletions src/main/kotlin/io/github/nocomment1105/modmailbot/_Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 5 additions & 4 deletions src/main/kotlin/io/github/nocomment1105/modmailbot/_Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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 ?: ""
}
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -51,7 +51,8 @@ class MessageEditing : Extension() {
val threadMessageIdToEdit =
SentMessagesCollection().getInternalMessageById(userThread!!.threadId, event.messageId)!!
val threadMessageToEdit =
kord.getGuildOrNull(MAIL_SERVER)!!.getChannelOf<GuildMessageChannel>(userThread.threadId)
// TODO Add a config system to enable the mail server gotten
kord.getGuildOrNull(Snowflake(""))!!.getChannelOf<GuildMessageChannel>(userThread.threadId)
.getMessage(threadMessageIdToEdit)

threadMessageToEdit.edit {
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand Down Expand Up @@ -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
)

Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/translations/modmailbot/strings.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 68a4b36

Please sign in to comment.