Skip to content

Commit

Permalink
feat: 유저 조회 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosine-A committed Nov 16, 2024
1 parent 2d3074a commit f3065c9
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 7 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "kr.cosine.discordauth"
version = "1.1.0"
version = "1.2.0"

repositories {
maven("https://maven.hqservice.kr/repository/maven-public")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class AuthAdminCommand(
private val authorizedPlayerConfig: AuthorizedPlayerConfig,
private val authService: AuthService
) {

@CommandExecutor("제거", "해당 유저의 인증을 제거합니다.", priority = 1)
suspend fun removeAuth(
sender: CommandSender,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kr.cosine.discordauth.command

import kr.cosine.discordauth.command.argument.DiscordIdArgument
import kr.cosine.discordauth.command.argument.UniqueIdOrNameArgument
import kr.cosine.discordauth.service.AuthService
import kr.hqservice.framework.command.ArgumentLabel
import kr.hqservice.framework.command.Command
import kr.hqservice.framework.command.CommandExecutor
import org.bukkit.command.CommandSender

@Command(parent = AuthAdminCommand::class, label = "유저조회")
class AutoFindUserAdminCommand(
private val authService: AuthService
) {
@CommandExecutor("마크", "마크 UUID/닉네임을 기반으로 디코 닉네임/아이디를 조회합니다.", priority = 1)
fun showDiscordNameAndId(
sender: CommandSender,
@ArgumentLabel("마크 UUID/닉네임") uniqueIdOrNameArgument: UniqueIdOrNameArgument
) {
authService.showDiscordNicknameAndIdByMinecraftUniqueIdOrName(sender, uniqueIdOrNameArgument.uniqueIdOrName)
}

@CommandExecutor("디코", "디코 아이디를 기반으로 마크 닉네임/UUID를 조회합니다.", priority = 2)
fun showMinecraftNameAndUniqueId(
sender: CommandSender,
@ArgumentLabel("디코 아이디") discordIdArgument: DiscordIdArgument
) {
authService.showMinecraftNameAndUniqueIdByDiscordId(sender, discordIdArgument.discordId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kr.cosine.discordauth.command.argument

data class DiscordIdArgument(
val discordId: Long
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kr.cosine.discordauth.command.argument

data class UniqueIdOrNameArgument(
val uniqueIdOrName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kr.cosine.discordauth.command.provider

import kr.cosine.discordauth.command.argument.DiscordIdArgument
import kr.cosine.discordauth.registry.AuthorizedPlayerRegistry
import kr.hqservice.framework.command.CommandArgumentProvider
import kr.hqservice.framework.command.CommandContext
import kr.hqservice.framework.command.argument.exception.ArgumentFeedback
import kr.hqservice.framework.global.core.component.Component
import org.bukkit.Location

@Component
class DiscordIdArgumentProvider(
private val authorizedPlayerRegistry: AuthorizedPlayerRegistry
) : CommandArgumentProvider<DiscordIdArgument> {
override suspend fun cast(context: CommandContext, argument: String?): DiscordIdArgument {
if (argument == null) {
throw ArgumentFeedback.Message("§c디스코드 아이디를 입력해주세요.")
}
val discordId = argument.toLongOrNull() ?: throw ArgumentFeedback.Message("§c숫자만 입력할 수 있습니다.")
return DiscordIdArgument(discordId)
}

override suspend fun getTabComplete(context: CommandContext, location: Location?): List<String> {
return authorizedPlayerRegistry.getDiscordIds().map(Long::toString)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.bukkit.Server
class NameArgumentProvider(
private val server: Server
) : CommandArgumentProvider<NameArgument> {

override suspend fun cast(context: CommandContext, argument: String?): NameArgument {
if (argument == null) {
throw ArgumentFeedback.Message("§c닉네임을 입력해주세요.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kr.cosine.discordauth.command.provider

import kr.cosine.discordauth.command.argument.NameArgument
import kr.cosine.discordauth.command.argument.UniqueIdOrNameArgument
import kr.hqservice.framework.command.CommandArgumentProvider
import kr.hqservice.framework.command.CommandContext
import kr.hqservice.framework.command.argument.exception.ArgumentFeedback
import kr.hqservice.framework.global.core.component.Component
import org.bukkit.Location
import org.bukkit.Server

@Component
class UniqueIdOrNameArgumentProvider(
private val server: Server
) : CommandArgumentProvider<UniqueIdOrNameArgument> {
override suspend fun cast(context: CommandContext, argument: String?): UniqueIdOrNameArgument {
if (argument == null) {
throw ArgumentFeedback.Message("§cUUID 또는 닉네임을 입력해주세요.")
}
return UniqueIdOrNameArgument(argument)
}

override suspend fun getTabComplete(context: CommandContext, location: Location?): List<String> {
val onlinePlayers = server.onlinePlayers
return onlinePlayers.map { it.name } + onlinePlayers.map { "${it.uniqueId}" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class AuthorizedPlayerConfig(
plugin: Plugin,
private val authorizedPlayerRegistry: AuthorizedPlayerRegistry
) {

private val gson = Gson()

private val file = File(plugin.dataFolder, "authorized-player.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ import net.md_5.bungee.api.chat.TextComponent

fun textComponentListOf(vararg messages: String): List<TextComponent> {
return messages.map(::TextComponent)
}

fun textComponentArrayOf(vararg messages: String): Array<TextComponent> {
return arrayOf(*messages.map(::TextComponent).toTypedArray())
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ class AuthorizedPlayerRegistry {
authorizedPlayerMap.remove(uniqueId)
isChanged = true
}

fun getDiscordIds(): List<Long> {
return authorizedPlayerMap.values.toList()
}
}
59 changes: 59 additions & 0 deletions src/main/kotlin/kr/cosine/discordauth/service/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.withContext
import kr.cosine.discordapi.service.BotService
import kr.cosine.discordauth.enums.AuthType
import kr.cosine.discordauth.enums.Message
import kr.cosine.discordauth.extension.textComponentArrayOf
import kr.cosine.discordauth.registry.AuthCodeRegistry
import kr.cosine.discordauth.registry.AuthorizedPlayerRegistry
import kr.cosine.discordauth.registry.SettingRegistry
Expand All @@ -17,6 +18,10 @@ import kr.hqservice.framework.bukkit.core.coroutine.extension.BukkitMain
import kr.hqservice.framework.global.core.component.Service
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder
import net.md_5.bungee.api.chat.ClickEvent
import net.md_5.bungee.api.chat.HoverEvent
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import java.time.LocalDateTime
import java.util.UUID
Expand Down Expand Up @@ -178,4 +183,58 @@ class AuthService(
return@withContext true
}
}

fun showDiscordNicknameAndIdByMinecraftUniqueIdOrName(sender: CommandSender, uniqueIdOrName: String) {
plugin.launch(Dispatchers.IO) {
val target = runCatching {
val targetUniqueId = UUID.fromString(uniqueIdOrName)
server.getOfflinePlayer(targetUniqueId)
}.getOrNull() ?: server.getOfflinePlayer(uniqueIdOrName)
val targetUniqueId = target.uniqueId
val targetDiscordId = authorizedPlayerRegistry.findDiscordId(targetUniqueId) ?: run {
sender.sendMessage("§c인증되지 않은 유저입니다.")
return@launch
}
val targetMember = botService.findMemberById(targetDiscordId) ?: run {
sender.sendMessage("§c디스코드 방에 없는 유저입니다.")
return@launch
}
sender.sendMessage("§6${target.name}님의 디스코드 정보")
sender.spigot().sendMessage(createClipboardTextComponent("UUID", "§7(${targetUniqueId})", "$targetUniqueId"))
sender.sendInfoMessage("문자 아이디", targetMember.user.name)
sender.sendInfoMessage("숫자 아이디", "${targetMember.idLong}")
sender.sendInfoMessage("원본 닉네임", targetMember.user.effectiveName)
sender.sendInfoMessage("변경된 닉네임", targetMember.effectiveName)
}
}

fun showMinecraftNameAndUniqueIdByDiscordId(sender: CommandSender, discordId: Long) {
plugin.launch(Dispatchers.IO) {
val targetUniqueId = authorizedPlayerRegistry.findUniqueIdByDiscordId(discordId) ?: run {
sender.sendMessage("§c인증하지 않은 유저입니다.")
return@launch
}
val target = server.getOfflinePlayer(targetUniqueId)
val targetMember = botService.findMemberById(discordId)
sender.sendMessage("§6${targetMember?.user?.name ?: "§c불러오지 못함"}§6님의 마인크래프트 정보")
sender.spigot().sendMessage(createClipboardTextComponent("디스코드 아이디", "§7(${discordId})", "$discordId"))
sender.sendInfoMessage("마크 닉네임", "${target.name}")
sender.sendInfoMessage("마크 UUID", "$targetUniqueId")
}
}

private fun CommandSender.sendInfoMessage(title: String, text: String) {
val textComponent = TextComponent().apply {
addExtra(TextComponent("§f└ $title: "))
addExtra(createClipboardTextComponent(title, text))
}
spigot().sendMessage(textComponent)
}

private fun createClipboardTextComponent(title: String, text: String, copy: String = text): TextComponent {
return TextComponent("§a$text").apply {
hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, textComponentArrayOf("§f클릭 시 ${title}을(를) 복사합니다."))
clickEvent = ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, copy)
}
}
}
2 changes: 0 additions & 2 deletions src/main/kotlin/kr/cosine/discordauth/util/TimeUtils.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kr.cosine.discordauth.util

import java.time.Duration
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

object TimeUtils {

private val fullFormatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분 ss초")
private val shortFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: HQDiscordAuth
main: kr.cosine.discordauth.HQDiscordAuth
version: 1.1.0
version: 1.2.0
api-version: 1.17
author: Cosine_A
depend:
Expand Down

0 comments on commit f3065c9

Please sign in to comment.