Skip to content

Commit

Permalink
Reduce some duplication here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
NoComment1105 committed Aug 29, 2024
1 parent 378f224 commit 1998d2e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This code was utilized from [cozy](https://github.com/QuiltMC/cozy-discord) by QuiltMC
* and hence is subject to the terms of the Mozilla Public License V. 2.0
* A copy of this license can be found at https://mozilla.org/MPL/2.0/.
* and hence is subject to the terms of the Mozilla Public Licence V. 2.0
* A copy of this licence can be found at https://mozilla.org/MPL/2.0/.
*/
package org.hyacinthbots.lilybot.database.entities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.kord.rest.builder.message.embed
import dev.kordex.core.checks.anyGuild
import dev.kordex.core.checks.hasPermission
import dev.kordex.core.commands.Arguments
import dev.kordex.core.commands.application.slash.EphemeralSlashCommandContext
import dev.kordex.core.commands.application.slash.SlashCommand
import dev.kordex.core.commands.application.slash.converters.impl.stringChoice
import dev.kordex.core.commands.application.slash.ephemeralSubCommand
Expand All @@ -28,110 +29,109 @@ suspend fun SlashCommand<*, *, *>.configClearCommand() = ephemeralSubCommand(::C
}

action {
suspend fun logClear() {
val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, this.getGuild()!!)
when (arguments.config) {
ConfigType.MODERATION.name -> clearConfig(ConfigType.MODERATION, arguments)

if (utilityLog == null) {
respond {
content = "Consider setting a utility config to log changes to configurations."
}
return
}
ConfigType.LOGGING.name -> clearConfig(ConfigType.LOGGING, arguments)

utilityLog.createMessage {
embed {
title = "Configuration Cleared: ${arguments.config[0]}${
arguments.config.substring(1, arguments.config.length).lowercase()
}"
footer {
text = "Config cleared by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
}
ConfigType.UTILITY.name -> clearConfig(ConfigType.UTILITY, arguments)

ConfigType.ALL.name -> clearConfig(ConfigType.ALL, arguments)
}

when (arguments.config) {
ConfigType.MODERATION.name -> {
ModerationConfigCollection().getConfig(guild!!.id) ?: run {
respond {
content = "No moderation configuration exists to clear!"
}
return@action
respond {
embed {
title = if (arguments.config == ConfigType.ALL.name) {
"All configs cleared"
} else {
"Config cleared: ${arguments.config}"
}

logClear()

ModerationConfigCollection().clearConfig(guild!!.id)
respond {
embed {
title = "Config cleared: Moderation"
footer {
text = "Config cleared by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
footer {
text = "Config cleared by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
}
}
}

ConfigType.LOGGING.name -> {
LoggingConfigCollection().getConfig(guild!!.id) ?: run {
respond {
content = "No logging configuration exists to clear!"
}
return@action
}

logClear()

LoggingConfigCollection().clearConfig(guild!!.id)
/**
* Handles the clearing of a configuration(s) based on the [args] and [type].
*
* @param type The type of config to clear
* @param args The [ClearArgs] for the clearing of the config
* @author NoComment1105
* @since 5.0.0
*/
private suspend fun EphemeralSlashCommandContext<*, *>.clearConfig(type: ConfigType, args: ClearArgs) {
when (type) {
ConfigType.MODERATION -> {
ModerationConfigCollection().getConfig(guild!!.id) ?: run {
respond {
embed {
title = "Config cleared: Logging"
footer {
text = "Config cleared by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
content = "No moderation configuration exists to clear"
}
return
}
logClear(args)
ModerationConfigCollection().clearConfig(guild!!.id)
}

ConfigType.UTILITY.name -> {
UtilityConfigCollection().getConfig(guild!!.id) ?: run {
respond {
content = "No utility configuration exists to clear"
}
return@action
}

logClear()

UtilityConfigCollection().clearConfig(guild!!.id)
ConfigType.LOGGING -> {
LoggingConfigCollection().getConfig(guild!!.id) ?: run {
respond {
embed {
title = "Config cleared: Utility"
footer {
text = "Config cleared by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
content = "No logging configuration exists to clear"
}
}
logClear(args)
LoggingConfigCollection().clearConfig(guild!!.id)
}

ConfigType.ALL.name -> {
ModerationConfigCollection().clearConfig(guild!!.id)
LoggingConfigCollection().clearConfig(guild!!.id)
UtilityConfigCollection().clearConfig(guild!!.id)
ConfigType.UTILITY -> {
UtilityConfigCollection().getConfig(guild!!.id) ?: run {
respond {
embed {
title = "All configs cleared"
footer {
text = "Configs cleared by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
content = "No utility configuration exists to clear"
}
}
logClear(args)
UtilityConfigCollection().clearConfig(guild!!.id)
}

ConfigType.ALL -> {
ModerationConfigCollection().clearConfig(guild!!.id)
LoggingConfigCollection().clearConfig(guild!!.id)
UtilityConfigCollection().clearConfig(guild!!.id)
}
}
}

/**
* Log the clearing of a configuration to the utility log, should there still be one.
*
* @param arguments The [ClearArgs] for the clearing of the config
* @author NoComment1105
* @since 5.0.0
*/
suspend fun EphemeralSlashCommandContext<*, *>.logClear(arguments: ClearArgs) {
// Skip this if the utility config is cleared or all configs are cleared
if (arguments.config == ConfigType.UTILITY.name || arguments.config == ConfigType.ALL.name) return
val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, this.getGuild()!!)

if (utilityLog == null) {
respond {
content = "Consider setting a utility config to log changes to configurations."
}
return
}

utilityLog.createMessage {
embed {
title = "Configuration Cleared: ${arguments.config[0]}${
arguments.config.substring(1, arguments.config.length).lowercase()
}"
footer {
text = "Config cleared by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DuplicatedCode")

package org.hyacinthbots.lilybot.extensions.config.utils

import dev.kord.core.behavior.GuildBehavior
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ class Github : Extension() {
}
// Clarify the input is formatted correctly, inform the user if not.
if (!repository.contains("/")) {
sentry.breadcrumb(BreadcrumbType.Error) {
category = "extensions.util.Github.issue.InputCheck"
message = "Input missing /"
}
respond {
embed {
title = "Make sure your repository input is formatted like this:"
Expand Down Expand Up @@ -124,11 +120,6 @@ class Github : Extension() {
if (iterator.hasNext()) {
issue = iterator.next()
} else {
sentry.breadcrumb(BreadcrumbType.Error) {
category = "extensions.util.Github.issue.getIssue"
message = "Unable to find issue"
}

respond {
embed {
title = "Invalid issue number. Make sure this issue exists!"
Expand Down Expand Up @@ -167,10 +158,6 @@ class Github : Extension() {
merged = pull.isMerged
draft = pull.isDraft
} catch (ioException: IOException) {
sentry.breadcrumb(BreadcrumbType.Error) {
category = "extensions.util.Github.issue.CheckPRStatus"
message = "Error initializing PR wtf"
}
ioException.printStackTrace()
title = "Error!"
description = "Error occurred initializing Pull Request. How did this happen?"
Expand Down Expand Up @@ -295,12 +282,8 @@ class Github : Extension() {
}
return@action
}
// Clarify the input is formatted correctly, inform the user if not

if (!repository.contains("/")) {
sentry.breadcrumb(BreadcrumbType.Error) {
category = "extensions.util.Github.repository.InputCheck"
message = "Input missing /"
}
respond {
embed {
title = "Make sure your input is formatted like this:"
Expand All @@ -321,10 +304,6 @@ class Github : Extension() {
} else {
github.getRepository(repository)
}
sentry.breadcrumb(BreadcrumbType.Info) {
category = "extensions.util.Github.repository.getRepository"
message = "Repository found"
}
} catch (_: IOException) {
sentry.breadcrumb(BreadcrumbType.Error) {
category = "extensions.util.Github.repository.getRepository"
Expand Down Expand Up @@ -402,15 +381,7 @@ class Github : Extension() {
} else {
github.getUser(arguments.username)
}
sentry.breadcrumb(BreadcrumbType.Info) {
category = "extensions.util.Github.user.getUser"
message = "User found"
}
} catch (_: IOException) {
sentry.breadcrumb(BreadcrumbType.Error) {
category = "extensions.util.Github.user.getUser"
message = "Unable to find user"
}
respond {
embed {
title = "Invalid Username. Make sure this user exists!"
Expand Down
Loading

0 comments on commit 1998d2e

Please sign in to comment.