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

add smart vc notify #33

Merged
merged 6 commits into from
Apr 13, 2024
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.flywaydb:flyway-core")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shiron.kotodiscord.service.command
package dev.shiron.kotodiscord.command

import dev.shiron.kotodiscord.i18n.I18n
import dev.shiron.kotodiscord.util.data.action.BotSlashCommandData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shiron.kotodiscord.service.command
package dev.shiron.kotodiscord.command

import dev.shiron.kotodiscord.i18n.I18n
import dev.shiron.kotodiscord.util.data.action.BotSlashCommandData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shiron.kotodiscord.service.command
package dev.shiron.kotodiscord.command

import dev.shiron.kotodiscord.i18n.I18n
import dev.shiron.kotodiscord.util.data.action.BotSlashCommandData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shiron.kotodiscord.service.command.bump
package dev.shiron.kotodiscord.command.bump

import dev.shiron.kotodiscord.bot.KotoMain
import dev.shiron.kotodiscord.domain.BumpConfigData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shiron.kotodiscord.service.command.bump
package dev.shiron.kotodiscord.command.bump

import dev.shiron.kotodiscord.domain.BumpJobQueueData
import dev.shiron.kotodiscord.repository.BumpConfigDataRepository
Expand Down
249 changes: 249 additions & 0 deletions src/main/kotlin/dev/shiron/kotodiscord/command/vc/notify/VCCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package dev.shiron.kotodiscord.command.vc.notify

import dev.shiron.kotodiscord.domain.VCNotificationData
import dev.shiron.kotodiscord.i18n.I18n
import dev.shiron.kotodiscord.util.data.action.*
import dev.shiron.kotodiscord.util.meta.SingleCommandEnum
import dev.shiron.kotodiscord.util.service.SingleCommandServiceClass
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.channel.ChannelType
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion
import net.dv8tion.jda.api.interactions.components.ActionComponent
import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class VCCommand
@Autowired
constructor(
private val vcService: VCService,
private val i18n: I18n,
) : SingleCommandServiceClass(
SingleCommandEnum.VC_NOTIFICATION,
i18n,
) {
fun genBtnSet(shared: Boolean): Button {
return Button.secondary(
genComponentId("set", shared, ComponentSendType.EDIT),
i18n.format("button.vc_notification.set"),
)
}

fun genBtnSetAll(shared: Boolean): Button {
return Button.secondary(
genComponentId("set_all", shared, ComponentSendType.EDIT),
i18n.format("button.vc_notification.set_all"),
)
}

fun genBtnRemove(shared: Boolean): Button {
return Button.secondary(
genComponentId("remove", shared, ComponentSendType.EDIT),
i18n.format("button.vc_notification.remove"),
)
}

fun genBtnSetSmart(shared: Boolean): Button {
return Button.secondary(
genComponentId("set_smart", shared, ComponentSendType.EDIT),
i18n.format("button.vc_notification.set_smart"),
)
}

override fun onSlashCommand(cmd: BotSlashCommandData) {
val dataList = vcService.listVCNotification(cmd.guild.idLong)

var msg = i18n.format("command.message.vc_notification.main") + "\n\n"
val components = mutableListOf<ActionComponent>()

if (dataList.isEmpty()) {
msg += i18n.format("command.message.vc_notification.empty")
components.add(genBtnSetAll(cmd.shared))
components.add(genBtnSet(cmd.shared))
} else {
msg += i18n.format("command.message.vc_notification.exist") + "\n"

if (dataList.size < 10) {
if (dataList.find { it.vcCategoryId == null && it.vcChannelId == null && it.textChannelId == cmd.event.channel.idLong } == null) {
components.add(genBtnSetAll(cmd.shared))
}

components.add(genBtnSet(cmd.shared))
} else {
msg += i18n.format("command.message.vc_notification.set.limit")
}

msg +=
dataList.joinToString("\n") {
"- " + getConfString(it)
}

components.add(genBtnSetSmart(cmd.shared))
components.add(genBtnRemove(cmd.shared))
}

cmd.send(msg, components.ifEmpty { null })
}

override fun onButton(event: BotButtonData) {
when (event.actionData.key) {
"set" -> {
event.send(
i18n.format("command.message.vc_notification.set.vc"),
listOf(
EntitySelectMenu.create(
genComponentId("vc", event.actionData.isShow, ComponentSendType.EDIT),
EntitySelectMenu.SelectTarget.CHANNEL,
).build(),
),
)
}
"set_all" -> {
setVCNotification(event.guild, null, event.event.channel, event)
}
"set_smart" -> {
val dataList = vcService.listVCNotification(event.guild.idLong)

event.send(
i18n.format("command.message.vc_notification.set_smart"),
listOf(buildOptionSelection("set_smart", dataList, event.guild, event.actionData.isShow)),
)
}
"remove" -> {
val dataList = vcService.listVCNotification(event.guild.idLong)

event.send(
i18n.format("command.message.vc_notification.remove"),
listOf(buildOptionSelection("rm", dataList, event.guild, event.actionData.isShow)),
)
}
}
}

override fun onEntitySelect(event: BotEntitySelectData) {
when (event.actionData.key) {
"vc" -> {
val vcChannelId = event.event.values.first().idLong
setVCNotification(event.guild, vcChannelId, event.event.channel, event)
}
}
}

override fun onStringSelect(event: BotStringSelectData) {
when (event.actionData.key) {
"rm" -> {
val index = event.values.first().toIntOrNull()?.minus(1) ?: return
val data = vcService.listVCNotification(event.guild.idLong)[index]
vcService.removeVCNotification(data)
event.send(
i18n.format(
"command.message.vc_notification.remove.success",
(index + 1).toString(),
data.vcName,
"<#${data.textChannelId}>",
),
)
}
"set_smart" -> {
val index = event.values.first().toIntOrNull()?.minus(1) ?: return
val data = vcService.listVCNotification(event.guild.idLong)[index]
vcService.setVCNotification(data.copy(isSmart = !data.isSmart))
event.send(
i18n.format(
"command.message.vc_notification.set_smart.success",
(index + 1).toString(),
data.vcName,
"<#${data.textChannelId}>",
if (!data.isSmart) {
i18n.format("command.message.vc_notification.set_smart.true")
} else {
i18n.format("command.message.vc_notification.set_smart.false")
},
),
)
}
}
}

private fun setVCNotification(
guild: Guild,
vcChannelId: Long?,
textChannel: MessageChannelUnion,
event: BotSendEventClass,
) {
val vcChannel = vcChannelId?.let { guild.getVoiceChannelById(it) }
val categoryChannel = vcChannelId?.let { guild.getCategoryById(it) }

if (vcChannelId != null && (vcChannel == null && categoryChannel == null)) {
event.send(i18n.format("command.message.vc_notification.set.error.vc"))
return
}
if (textChannel.type != ChannelType.TEXT) {
event.send(i18n.format("command.message.vc_notification.set.error.text"))
return
}

vcService.setVCNotification(
VCNotificationData(
guildId = guild.idLong,
vcCategoryId = categoryChannel?.idLong,
vcChannelId = vcChannel?.idLong,
textChannelId = textChannel.idLong,
),
)
event.send(
i18n.format(
"command.message.vc_notification.set.success",
vcChannel?.asMention ?: categoryChannel?.asMention ?: "サーバー全体",
textChannel.asMention,
),
)
}

private fun buildOptionSelection(
key: String,
dataList: List<VCNotificationData>,
guild: Guild,
isShow: Boolean,
): StringSelectMenu {
return StringSelectMenu.create(genComponentId(key, isShow, ComponentSendType.EDIT)).apply {
dataList.forEachIndexed { index, vcNotificationData ->
val vcId = vcNotificationData.vcCategoryId ?: vcNotificationData.vcChannelId
val vcName = vcId.let { guild.channels.find { it.idLong == vcId }?.name }?.let { "#$it" } ?: "サーバー全体"
val textName = guild.channels.find { it.idLong == vcNotificationData.textChannelId }?.name?.let { "#$it" } ?: "不明"
addOption(
"${index + 1}" +
i18n.format(
"command.message.vc_notification.conf",
vcName,
textName,
if (vcNotificationData.isSmart) {
i18n.format("command.message.vc_notification.set_smart.true")
} else {
i18n.format("command.message.vc_notification.set_smart.false")
},
),
"${index + 1}",
)
}
}.build()
}

private fun getConfString(
data: VCNotificationData,
): String {
return i18n.format(
"command.message.vc_notification.conf",
data.vcName,
"<#${data.textChannelId}>",
if (data.isSmart) {
i18n.format("command.message.vc_notification.set_smart.true")
} else {
i18n.format("command.message.vc_notification.set_smart.false")
},
)
}
}
Loading
Loading