Skip to content

Commit

Permalink
言語系の追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars committed Dec 1, 2024
1 parent 8900398 commit 0896816
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 53 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {

implementation("org.yaml:snakeyaml:2.3")
implementation("net.kyori:adventure-text-serializer-gson:4.17.0")
implementation("net.rk4z.s1:swiftbase-core:2.0.7")
implementation("net.rk4z.s1:swiftbase-core:2.0.8")
implementation("net.rk4z.s1:swiftbase-fabric:2.0.1")

includeInJar("net.dv8tion:JDA:5.2.1") {
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/net/rk4z/fabricord/Fabricord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class Fabricord : DedicatedServerModEntry(

var webHookId: String? = null
//endregion

fun get(): Fabricord? {
return get<Fabricord>()
}
}

val name: String = description.name
Expand Down Expand Up @@ -120,7 +124,7 @@ class Fabricord : DedicatedServerModEntry(
val player = handler.player

if (!DiscordBotManager.botIsInitialized) {
player.networkHandler.disconnect(Text.of(LMB.getSysMessage(System.Log.STILLSTARTINGUP)))
player.networkHandler.disconnect(Text.of(LMB.getSysMessage(System.Log.STILL_STARTING_UP)))
return@Join
}

Expand All @@ -142,7 +146,7 @@ class Fabricord : DedicatedServerModEntry(
DiscordBotManager.init(server)
DiscordBotManager.startBot()
} catch (e: Exception) {
logger.error(LMB.getSysMessage(System.Log.FAILEDSTART, e))
logger.error(LMB.getSysMessage(System.Log.FAILED_TO_START, e))
server.stop(false)
}
}
Expand All @@ -152,7 +156,7 @@ class Fabricord : DedicatedServerModEntry(
try {
DiscordBotManager.stopBot()
} catch (e: Exception) {
logger.error(LMB.getSysMessage(System.Log.FAILEDSTOP, e))
logger.error(LMB.getSysMessage(System.Log.FAILED_TO_STOP, e))
}
}
}
Expand Down
81 changes: 41 additions & 40 deletions src/main/kotlin/net/rk4z/fabricord/discord/DiscordBotManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import net.dv8tion.jda.api.requests.GatewayIntent
import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerPlayerEntity
import net.rk4z.fabricord.Fabricord
import net.rk4z.fabricord.utils.System
import net.rk4z.s1.swiftbase.core.CB
import net.rk4z.s1.swiftbase.core.LMB
import net.rk4z.s1.swiftbase.core.Logger
import java.awt.Color
import java.util.*
Expand Down Expand Up @@ -114,7 +116,7 @@ object DiscordBotManager : ListenerAdapter() {

private val discordListener = object : ListenerAdapter() {
override fun onMessageReceived(event: MessageReceivedEvent) {
val server = server ?: return Logger.error("MinecraftServer is not initialized. Cannot process Discord message.")
val server = server ?: return Logger.error(LMB.getSysMessage(System.Log.NOT_INITIALIZED))

CB.executor.executeAsync {
val mentionedPlayers = findMentionedPlayers(event.message.contentRaw, server.playerManager.playerList)
Expand All @@ -128,9 +130,19 @@ object DiscordBotManager : ListenerAdapter() {
}

private fun handlePlayerListCommand(event: SlashCommandInteractionEvent) {
var lang: String = event.guildLocale.locale
if (lang == "unknown") {
// Default fallback
lang = "en"
}
Fabricord.get()?.availableLang?.let {
if (lang !in it) {
lang = "en"
}
}
val server = server ?: run {
Logger.error("MinecraftServer is not initialized. Cannot process /playerlist command.")
event.reply("Sorry, I can't get the player list right now.")
Logger.error(LMB.getSysMessage(System.Log.NOT_INITIALIZED))
event.reply(LMB.getSysMessageByLangCode(System.Command.Online_Players.CANT_GET_PLAYER_LIST, lang))
.setEphemeral(true)
.queue {
CB.executor.executeAsyncLater({
Expand All @@ -143,16 +155,17 @@ object DiscordBotManager : ListenerAdapter() {
val onlinePlayers = server.playerManager.playerList
val playerCount = onlinePlayers.size

// Discord API doesn't provide user language.
val embedBuilder = EmbedBuilder()
.setTitle("Online Players")
.setTitle(LMB.getSysMessageByLangCode(System.Command.Online_Players.TITLE, lang))
.setColor(Color.GREEN)
.setDescription("There are currently $playerCount players online.\n")
.setDescription(LMB.getSysMessageByLangCode(System.Command.Online_Players.DESCRIPTION, lang, playerCount))

if (playerCount > 0) {
val playerList = onlinePlayers.joinToString(separator = "\n") { player -> player.name.string }
embedBuilder.setDescription(embedBuilder.descriptionBuilder.append(playerList).toString())
} else {
embedBuilder.setDescription("There are currently no players online.")
embedBuilder.setDescription(LMB.getSysMessageByLangCode(System.Command.Online_Players.NO_PLAYER, lang))
}

event.replyEmbeds(embedBuilder.build()).queue()
Expand All @@ -178,45 +191,33 @@ object DiscordBotManager : ListenerAdapter() {

fun sendToDiscord(message: String) {
CB.executor.executeAsync {
Fabricord.logChannelID?.let { logChannelID ->
val messageAction = jda?.getTextChannelById(logChannelID)?.sendMessage(message)
if (Fabricord.allowMention == true) {
val allowedMentions = mutableSetOf<Message.MentionType>()

if (Fabricord.allowEveryone == true) {
allowedMentions.add(Message.MentionType.EVERYONE)
}

if (Fabricord.allowHere == true) {
allowedMentions.add(Message.MentionType.HERE)
}

if (Fabricord.allowRoleMention == true) {
allowedMentions.add(Message.MentionType.ROLE)

if (!Fabricord.allowedRoles.isNullOrEmpty()) {
messageAction?.mentionRoles(Fabricord.allowedRoles!!)
} else {
messageAction?.mentionRoles(emptyList()) // 空リストを指定して全ロール許可
try {
val logChannelID = Fabricord.logChannelID ?: return@executeAsync
val channel = jda?.getTextChannelById(logChannelID) ?: return@executeAsync

val messageAction = channel.sendMessage(message)

val allowedMentions = mutableSetOf<Message.MentionType>().apply {
if (Fabricord.allowMention == true) {
if (Fabricord.allowEveryone == true) add(Message.MentionType.EVERYONE)
if (Fabricord.allowHere == true) add(Message.MentionType.HERE)
if (Fabricord.allowRoleMention == true) {
add(Message.MentionType.ROLE)
Fabricord.allowedRoles?.let { messageAction.mentionRoles(it) } ?: messageAction.mentionRoles(emptyList())
}
}

if (Fabricord.allowUserMention == true) {
allowedMentions.add(Message.MentionType.USER)

if (!Fabricord.allowedUsers.isNullOrEmpty()) {
messageAction?.mentionUsers(Fabricord.allowedUsers!!)
} else {
messageAction?.mentionUsers(emptyList()) // 空リストを指定して全ユーザー許可
if (Fabricord.allowUserMention == true) {
add(Message.MentionType.USER)
Fabricord.allowedUsers?.let { messageAction.mentionUsers(it) } ?: messageAction.mentionUsers(emptyList())
}
}

messageAction?.setAllowedMentions(allowedMentions)
} else {
messageAction?.setAllowedMentions(emptySet())
}
messageAction?.queue()

messageAction.setAllowedMentions(allowedMentions)
messageAction.queue()
} catch (e: Exception) {
e.printStackTrace()
}
}
}

}
16 changes: 13 additions & 3 deletions src/main/kotlin/net/rk4z/fabricord/utils/Translations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ open class System : FabricMessageKey {
object ENABLING : Log()
object DISABLING : Log()
object INITIALIZED : Log()
object NOT_INITIALIZED : Log()

open class MissingRequiredParam : Log() {
object ITEM_0 : MissingRequiredParam()
object ITEM_1 : MissingRequiredParam()
object ITEM_2 : MissingRequiredParam()
}

object STILLSTARTINGUP : Log()
object FAILEDSTART : Log()
object FAILEDSTOP : Log()
object STILL_STARTING_UP : Log()
object FAILED_TO_START : Log()
object FAILED_TO_STOP : Log()

object CHECKING_UPDATE : Log()
object ALL_VERSION_COUNT : Log()
Expand All @@ -35,4 +36,13 @@ open class System : FabricMessageKey {
object ERROR : Other()
}
}

open class Command : System() {
open class Online_Players : Command() {
object TITLE : Online_Players()
object DESCRIPTION : Online_Players()
object NO_PLAYER : Online_Players()
object CANT_GET_PLAYER_LIST : Online_Players()
}
}
}
8 changes: 3 additions & 5 deletions src/main/resources/assets/fabricord/lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ system:
enabling: "Enabling %s v%s..."
disabling: "Disabling %s v%s..."
initialized: "%s initialized successfully."
not_initialized: "MinecraftServer is not initialized. Cannot process /playerlist command."
missingrequiredparams:
- "Bot token or log channel ID is missing in config file."
- "Maybe you are running the mod for the first time?"
- "Please check the config file at %s"
stillstartingup: "Server is still starting up, please try again later."
failedstart: "Failed to start the bot: %s"
failedstop: "Failed to stop the bot: %s"
failed_to_start: "Failed to start the bot: %s"
failed_to_stop: "Failed to stop the bot: %s"
checking_update: "Checking for updates..."
all_version_count: "There are %s available versions."
new_version_count: "There are %s newer versions than the current one."
Expand All @@ -26,6 +27,3 @@ system:
unknown: "Unknown"
unknown_error: "An unknown error has occurred."
error: "Error"


main:
18 changes: 17 additions & 1 deletion src/main/resources/assets/fabricord/lang/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ system:
loading: "%s v%s をロード中..."
enabling: "%s v%s を起動中..."
disabling: "%s v%s を停止中..."
initialized: "%s が正常に初期化されました。"
not_initialized: "MinecraftServer が初期化されていないため、/playerlist コマンドを処理できません。"
missingrequiredparams:
- "設定ファイルにボットトークンまたはログチャンネルIDがありません。"
- "MODを初めて起動しましたか?"
- "設定ファイルを確認してください:%s"
stillstartingup: "サーバーがまだ起動中です。後でもう一度お試しください。"
failed_to_start: "ボットの起動に失敗しました:%s"
failed_to_stop: "ボットの停止に失敗しました:%s"
checking_update: "アップデートを確認中..."
all_version_count: "利用可能なバージョンが%s個あります。"
new_version_count: "現在のバージョンより新しいバージョンが%s個あります。"
Expand All @@ -17,4 +26,11 @@ system:
other:
unknown: "不明"
unknown_error: "不明なエラーが発生しました。"
error: "エラー"
error: "エラー"

command:
online_players:
title: "オンラインのプレイヤー"
description: "現在 %s 人がオンラインです。"
no_player: "オンラインのプレイヤーはいません。"
cant_get_player_list: "プレイヤーリストを取得できませんでした。"

0 comments on commit 0896816

Please sign in to comment.