From aeda839ecf3751a7612fb1e1bb85889f913d7f36 Mon Sep 17 00:00:00 2001 From: testusuke Date: Sun, 1 Oct 2023 16:53:57 +0900 Subject: [PATCH 1/2] feat: command to find ojt channel --- src/main/kotlin/dev/t7e/mechatechkt/Main.kt | 4 +- .../commands/WhereOJTChannelCommand.kt | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt diff --git a/src/main/kotlin/dev/t7e/mechatechkt/Main.kt b/src/main/kotlin/dev/t7e/mechatechkt/Main.kt index a7202a1..7211d62 100644 --- a/src/main/kotlin/dev/t7e/mechatechkt/Main.kt +++ b/src/main/kotlin/dev/t7e/mechatechkt/Main.kt @@ -10,6 +10,7 @@ import dev.kord.gateway.PrivilegedIntent import dev.t7e.mechatechkt.commands.CreateOJTChannelCommand import dev.t7e.mechatechkt.commands.CreatePrivateChannelCommand import dev.t7e.mechatechkt.commands.ProgressReportCommand +import dev.t7e.mechatechkt.commands.WhereOJTChannelCommand import dev.t7e.mechatechkt.config.BotConfig import dev.t7e.mechatechkt.config.BotStatus import dev.t7e.mechatechkt.unit.ProgressReport @@ -28,7 +29,8 @@ suspend fun main(args: Array) { val commands = mapOf( "mentoring" to CreatePrivateChannelCommand, "shinchoku" to ProgressReportCommand, - "create-ojt" to CreateOJTChannelCommand + "create-ojt" to CreateOJTChannelCommand, + "ojt" to WhereOJTChannelCommand ) client.createGlobalApplicationCommands { diff --git a/src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt b/src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt new file mode 100644 index 0000000..b7cc33a --- /dev/null +++ b/src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt @@ -0,0 +1,46 @@ +package dev.t7e.mechatechkt.commands + +import dev.kord.core.behavior.interaction.respondEphemeral +import dev.kord.core.entity.interaction.ApplicationCommandInteraction +import dev.kord.rest.builder.interaction.GlobalMultiApplicationCommandBuilder +import dev.kord.rest.builder.interaction.user +import kotlinx.coroutines.flow.toList + +/** + * Created by testusuke on 2023/10/01 + * @author testusuke + */ +object WhereOJTChannelCommand : CommandHandler { + override suspend fun handle(interaction: ApplicationCommandInteraction) { + val guild = interaction.channel.getGuildOrNull()!! + // get all channel + val channels = guild.channels.toList() + + // arguments + val userId = interaction.getOptionSnowflake("user")!! + val user = guild.getMember(userId) + + // get ojt channel + val channel = channels.find { + val description = it.data.topic.value ?: return@find false + description.contains(userId.toString()) + } + + if (channel == null) { + interaction.respondEphemeral { content = "OJTチャンネルが見つかりませんでした" } + return + } + + interaction.respondEphemeral { content = channel.mention } + } + + override fun register(builder: GlobalMultiApplicationCommandBuilder) { + builder.input("ojt", "OJTチャンネルを検索します") { + dmPermission = false + + user("user", "ユーザー") { + required = true + } + } + } +} \ No newline at end of file From ee74fc3034b250bc0befbd8a4beecc0ec674fc79 Mon Sep 17 00:00:00 2001 From: testusuke Date: Sun, 1 Oct 2023 16:57:36 +0900 Subject: [PATCH 2/2] refactor: remove user val --- .../dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt b/src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt index b7cc33a..661e7f9 100644 --- a/src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt +++ b/src/main/kotlin/dev/t7e/mechatechkt/commands/WhereOJTChannelCommand.kt @@ -18,7 +18,6 @@ object WhereOJTChannelCommand : CommandHandler { // arguments val userId = interaction.getOptionSnowflake("user")!! - val user = guild.getMember(userId) // get ojt channel val channel = channels.find {