lilJudd/src/main/kotlin/net/moonleay/liljudd/extensions/FeatureManageExtension.kt
moonleay c65e4031d9
chore: update copyright
Signed-off-by: moonleay <contact@moonleay.net>
2024-02-13 19:24:48 +01:00

136 lines
5.5 KiB
Kotlin

/*
* lilJudd
* Copyright (C) 2024 moonleay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.moonleay.liljudd.extensions
import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.enumChoice
import com.kotlindiscord.kord.extensions.commands.converters.impl.channel
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand
import com.kotlindiscord.kord.extensions.utils.hasPermission
import dev.kord.common.entity.Permission
import dev.kord.rest.builder.message.embed
import net.moonleay.liljudd.extensions.component.EnableOrDisable
import net.moonleay.liljudd.features.component.FeatureEnum
import net.moonleay.liljudd.features.component.FeatureManager
import net.moonleay.liljudd.util.EmbedColor
import net.moonleay.liljudd.util.Logger
import net.moonleay.liljudd.util.MessageUtil
class FeatureManageExtension : Extension() {
override val name = "feature"
override val allowApplicationCommandInDMs: Boolean
get() = false
override suspend fun setup() {
publicSlashCommand(::FeatureManagerArgs) {
name = "feature"
description = "Manage features"
this.action {
val u = this.user
if (!u.asMember(this.guild!!.id).hasPermission(Permission.Administrator)) {
this.respond {
this.embed {
this.color = EmbedColor.ERROR.color
this.title = "401: Not Authorized"
this.description =
"You cannot edit features, as you don't have the Administrator permission."
this.footer {
this.icon = u.asUser().avatar?.cdnUrl?.toUrl()
this.text = MessageUtil.getFooter(u)
}
}
}
return@action
}
val gID = this.guild!!.id.value.toLong()
val cID = this.arguments.channel.id.value.toLong()
val channel = this.arguments.channel
val args = this.arguments
Logger.out("${args.feature.readableName} ${args.setStatus.readableName} ${channel.data.name.value}")
val f = FeatureManager.getFeature(args.feature)
if (f == null) {
this.respond {
this.embed {
this.color = EmbedColor.ERROR.color
this.title = "404: Not Found"
this.description = "The feature you are trying to edit does not exist."
this.footer {
this.icon = u.asUser().avatar?.cdnUrl?.toUrl()
this.text = MessageUtil.getFooter(u)
}
}
}
return@action
}
if (this.arguments.setStatus == EnableOrDisable.ENABLE) {
val enabled = f.enable(u, gID, cID, channel, args)
this.respond {
this.embed {
this.color = enabled.color
this.title = enabled.title
this.description = enabled.description
this.footer {
this.icon = u.asUser().avatar?.cdnUrl?.toUrl()
this.text = MessageUtil.getFooter(u)
}
}
}
return@action
}
val disabled = f.disable(u, gID, cID, channel, args)
this.respond {
this.embed {
this.color = disabled.color
this.title = disabled.title
this.description = disabled.description
this.footer {
this.icon = u.asUser().avatar?.cdnUrl?.toUrl()
this.text = MessageUtil.getFooter(u)
}
}
}
}
}
}
inner class FeatureManagerArgs : Arguments() {
val feature by enumChoice<FeatureEnum> {
this.name = "feature"
this.description = "The targeted feature"
this.typeName = "en_US"
}
val setStatus by enumChoice<EnableOrDisable> {
this.name = "set"
this.description = "Set enabled or disabled"
this.typeName = "en_US"
}
val channel by channel {
this.name = "channel"
this.description = "Target Channel"
}
}
}