Skip to content

Commit

Permalink
Reduce code duplication and rename some packages
Browse files Browse the repository at this point in the history
  • Loading branch information
NoComment1105 committed Aug 29, 2024
1 parent 3081b45 commit 1af9cbc
Show file tree
Hide file tree
Showing 25 changed files with 343 additions and 394 deletions.
24 changes: 12 additions & 12 deletions src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ import org.hyacinthbots.lilybot.extensions.moderation.events.ModerationEvents
import org.hyacinthbots.lilybot.extensions.threads.AutoThreading
import org.hyacinthbots.lilybot.extensions.threads.ModThreadInviting
import org.hyacinthbots.lilybot.extensions.threads.ThreadControl
import org.hyacinthbots.lilybot.extensions.utils.commands.GalleryChannel
import org.hyacinthbots.lilybot.extensions.utils.commands.Github
import org.hyacinthbots.lilybot.extensions.utils.commands.GuildAnnouncements
import org.hyacinthbots.lilybot.extensions.utils.commands.InfoCommands
import org.hyacinthbots.lilybot.extensions.utils.commands.NewsChannelPublishing
import org.hyacinthbots.lilybot.extensions.utils.commands.PublicUtilities
import org.hyacinthbots.lilybot.extensions.utils.commands.Reminders
import org.hyacinthbots.lilybot.extensions.utils.commands.RoleMenu
import org.hyacinthbots.lilybot.extensions.utils.commands.StartupHooks
import org.hyacinthbots.lilybot.extensions.utils.commands.StatusPing
import org.hyacinthbots.lilybot.extensions.utils.commands.Tags
import org.hyacinthbots.lilybot.extensions.utils.events.UtilityEvents
import org.hyacinthbots.lilybot.extensions.utility.commands.GalleryChannel
import org.hyacinthbots.lilybot.extensions.utility.commands.Github
import org.hyacinthbots.lilybot.extensions.utility.commands.GuildAnnouncements
import org.hyacinthbots.lilybot.extensions.utility.commands.InfoCommands
import org.hyacinthbots.lilybot.extensions.utility.commands.NewsChannelPublishing
import org.hyacinthbots.lilybot.extensions.utility.commands.PublicUtilities
import org.hyacinthbots.lilybot.extensions.utility.commands.Reminders
import org.hyacinthbots.lilybot.extensions.utility.commands.RoleMenu
import org.hyacinthbots.lilybot.extensions.utility.commands.StartupHooks
import org.hyacinthbots.lilybot.extensions.utility.commands.StatusPing
import org.hyacinthbots.lilybot.extensions.utility.commands.Tags
import org.hyacinthbots.lilybot.extensions.utility.events.UtilityEvents
import org.hyacinthbots.lilybot.internal.BuildInfo
import org.hyacinthbots.lilybot.utils.BOT_TOKEN
import org.hyacinthbots.lilybot.utils.ENVIRONMENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dev.kordex.core.koin.KordExKoinComponent
import org.hyacinthbots.lilybot.database.Database
import org.hyacinthbots.lilybot.database.entities.ActionData
import org.hyacinthbots.lilybot.database.entities.ModerationActionData
import org.hyacinthbots.lilybot.extensions.moderation.ModerationAction
import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction
import org.koin.core.component.inject
import org.litote.kmongo.eq

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dev.kord.common.entity.Snowflake
import kotlinx.datetime.DateTimePeriod
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import org.hyacinthbots.lilybot.extensions.moderation.ModerationAction
import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction

/**
* The data for Moderation action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.hyacinthbots.lilybot.extensions.config.commands.configClearCommand
import org.hyacinthbots.lilybot.extensions.config.commands.configViewCommand
import org.hyacinthbots.lilybot.extensions.logging.config.loggingCommand
import org.hyacinthbots.lilybot.extensions.moderation.config.moderationCommand
import org.hyacinthbots.lilybot.extensions.utils.config.utilityCommand
import org.hyacinthbots.lilybot.extensions.utility.config.utilityCommand

class ConfigExtension : Extension() {
override val name: String = "config"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package org.hyacinthbots.lilybot.extensions.config.utils

import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.behavior.UserBehavior
import dev.kord.rest.builder.message.EmbedBuilder
import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection
import org.hyacinthbots.lilybot.extensions.logging.config.LoggingArgs
import org.hyacinthbots.lilybot.extensions.moderation.config.ModerationArgs
import org.hyacinthbots.lilybot.extensions.utility.config.UtilityArgs
import org.hyacinthbots.lilybot.utils.interval
import org.hyacinthbots.lilybot.utils.trimmedContents

suspend fun EmbedBuilder.utilityEmbed(arguments: UtilityArgs, user: UserBehavior) {
title = "Configuration: Utility"
field {
name = "Utility Log"
value = if (arguments.utilityLogChannel != null) {
"${arguments.utilityLogChannel!!.mention} ${arguments.utilityLogChannel!!.data.name.value}"
} else {
"Disabled"
}
}
field {
name = "Log Channel updates"
value = if (arguments.logChannelUpdates) "Yes" else "No"
}
field {
name = "Log Event updates"
value = if (arguments.logEventUpdates) "Yes" else "No"
}
field {
name = "Log Invite updates"
value = if (arguments.logInviteUpdates) "Yes" else "No"
}
field {
name = "Log Role updates"
value = if (arguments.logRoleUpdates) "Yes" else "No"
}

footer {
text = "Configured by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}

suspend fun EmbedBuilder.moderationEmbed(arguments: ModerationArgs, user: UserBehavior) {
title = "Configuration: Moderation"
field {
name = "Moderators"
value = arguments.moderatorRole?.mention ?: "Disabled"
}
field {
name = "Action log"
value = arguments.modActionLog?.mention ?: "Disabled"
}
field {
name = "Log publicly"
value = when (arguments.logPublicly) {
true -> "Enabled"
false -> "Disabled"
null -> "Disabled"
}
}
field {
name = "Quick timeout length"
value = arguments.quickTimeoutLength.interval() ?: "No quick timeout length set"
}
field {
name = "Warning Auto-punishments"
value = when (arguments.warnAutoPunishments) {
true -> "Enabled"
false -> "Disabled"
null -> "Disabled"
}
}
field {
name = "Ban DM Message"
value = arguments.banDmMessage ?: "No custom Ban DM message set"
}
field {
name = "Auto-invite Moderator Role"
value = when (arguments.autoInviteModeratorRole) {
true -> "Enabled"
false -> "Disabled"
null -> "Disabled"
}
}
footer {
text = "Configured by ${user.asUserOrNull()?.username}"
}
}

suspend fun EmbedBuilder.loggingEmbed(arguments: LoggingArgs, guild: GuildBehavior?, user: UserBehavior) {
title = "Configuration: Logging"
field {
name = "Message Delete Logs"
value = if (arguments.enableMessageDeleteLogs && arguments.messageLogs != null) {
arguments.messageLogs!!.mention
} else {
"Disabled"
}
}
field {
name = "Message Edit Logs"
value = if (arguments.enableMessageEditLogs && arguments.messageLogs != null) {
arguments.messageLogs!!.mention
} else {
"Disabled"
}
}
field {
name = "Member Logs"
value = if (arguments.enableMemberLogging && arguments.memberLog != null) {
arguments.memberLog!!.mention
} else {
"Disabled"
}
}

field {
name = "Public Member logs"
value = if (arguments.enablePublicMemberLogging && arguments.publicMemberLog != null) {
arguments.publicMemberLog!!.mention
} else {
"Disabled"
}
}
if (arguments.enableMemberLogging && arguments.publicMemberLog != null) {
val config = LoggingConfigCollection().getConfig(guild!!.id)
if (config != null) {
field {
name = "Join Message"
value = config.publicMemberLogData?.joinMessage.trimmedContents(256)!!
}
field {
name = "Leave Message"
value = config.publicMemberLogData?.leaveMessage.trimmedContents(256)!!
}
field {
name = "Ping on join"
value = config.publicMemberLogData?.pingNewUsers.toString()
}
}
}

footer {
text = "Configured by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.behavior.getChannelOfOrNull
import dev.kord.core.behavior.interaction.modal
import dev.kord.core.entity.channel.TextChannel
import dev.kord.rest.builder.message.EmbedBuilder
import dev.kord.rest.builder.message.embed
import dev.kordex.core.checks.anyGuild
import dev.kordex.core.checks.hasPermission
Expand All @@ -18,8 +17,8 @@ import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection
import org.hyacinthbots.lilybot.database.entities.LoggingConfigData
import org.hyacinthbots.lilybot.database.entities.PublicMemberLogData
import org.hyacinthbots.lilybot.extensions.config.ConfigOptions
import org.hyacinthbots.lilybot.extensions.config.utils.loggingEmbed
import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms
import org.hyacinthbots.lilybot.utils.trimmedContents

@OptIn(UnsafeAPI::class)
suspend fun SlashCommand<*, *, *>.loggingCommand() = unsafeSubCommand(::LoggingArgs) {
Expand Down Expand Up @@ -109,65 +108,6 @@ suspend fun SlashCommand<*, *, *>.loggingCommand() = unsafeSubCommand(::LoggingA
}
}

suspend fun EmbedBuilder.loggingEmbed() {
title = "Configuration: Logging"
field {
name = "Message Delete Logs"
value = if (arguments.enableMessageDeleteLogs && arguments.messageLogs != null) {
arguments.messageLogs!!.mention
} else {
"Disabled"
}
}
field {
name = "Message Edit Logs"
value = if (arguments.enableMessageEditLogs && arguments.messageLogs != null) {
arguments.messageLogs!!.mention
} else {
"Disabled"
}
}
field {
name = "Member Logs"
value = if (arguments.enableMemberLogging && arguments.memberLog != null) {
arguments.memberLog!!.mention
} else {
"Disabled"
}
}

field {
name = "Public Member logs"
value = if (arguments.enablePublicMemberLogging && arguments.publicMemberLog != null) {
arguments.publicMemberLog!!.mention
} else {
"Disabled"
}
}
if (arguments.enableMemberLogging && arguments.publicMemberLog != null) {
val config = LoggingConfigCollection().getConfig(guild!!.id)
if (config != null) {
field {
name = "Join Message"
value = config.publicMemberLogData?.joinMessage.trimmedContents(256)!!
}
field {
name = "Leave Message"
value = config.publicMemberLogData?.leaveMessage.trimmedContents(256)!!
}
field {
name = "Ping on join"
value = config.publicMemberLogData?.pingNewUsers.toString()
}
}
}

footer {
text = "Configured by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}

var publicMemberLogData: PublicMemberLogData? = null
if (arguments.enablePublicMemberLogging) {
val modalObj = PublicLoggingModal()
Expand Down Expand Up @@ -209,9 +149,7 @@ suspend fun SlashCommand<*, *, *>.loggingCommand() = unsafeSubCommand(::LoggingA
if (!arguments.enablePublicMemberLogging) {
ackEphemeral()
}
respondEphemeral {
embed { loggingEmbed() }
}
respondEphemeral { embed { loggingEmbed(arguments, guild, user) } }

val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild!!)

Expand All @@ -222,10 +160,6 @@ suspend fun SlashCommand<*, *, *>.loggingCommand() = unsafeSubCommand(::LoggingA
return@action
}

utilityLog.createMessage {
embed {
loggingEmbed()
}
}
utilityLog.createMessage { embed { loggingEmbed(arguments, guild, user) } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlinx.datetime.Clock
import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection
import org.hyacinthbots.lilybot.database.collections.ModerationActionCollection
import org.hyacinthbots.lilybot.extensions.config.ConfigOptions
import org.hyacinthbots.lilybot.extensions.moderation.ModerationAction
import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction
import org.hyacinthbots.lilybot.utils.baseModerationEmbed
import org.hyacinthbots.lilybot.utils.dmNotificationStatusEmbedField
import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import org.hyacinthbots.lilybot.database.entities.ActionData
import org.hyacinthbots.lilybot.database.entities.TemporaryBanData
import org.hyacinthbots.lilybot.database.entities.TimeData
import org.hyacinthbots.lilybot.extensions.config.ConfigOptions
import org.hyacinthbots.lilybot.extensions.moderation.ModerationAction
import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction
import org.hyacinthbots.lilybot.utils.HYACINTH_GITHUB
import org.hyacinthbots.lilybot.utils.baseModerationEmbed
import org.hyacinthbots.lilybot.utils.dmNotificationStatusEmbedField
Expand Down
Loading

0 comments on commit 1af9cbc

Please sign in to comment.