@@ -2,21 +2,42 @@ package ua.mei.minekord.auth
2
2
3
3
import dev.kord.common.annotation.KordExperimental
4
4
import dev.kord.core.entity.Member
5
+ import io.ktor.client.HttpClient
6
+ import io.ktor.client.engine.okhttp.OkHttp
7
+ import io.ktor.client.plugins.timeout
8
+ import io.ktor.client.request.get
9
+ import io.ktor.client.statement.HttpResponse
10
+ import io.ktor.http.HttpStatusCode
5
11
import kotlinx.coroutines.flow.filter
6
12
import kotlinx.coroutines.flow.firstOrNull
7
13
import kotlinx.coroutines.runBlocking
8
14
import ua.mei.minekord.bot.MinekordBot
9
15
import ua.mei.minekord.config.AuthSpec
10
16
import ua.mei.minekord.config.config
11
17
import java.util.UUID
18
+ import kotlin.experimental.inv
12
19
13
20
object SnowflakeToUUID {
21
+ private val client: HttpClient = HttpClient (OkHttp )
22
+
14
23
fun enabled (): Boolean = config[AuthSpec .uuidFromSnowflake]
24
+ fun allowOfflinePlayers (): Boolean = config[AuthSpec .allowOfflinePlayers]
25
+
26
+ fun premiumPlayer (uuid : UUID ): Boolean {
27
+ return runBlocking {
28
+ try {
29
+ client.get(" https://sessionserver.mojang.com/session/minecraft/profile/$uuid " )
30
+ .status == HttpStatusCode .OK
31
+ } catch (_: Exception ) {
32
+ false
33
+ }
34
+ }
35
+ }
15
36
16
37
@OptIn(KordExperimental ::class )
17
38
fun generateFromNickname (nickname : String ): UUID ? {
18
39
return runBlocking {
19
- return @runBlocking try {
40
+ try {
20
41
val member: Member = MinekordBot .guild?.getMembers(nickname)?.filter {
21
42
it.roleIds.map { it.value }.all { it in config[AuthSpec .requiredRoles] }
22
43
}?.firstOrNull() ? : return @runBlocking null
@@ -29,8 +50,8 @@ object SnowflakeToUUID {
29
50
}
30
51
31
52
fun generateFromId (discordId : ULong ): UUID {
32
- val mostSigBytes: ByteArray = ByteArray (8 ) { i -> (discordId shr ((7 - i ) * 8 ) and 0xFFu ).toByte() }
33
- val leastSigBytes: ByteArray = ByteArray ( 8 ) { i -> (( ULong . MAX_VALUE - discordId) shr (( 7 - i) * 8 ) and 0xFFu ).toByte() }
53
+ val mostSigBytes: ByteArray = ByteArray (8 ) { (discordId shr ((7 - it ) * 8 ) and 0xFFu ).toByte() }
54
+ val leastSigBytes: ByteArray = mostSigBytes.map { it. inv () }.toByteArray()
34
55
35
56
val mostSigBits: Long = mostSigBytes.fold(0L ) { acc, byte -> (acc shl 8 ) or (byte.toLong() and 0xFF ) }
36
57
val leastSigBits: Long = leastSigBytes.fold(0L ) { acc, byte -> (acc shl 8 ) or (byte.toLong() and 0xFF ) }
0 commit comments