Skip to content

Commit

Permalink
0.2.0 update
Browse files Browse the repository at this point in the history
  • Loading branch information
layou233 committed Oct 27, 2020
1 parent 6f0f0a8 commit fea999d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 63 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "layou233.mcbot"
version = "0.1.0"
version = "0.2.0"

repositories {
mavenLocal()
Expand Down
26 changes: 20 additions & 6 deletions src/main/kotlin/layou233/mcbot/PluginMain.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package layou233.mcbot

import kotlinx.coroutines.plus
import layou233.mcbot.hypixel.apiRequester.*
import layou233.mcbot.mojang.getSkin.skin
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.event.Listener
Expand All @@ -14,7 +14,7 @@ import net.mamoe.mirai.utils.info
object PluginMain : KotlinPlugin(
JvmPluginDescription(
id = "layou233.mcbot",
version = "0.1.0"
version = "0.2.0"
)
) {
var initialSubscription: Listener<BotEvent>? = null
Expand All @@ -28,15 +28,29 @@ object PluginMain : KotlinPlugin(
// logger.info { saying }
if (saying.length > 1) {
if (saying.substring(0, 2) == "sb") {
if (saying.substring(2).indexOf("money") == 0) {
if (saying.substring(2, 7) == "money") {
val playerId: String = saying.substring(7)
quoteReply("$playerId 共拥有 ${sb_money(playerId).toString()} coins")
val money: Int = sb_money(playerId)
when (money) {
-1 -> quoteReply("$playerId 没有Skyblock存档!")
-2 -> quoteReply("玩家 $playerId 无Hypixel数据或不存在!")
-3 -> quoteReply("网络超时,请重试.")
else -> quoteReply("$playerId 共拥有 $money coins")
}
}
}
}
if (saying.length > 4) {
if (saying.substring(0, 4) == "skin") {
val playerId: String = saying.substring(4)
val skinAddr: String? = skin(playerId)
if (skinAddr == null) quoteReply("未找到该玩家或服务器出错")
else {
quoteReply(skinAddr)
}
}
}

}

is TempMessageEvent -> reply("为防止部分离奇问题出现,请先添加好友再使用功能!")
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/layou233/mcbot/httpClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package layou233.mcbot.httpClient

import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

val client = OkHttpClient.Builder()
.connectTimeout(10000, TimeUnit.MILLISECONDS)
.readTimeout(3000, TimeUnit.MILLISECONDS)
.addInterceptor {
return@addInterceptor it.proceed(
it.request().newBuilder()
.addHeader(
"User-Agent",
""
)
.build()
)
}
.build()
50 changes: 21 additions & 29 deletions src/main/kotlin/layou233/mcbot/hypixel/apiRequester.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
package layou233.mcbot.hypixel.apiRequester

import okhttp3.OkHttpClient
import layou233.mcbot.httpClient.client
import okhttp3.Request
import okhttp3.Response
import java.lang.Exception
import java.util.concurrent.TimeUnit
import java.net.SocketTimeoutException

val client = OkHttpClient.Builder()
.connectTimeout(10000, TimeUnit.MILLISECONDS)
.readTimeout(3000, TimeUnit.MILLISECONDS)
.addInterceptor {
return@addInterceptor it.proceed(
it.request().newBuilder()
.addHeader(
"User-Agent",
""
)
.build()
)
}
.build()

fun sb_money(id: String): Int {
val response: Response =
client.newCall(Request.Builder().url("http://sky.shiiyu.moe/api/v2/coins/$id").build()).execute()
var money: Int = 0
val back: String = response.body!!.string()
val finding: List<String> = listOf("purse", "bank")
val indexes: List<Int> = listOf(7, 6)
for (ind in finding.indices) {
val found = Regex("""${finding.elementAt(ind)}\":.*?(?=[,.}])""").findAll(back)
for (i in found) money += i.value.substring(indexes.elementAt(ind)).toInt()
try {
val response =
client.newCall(Request.Builder().url("http://sky.shiiyu.moe/api/v2/coins/$id").build()).execute()
var money: Int = 0
val back: String = response.body!!.string()
if (back == "{\"error\":\"Player has no SkyBlock profiles.\"}") return -1
else if (back == "{\"error\":\"Failed resolving username.\"}") return -2
else {
/* val finding: List<String> = listOf("purse", "bank")
val indexes: List<Int> = listOf(7, 6)*/
for (ind in listOf("purse", "bank").indices) {
val found = Regex("""${listOf("purse", "bank").elementAt(ind)}\":.*?(?=[,.}])""").findAll(back)
for (i in found) money += i.value.substring(listOf(7, 6).elementAt(ind)).toInt()
}
response.close()
return money
}
} catch (e: SocketTimeoutException) {
return -3
}
response.close()
return money
}
52 changes: 27 additions & 25 deletions src/main/kotlin/layou233/mcbot/mojang/apiRequester.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package layou233.mcbot.mojang.apiRequester

import okhttp3.OkHttpClient
import layou233.mcbot.httpClient.client
import okhttp3.Request
import java.lang.Exception
import java.util.concurrent.TimeUnit
import java.net.SocketTimeoutException
import java.util.Base64.getDecoder

val client = OkHttpClient.Builder()
.connectTimeout(10000, TimeUnit.MILLISECONDS)
.readTimeout(3000, TimeUnit.MILLISECONDS)
.addInterceptor {
return@addInterceptor it.proceed(
it.request().newBuilder()
.addHeader(
"User-Agent",
""
)
.build()
)
}
.build()
/*
fun uuid(id: String): String? {
TODO("Have not start yet")
return try
try {
val response =
client.newCall(Request.Builder().url("https://api.mojang.com/users/profiles/minecraft/$id").build())
.execute()
val found: String = Regex("\\\":.*?(?=[,.}])").find(response.toString()).toString()
val back: String = response.body!!.string()
response.close()
found
} catch (e: Exception) {
return null
return back.substring(17 + id.length, back.length - 2)
} catch (e: SocketTimeoutException) {
return null
}
}
}*/

fun resource(uuid: String?): String? {
if (uuid==null) return null
else{
try {
val response =
client.newCall(Request.Builder().url("https://sessionserver.mojang.com/session/minecraft/profile/$uuid")
.build())
.execute()
val back: String = response.body!!.string().replace("\\s".toRegex(), "")
response.close()
val res: String = Regex("""value\":\".*=+""").find(back)!!.value.substring(8)
return String(getDecoder().decode(res)).replace("\\s".toRegex(), "")
} catch (e: SocketTimeoutException) {
return null
}}

}
13 changes: 13 additions & 0 deletions src/main/kotlin/layou233/mcbot/mojang/getSkin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package layou233.mcbot.mojang.getSkin

import layou233.mcbot.mojang.apiRequester.resource
import layou233.mcbot.mojang.apiRequester.uuid

fun skin(id: String): String? {
val res: String? = resource(uuid(id))
return if (res == null) null
else {
Regex("""http.*[0-z]""").find(res)!!.value
//Example for Regex: {"url":"http://textures.minecraft.net/texture/a1b2c3d4e5"}}}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/plugins.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "MCBot"
author: "layou233"
version: "0.1.0"
version: "0.2.0"
main: "layou233.mcbot"
info: ""
info: "https://github.com/layou233/mirai-MCBot/"
depends: []

0 comments on commit fea999d

Please sign in to comment.