Skip to content

Commit

Permalink
Start implementing minecraft interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Erdragh committed Nov 30, 2023
1 parent 0dd34de commit f2f62e6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.erdragh.astralbot.commands

import com.mojang.authlib.GameProfile
import dev.erdragh.astralbot.handlers.MinecraftHandler
import dev.erdragh.astralbot.handlers.WhitelistHandler
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent
Expand All @@ -24,11 +26,10 @@ object LinkCommand : HandledSlashCommand {

try {
if (WhitelistHandler.checkWhitelist(minecraftID) != null) {
event.hook.setEphemeral(true)
.sendMessageFormat("Minecraft username %s already linked", "Erdragh").queue()
event.hook.setEphemeral(true).sendMessageFormat("Minecraft username %s already linked", "Erdragh")
.queue()
} else if (WhitelistHandler.checkWhitelist(event.user.idLong) != null) {
event.hook.setEphemeral(true)
.sendMessageFormat("%s already linked", event.member).queue()
event.hook.setEphemeral(true).sendMessageFormat("%s already linked", event.member).queue()
} else {
WhitelistHandler.whitelist(event.user, minecraftID)
event.hook.setEphemeral(true)
Expand Down Expand Up @@ -63,61 +64,50 @@ object LinkCheckCommand : HandledSlashCommand, AutocompleteCommand {

private fun handleMinecraftToDiscord(event: SlashCommandInteractionEvent, minecraftName: String) {
val notFound = "Minecraft username %s is not linked to any Discord User"
val discordID =
WhitelistHandler.checkWhitelist(UUID.fromString("decdaa2b-c56e-49e8-862d-bbdd89a15b0a")) // TODO: Get actual minecraft user from server
val minecraftID = MinecraftHandler.nameToUUID(minecraftName)
val discordID = if (minecraftID != null) WhitelistHandler.checkWhitelist(minecraftID) else null
if (discordID != null) {
val guild = event.guild
if (guild != null) {
var found = false
guild.loadMembers {
if (it.idLong == discordID) {
event.hook.setEphemeral(true)
.sendMessageFormat("Minecraft username %s is linked to %s", minecraftName, it)
.queue()
.sendMessageFormat("Minecraft username %s is linked to %s", minecraftName, it).queue()
found = true
}
}.onError {
event.hook.setEphemeral(true)
.sendMessageFormat("Something went wrong: %s", it.localizedMessage)
event.hook.setEphemeral(true).sendMessageFormat("Something went wrong: %s", it.localizedMessage)
.queue()
}.onSuccess {
if (!found) {
event.hook.setEphemeral(true)
.sendMessageFormat(
notFound,
minecraftName
event.hook.setEphemeral(true).sendMessageFormat(
notFound, minecraftName
).queue()
}
}
} else {
event.hook.setEphemeral(true)
.sendMessage("Something went wrong")
.queue()
event.hook.setEphemeral(true).sendMessage("Something went wrong").queue()
}
} else {
event.hook.setEphemeral(true)
.sendMessageFormat(
notFound,
minecraftName
event.hook.setEphemeral(true).sendMessageFormat(
notFound, minecraftName
).queue()
}
}

private fun handleDiscordToMinecraft(event: SlashCommandInteractionEvent, discordUser: Member) {
val minecraftID = WhitelistHandler.checkWhitelist(discordUser.idLong)
if (minecraftID != null) {
val minecraftUser = "Erdragh" // TODO: Get using actual minecraftID from server instance
val minecraftUser = MinecraftHandler.uuidToName(minecraftID)
if (minecraftUser != null) {
event.hook.setEphemeral(true).sendMessageFormat(
"%s is linked to Minecraft username %s",
discordUser,
minecraftUser
"%s is linked to Minecraft username %s", discordUser, minecraftUser
).queue()
return
}
}
event.hook.setEphemeral(true)
.sendMessageFormat("%s not linked to any Minecraft username", discordUser).queue()
event.hook.setEphemeral(true).sendMessageFormat("%s not linked to any Minecraft username", discordUser).queue()
}

override fun handle(event: SlashCommandInteractionEvent) {
Expand All @@ -144,8 +134,8 @@ object LinkCheckCommand : HandledSlashCommand, AutocompleteCommand {

override fun autocomplete(event: CommandAutoCompleteInteractionEvent) {
if (event.focusedOption.name == OPTION_MC) {
// TODO: Suggest currently online users
event.replyChoiceStrings(arrayOf("Erdragh").filter { it.startsWith(event.focusedOption.value) }).queue()
val minecraftUsers = MinecraftHandler.getOnlinePlayers()?.map(GameProfile::getName)
event.replyChoiceStrings(minecraftUsers?.filter { it.startsWith(event.focusedOption.value) } ?: listOf()).queue()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.erdragh.astralbot.handlers

import com.mojang.authlib.GameProfile
import net.minecraft.client.Minecraft
import java.util.UUID

object MinecraftHandler {
fun getOnlinePlayers(): Collection<GameProfile>? {
val server = Minecraft.getInstance().currentServer;

return server?.players?.sample
}

fun uuidToName(id: UUID): String? {
val onlinePlayer = getOnlinePlayers()?.find { it.id.equals(id) }
return if (onlinePlayer != null) {
onlinePlayer.name
} else {
null
}
}

fun nameToUUID(name: String): UUID? {
val onlinePlayer = getOnlinePlayers()?.find { it.name.equals(name) }
return if (onlinePlayer != null) {
onlinePlayer.id
} else {
null
}
}
}

0 comments on commit f2f62e6

Please sign in to comment.