Skip to content

Commit

Permalink
feat: 登録削除コマンド "vcspeaker remove" の追加 (#173)
Browse files Browse the repository at this point in the history
* feat: 登録削除コマンド "vcspeaker remove" の追加

* docs: ドキュメントの更新

* feat: 削除前の確認処理と、エイリアスなどの設定削除を追加

* Apply suggestions from code review

Co-authored-by: yuuaHP <identity@yuua.dev>

---------

Co-authored-by: yuuaHP <identity@yuua.dev>
  • Loading branch information
book000 and yuuahp authored Jul 7, 2024
1 parent 95cbbba commit fb499c3
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
18 changes: 17 additions & 1 deletion docs/for-users/commands/vcspeaker.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ VCSpeaker を再起動します。

### settings

VCSpeaker の設定を行います。この設定は、サーバごとに保存されます。
VCSpeaker の設定を行います。この設定は、サーバごとに保存されます。
VCSpeaker を使いはじめる場合、この設定操作を行うことによって登録されます。

```text
/vcspeaker settings [channel] [prefix] [speaker] [emotion] [emotion-level] [pitch] [speed] [volume] [auto-join]
Expand All @@ -31,6 +32,21 @@ VCSpeaker の設定を行います。この設定は、サーバごとに保存
- `[volume]`: 音量 (50% から 200%)
- `[auto-join]`: ボイスチャンネルに自動で入退室するかどうか

### remove

VCSpeaker の登録を削除します。
コマンド実行後、「はい」を選択することで削除されます。

```text
/vcspeaker remove
```

登録を削除すると、サーバに関連する以下の情報も削除されます。

- エイリアス
- 無視ルール
- タイトル

## 注意事項

- `restart` サブコマンドを使用すると、Bot は即座に再起動されます。これにより、一時的に Bot が使用不可能になることがあります。
Expand Down
109 changes: 109 additions & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/commands/VCSpeakerCommand.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.jaoafa.vcspeaker.commands

import com.jaoafa.vcspeaker.stores.AliasStore
import com.jaoafa.vcspeaker.stores.GuildStore
import com.jaoafa.vcspeaker.stores.IgnoreStore
import com.jaoafa.vcspeaker.stores.TitleStore
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.asChannelOf
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.authorOf
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.errorColor
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.infoColor
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.respond
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.respondEmbed
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.successColor
import com.jaoafa.vcspeaker.tools.discord.DiscordExtensions.warningColor
import com.jaoafa.vcspeaker.tools.discord.DiscordLoggingExtension.log
import com.jaoafa.vcspeaker.tools.discord.Options
import com.jaoafa.vcspeaker.tools.discord.SlashCommandExtensions.publicSlashCommand
Expand All @@ -19,9 +25,13 @@ import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalBoolea
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChannel
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalInt
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString
import com.kotlindiscord.kord.extensions.components.components
import com.kotlindiscord.kord.extensions.components.publicButton
import com.kotlindiscord.kord.extensions.extensions.Extension
import dev.kord.common.entity.ButtonStyle
import dev.kord.common.entity.ChannelType
import dev.kord.core.entity.channel.TextChannel
import dev.kord.rest.builder.message.embed
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlin.system.exitProcess

Expand Down Expand Up @@ -211,6 +221,105 @@ class VCSpeakerCommand : Extension() {
}
}
}

publicSubCommand("remove", "VCSpeaker の登録を削除します。") {
action {
val guildId = guild!!.id
val guildData = GuildStore[guildId]

if (guildData == null) {
respond("**:x: このサーバーは登録されていません。**")
return@action
}

val confirmUser = user

// 削除する前に確認を設ける
respond {
embed {
title = ":wastebasket: Confirm registration removal"
description = "VCSpeaker の登録を削除しますが、よろしいですか?"
authorOf(user)
warningColor()
}

components {
publicButton {
label = "はい"
style = ButtonStyle.Primary
deferredAck = true
action buttonAction@{
// 異なるユーザーが操作できないようにする
if (user.id != confirmUser.id) {
respondEmbed(
":x: Failed to remove registration",
"この操作は実行者のみが実行できます。"
) {
authorOf(user)

errorColor()
}

return@buttonAction
}

// 各種データを削除
AliasStore.removeForGuild(guildId)
IgnoreStore.removeForGuild(guildId)
TitleStore.removeForGuild(guildId)
GuildStore.remove(guildData)
edit {
embed {
title = ":wastebasket: Registration removed"
description = "VCSpeaker の登録を削除しました。"

authorOf(user)
successColor()
}

components {}
}

log(logger) { guild, user ->
"[${guild.name}] Registration Removed: Removed by @${user.username}"
}
}
}

publicButton {
label = "いいえ"
style = ButtonStyle.Danger
action buttonAction@{
if (user.id != confirmUser.id) {
respondEmbed(
":x: Failed to remove registration",
"この操作は実行者のみが実行できます。"
) {
authorOf(user)

errorColor()
}

return@buttonAction
}

edit {
embed {
title = ":wastebasket: Registration removal canceled"
description = "VCSpeaker の登録削除をキャンセルしました。"

authorOf(user)
infoColor()
}

components {}
}
}
}
}
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/stores/AliasStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ object AliasStore : StoreStruct<AliasData>(
data.find { it.guildId == guildId && it.search == from }

fun filter(guildId: Snowflake?) = data.filter { it.guildId == guildId }

fun removeForGuild(guildId: Snowflake) {
data.removeIf { it.guildId == guildId }
write()
}
}

5 changes: 5 additions & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/stores/IgnoreStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ object IgnoreStore : StoreStruct<IgnoreData>(
fun find(guildId: Snowflake, text: String) = data.find { it.guildId == guildId && it.search == text }

fun filter(guildId: Snowflake?) = data.filter { it.guildId == guildId }

fun removeForGuild(guildId: Snowflake) {
data.removeIf { it.guildId == guildId }
write()
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/com/jaoafa/vcspeaker/stores/TitleStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.jaoafa.vcspeaker.stores
import com.jaoafa.vcspeaker.VCSpeaker
import dev.kord.common.entity.Snowflake
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

@Serializable
Expand All @@ -23,4 +22,9 @@ object TitleStore : StoreStruct<TitleData>(
fun find(channelId: Snowflake) = data.find { it.channelId == channelId }

fun filterGuild(guildId: Snowflake) = data.filter { it.guildId == guildId }

fun removeForGuild(guildId: Snowflake) {
data.removeIf { it.guildId == guildId }
write()
}
}

0 comments on commit fb499c3

Please sign in to comment.