Skip to content

Commit

Permalink
fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakkoble committed Jun 10, 2023
1 parent 49a46dc commit 2aa40c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/de/jakkoble/TwitchBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TwitchBot {
val withSpecialCharacters = Pattern.compile("[^A-Za-z0-9_]").matcher(inputName).find()
val userData = if (!withSpecialCharacters) inputName.getUserDataFromName() else null
@Suppress("KotlinConstantConditions")
if (userData == null || inputName.length > 25 || withSpecialCharacters) {
if (userData?.id == null || userData.name == null || inputName.length > 25 || withSpecialCharacters) {
if (sendMessage) twitchClient.chat.sendMessage(getChannelOfId(channelID), String.format(
playerNotFoundMessage,
event.redemption.user.displayName,
Expand Down
14 changes: 9 additions & 5 deletions src/main/kotlin/de/jakkoble/Whitelist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@ class Whitelist {
fun playerNames(): List<String> = whitelist.map { it.name }
fun usedWhitelist(twitchUserID: String): Boolean {
if (twitchUserID == channelID) return false
if (whitelist.count { it.twitchUserID == twitchUserID } >= ticketPerUser) return true
return false
return whitelist.count { it.twitchUserID == twitchUserID } >= ticketPerUser
}
fun userDataByName(name: String): UserData? = whitelist.firstOrNull { it.name.equals(name, ignoreCase = true) }
}
data class UserData(val name: String, val uuid: String, val twitchUserID: String)

fun String.getUserDataFromName(): Response? = getRequest("https://api.mojang.com/users/profiles/minecraft/$this")
data class Response(val name: String, val id: String)
fun String.getUserDataFromName(): Response? {
val result = getRequest("https://api.mojang.com/users/profiles/minecraft/$this")
if (result == null) TwitchWhitelist.INSTANCE.logger.warning("I am absolutely null")
return result
}
data class Response(val name: String?, val id: String?)
fun getRequest(url: String): Response? {
val client = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build()
return Gson().fromJson(client.send(request, HttpResponse.BodyHandlers.ofString()).body(), Response::class.java)
val data = client.send(request, HttpResponse.BodyHandlers.ofString()).body() ?: null
return Gson().fromJson(data, Response::class.java)
}
6 changes: 3 additions & 3 deletions src/main/kotlin/de/jakkoble/WhitelistCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ class WhitelistCommand : CommandExecutor, TabCompleter {
return true
}
val userData = inputName.getUserDataFromName()
val playerName = userData?.name
if (userData == null || inputName.length > 25) {
if (userData?.id == null || userData.name == null || inputName.length > 25) {
sendPlayerMessage(sender, "$prefix There is no Player called ${userData?.name ?: inputName}")
TwitchWhitelist.INSTANCE.server.consoleSender.sendMessage("${ChatColor.YELLOW}There is no Player called ${userData?.name ?: inputName}.")
return true
}
val playerName = userData.name
if (!Whitelist().whitelist(UserData(
name = playerName ?: return true,
name = playerName,
uuid = userData.id,
twitchUserID = channelID))) {
sendPlayerMessage(sender, "$prefix The Player $playerName is already Whitelisted.")
Expand Down

0 comments on commit 2aa40c6

Please sign in to comment.