add remove command

This commit is contained in:
dalbodeule 2024-06-12 22:59:27 +09:00
parent 2bef5be6fa
commit 02d12e74b5
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
2 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package space.mori.chzzk_bot.discord.commands
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.Commands
import net.dv8tion.jda.api.interactions.commands.build.OptionData
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.discord.Command
import space.mori.chzzk_bot.discord.CommandInterface
import space.mori.chzzk_bot.services.CommandService
import space.mori.chzzk_bot.services.UserService
@Command
object RemoveCommand : CommandInterface {
private val logger = LoggerFactory.getLogger(this::class.java)
override val name: String = "remove"
override val command = Commands.slash(name, "명령어를 삭제합니다.")
.addOptions(OptionData(OptionType.STRING, "label", "삭제할 명령어를 입력하세요.", true))
override fun run(event: SlashCommandInteractionEvent, bot: JDA) {
val label = event.getOption("label")?.asString
if(label == null) {
event.hook.sendMessage("명령어는 필수 입력입니다.").queue()
return
}
val user = UserService.getUser(event.user.idLong)
if(user == null) {
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
return
}
try {
CommandService.removeCommand(user, label)
event.hook.sendMessage("삭제가 완료되었습니다. $label").queue()
} catch (e: Exception) {
event.hook.sendMessage("에러가 발생했습니다.").queue()
logger.debug(e.stackTraceToString())
}
}
}

View File

@ -1,6 +1,7 @@
package space.mori.chzzk_bot.services package space.mori.chzzk_bot.services
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import space.mori.chzzk_bot.models.Command import space.mori.chzzk_bot.models.Command
import space.mori.chzzk_bot.models.Commands import space.mori.chzzk_bot.models.Commands
@ -17,6 +18,17 @@ object CommandService {
} }
} }
fun removeCommand(user: User, command: String): Command? {
return transaction {
val commandRow = Command.find(Commands.user eq user.id and(Commands.command eq command)).firstOrNull()
commandRow ?: throw RuntimeException("Command not found! $command")
commandRow.delete()
return@transaction commandRow
}
}
fun getCommand(id: Int): Command? { fun getCommand(id: Int): Command? {
return transaction { return transaction {
return@transaction Command.findById(id) return@transaction Command.findById(id)