mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
add update command.
This commit is contained in:
parent
02d12e74b5
commit
2d28bf8bcb
@ -6,6 +6,8 @@ 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.chzzk.ChzzkHandler
|
||||
import space.mori.chzzk_bot.chzzk.Connector
|
||||
import space.mori.chzzk_bot.discord.Command
|
||||
import space.mori.chzzk_bot.discord.CommandInterface
|
||||
import space.mori.chzzk_bot.services.CommandService
|
||||
@ -33,9 +35,13 @@ object AddCommand : CommandInterface {
|
||||
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
|
||||
return
|
||||
}
|
||||
val chzzkChannel = Connector.getChannel(user.token)
|
||||
|
||||
try {
|
||||
CommandService.saveCommand(user, label, content)
|
||||
try {
|
||||
ChzzkHandler.reloadCommand(chzzkChannel!!)
|
||||
} catch (_: Exception) {}
|
||||
event.hook.sendMessage("등록이 완료되었습니다. $label = $content").queue()
|
||||
} catch (e: Exception) {
|
||||
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||
|
@ -6,6 +6,8 @@ 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.chzzk.ChzzkHandler
|
||||
import space.mori.chzzk_bot.chzzk.Connector
|
||||
import space.mori.chzzk_bot.discord.Command
|
||||
import space.mori.chzzk_bot.discord.CommandInterface
|
||||
import space.mori.chzzk_bot.services.CommandService
|
||||
@ -31,9 +33,13 @@ object RemoveCommand : CommandInterface {
|
||||
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
|
||||
return
|
||||
}
|
||||
val chzzkChannel = Connector.getChannel(user.token)
|
||||
|
||||
try {
|
||||
CommandService.removeCommand(user, label)
|
||||
try {
|
||||
ChzzkHandler.reloadCommand(chzzkChannel!!)
|
||||
} catch (_: Exception) {}
|
||||
event.hook.sendMessage("삭제가 완료되었습니다. $label").queue()
|
||||
} catch (e: Exception) {
|
||||
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||
|
@ -0,0 +1,52 @@
|
||||
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.chzzk.ChzzkHandler
|
||||
import space.mori.chzzk_bot.chzzk.Connector
|
||||
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
|
||||
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
|
||||
|
||||
@Command
|
||||
object UpdateCommand : CommandInterface {
|
||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||
override val name: String = "update"
|
||||
override val command = Commands.slash(name, "명령어를 수정합니다.")
|
||||
.addOptions(OptionData(OptionType.STRING, "label", "수정할 명령어를 입력하세요.", true))
|
||||
.addOptions(OptionData(OptionType.STRING, "content", "표시될 텍스트를 입력하세요.", true))
|
||||
|
||||
override fun run(event: SlashCommandInteractionEvent, bot: JDA) {
|
||||
val label = event.getOption("label")?.asString
|
||||
val content = event.getOption("content")?.asString
|
||||
|
||||
if(label == null || content == null) {
|
||||
event.hook.sendMessage("명령어와 텍스트는 필수 입력입니다.").queue()
|
||||
return
|
||||
}
|
||||
|
||||
val user = UserService.getUser(event.user.idLong)
|
||||
if(user == null) {
|
||||
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
|
||||
return
|
||||
}
|
||||
val chzzkChannel = Connector.getChannel(user.token)
|
||||
|
||||
try {
|
||||
CommandService.updateCommand(user, label, content)
|
||||
try {
|
||||
ChzzkHandler.reloadCommand(chzzkChannel!!)
|
||||
} catch (_: Exception) {}
|
||||
event.hook.sendMessage("등록이 완료되었습니다. $label = $content").queue()
|
||||
} catch (e: Exception) {
|
||||
event.hook.sendMessage("에러가 발생했습니다.").queue()
|
||||
logger.debug(e.stackTraceToString())
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package space.mori.chzzk_bot.services
|
||||
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.update
|
||||
import space.mori.chzzk_bot.models.Command
|
||||
import space.mori.chzzk_bot.models.Commands
|
||||
import space.mori.chzzk_bot.models.User
|
||||
@ -29,6 +30,18 @@ object CommandService {
|
||||
}
|
||||
}
|
||||
|
||||
fun updateCommand(user: User, command: String, content: String): Command {
|
||||
return transaction {
|
||||
val updated = Commands.update({Commands.user eq user.id and(Commands.command eq command)}) {
|
||||
it[Commands.content] = content
|
||||
}
|
||||
|
||||
if(updated == 0) throw RuntimeException("Command not found! $command")
|
||||
|
||||
return@transaction Command.find(Commands.user eq user.id and(Commands.command eq command)).first()
|
||||
}
|
||||
}
|
||||
|
||||
fun getCommand(id: Int): Command? {
|
||||
return transaction {
|
||||
return@transaction Command.findById(id)
|
||||
|
@ -3,7 +3,7 @@
|
||||
<!-- 콘솔에 출력하는 기본 로그 설정 -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>INFO</level>
|
||||
<level>DEBUG</level>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
|
Loading…
x
Reference in New Issue
Block a user