Skip to content

Commit

Permalink
wdadコマンドにテキストコマンドで実行した際の処理を実装
Browse files Browse the repository at this point in the history
テキストコマンドでHelpコマンドを実行するとエラーが発生する問題を修正
  • Loading branch information
kosugikun committed Aug 15, 2023
1 parent bcc1487 commit f3cff49
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/cosgy/textToSpeak/TextToSpeak.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ object TextToSpeak {
.setAlternativePrefix(config.altPrefix)
.setOwnerId(config.ownerId.toString())
.setHelpWord("help")
.useHelpBuilder(false)
.setLinkedCacheSize(200)
.setGuildSettingsManager(settings)
.setListener(CommandAudit())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//////////////////////////////////////////////////////////////////////////////////////////
package dev.cosgy.textToSpeak.commands.dictionary

import com.jagrosh.jdautilities.command.CommandEvent
import com.jagrosh.jdautilities.command.SlashCommand
import com.jagrosh.jdautilities.command.SlashCommandEvent
import com.jagrosh.jdautilities.menu.ButtonMenu
Expand Down Expand Up @@ -48,6 +49,7 @@ class AddWordCmd(private val bot: Bot) : SlashCommand() {
if (isWordExist) {
val no = ""
val ok = ""

ButtonMenu.Builder()
.setText("単語が既に存在します。上書きしますか?")
.addChoices(no, ok)
Expand Down Expand Up @@ -95,6 +97,70 @@ class AddWordCmd(private val bot: Bot) : SlashCommand() {
handleCommand(event, replaceEmoji(word), reading)
}

/***
* テキストコマンド用
*/
private fun handleCommand(event: CommandEvent, word: String, reading: String) {
val guildId = event.guild!!.idLong
val dictionary = bot.dictionary
val isWordExist = dictionary!!.getWords(guildId).containsKey(word)
if (isWordExist) {
val no = ""
val ok = ""

ButtonMenu.Builder()
.setText("単語が既に存在します。上書きしますか?")
.addChoices(no, ok)
.setEventWaiter(bot.waiter)
.setTimeout(30, TimeUnit.SECONDS)
.setAction { re: Emoji ->
if (re.name == ok) {
dictionary.updateDictionary(guildId, word, reading)
sendSuccessMessage(event)
} else {

event.reply("辞書登録をキャンセルしました。")
}
}.setFinalAction { m: Message ->
try {
m.clearReactions().queue()
m.delete().queue()
} catch (ignore: PermissionException) {
}
}.build().display(event.channel)
} else {
dictionary.updateDictionary(guildId, word, reading)
sendSuccessMessage(event)
}
}

private fun sendSuccessMessage(event: CommandEvent) {
val args = event.args.split("\\s+".toRegex(), 2).toTypedArray()
val word = args[0]
val reading = args[1]
val builder = EmbedBuilder()
.setColor(SUCCESS_COLOR)
.setTitle("単語を追加しました。")
.addField("単語", "```${replaceEmoji(word)}```", false)
.addField("読み", "```${reading}```", false)
event.reply(builder.build())
}

override fun execute(event: CommandEvent) {
val args = event.args.split("\\s+".toRegex(), 2).toTypedArray()
if (args.size < 2) {
event.reply("コマンドが無効です。単語と読み方の2つを入力して実行して下さい。")
return
}
val word = args[0]
val reading = args[1]
if (!isKatakana(reading)) {
event.reply("読み方はすべてカタカナで入力して下さい。")
return
}
handleCommand(event, replaceEmoji(word), reading)
}

companion object {
private val SUCCESS_COLOR = Color(0, 163, 129)

Expand Down
33 changes: 16 additions & 17 deletions src/main/java/dev/cosgy/textToSpeak/commands/general/HelpCmd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import com.jagrosh.jdautilities.command.CommandEvent
import com.jagrosh.jdautilities.command.SlashCommand
import com.jagrosh.jdautilities.command.SlashCommandEvent
import dev.cosgy.textToSpeak.Bot
import dev.cosgy.textToSpeak.audio.VoiceCreation
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.channel.ChannelType
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel
import org.slf4j.LoggerFactory
import java.awt.Color
import java.util.*

class HelpCmd(var bot: Bot) : SlashCommand() {
init {
Expand Down Expand Up @@ -61,13 +64,10 @@ class HelpCmd(var bot: Bot) : SlashCommand() {
}

public override fun execute(event: CommandEvent) {
val builder = StringBuilder(
"""
**${event.jda.selfUser.name}** コマンド一覧:
""".trimIndent()
)
val eBuilder = EmbedBuilder()
eBuilder.setTitle("**" + event.jda.selfUser.name + "** コマンド一覧")
eBuilder.setColor(Color(245, 229, 107))
val builder = StringBuilder()
var category: Category? = null
val commands = event.client.commands
for (command in commands) {
Expand All @@ -76,24 +76,23 @@ class HelpCmd(var bot: Bot) : SlashCommand() {
category = command.category
builder.append("\n\n __").append(if (category == null) "カテゴリなし" else category.name).append("__:\n")
}
builder.append("\n`").append(event.client.textualPrefix)
.append(if (event.client.prefix == null) " " else "").append(command.name)
builder.append("\n`").append("/").append(command.name)
.append(if (command.arguments == null) "`" else " " + command.arguments + "`")
.append(" - ").append(command.help)
}
}
if (event.client.serverInvite != null) builder.append("\n\nさらにヘルプが必要な場合は、公式サーバーに参加することもできます: ")
.append(event.client.serverInvite)
eBuilder.setDescription(builder)
if (bot.config.helpToDm) {
event.replyInDm(
builder.toString(),
{ _: Message? -> if (event.isFromType(ChannelType.TEXT)) event.reactSuccess() }) { _: Throwable? ->
event.replyWarning(
"ダイレクトメッセージをブロックしているため、ヘルプを送信できません。"
)
}
event.author.openPrivateChannel()
.flatMap { channel: PrivateChannel -> channel.sendMessageEmbeds(eBuilder.build()) }.queue()
} else {
event.reply(builder.toString())
event.reply(eBuilder.build())
}
}

companion object{
private val logger = LoggerFactory.getLogger(VoiceCreation::class.java)
}
}

0 comments on commit f3cff49

Please sign in to comment.