Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ojt channel #6

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/kotlin/dev/t7e/mechatechkt/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.kord.core.on
import dev.kord.gateway.Intent
import dev.kord.gateway.Intents
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.config.BotConfig
Expand All @@ -27,6 +28,7 @@ suspend fun main(args: Array<String>) {
val commands = mapOf(
"mentoring" to CreatePrivateChannelCommand,
"shinchoku" to ProgressReportCommand,
"create-ojt" to CreateOJTChannelCommand
)

client.createGlobalApplicationCommands {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.t7e.mechatechkt.commands

import dev.kord.core.behavior.createTextChannel
import dev.kord.core.behavior.interaction.respondEphemeral
import dev.kord.core.entity.interaction.ApplicationCommandInteraction
import dev.kord.rest.builder.interaction.GlobalMultiApplicationCommandBuilder
import kotlinx.coroutines.flow.toList

/**
* Created by testusuke on 2023/09/23
* @author testusuke
*/
object CreateOJTChannelCommand : CommandHandler {
override suspend fun handle(interaction: ApplicationCommandInteraction) {
interaction.respondEphemeral { content = "OJTチャンネルを作成しています..." }

val guild = interaction.channel.getGuildOrNull()!!
val channel = interaction.channel.asChannel()
// get category channel what text channel is in
val category = channel.data.parentId!!.let { guild.getChannelOrNull(it.value!!) }


// get all channel
val channels = guild.channels.toList().filter {
val categoryId = it.data.parentId?.value ?: return@filter false
categoryId == category?.id
}

// get all user
val users = guild.members.toList()
.filter {
!it.isBot
}
.filter { member ->
!channels.any { channel ->
val description = channel.data.topic.value ?: return@any false

description.contains(member.id.toString())
}
}

users.forEach { user ->
// create ojt channel
val userName = user.nickname ?: user.username
guild.createTextChannel("ojt-$userName") {
this.parentId = category?.id
this.topic = user.id.toString()
}
}

}

override fun register(builder: GlobalMultiApplicationCommandBuilder) {
builder.input("create-ojt", "OJTチャンネル作成") {
dmPermission = false
}
}
}
Loading