This commit is contained in:
Finnley Somdahl 2023-11-26 02:46:36 -06:00
parent cf2d9ad654
commit d131562f34
2 changed files with 0 additions and 205 deletions

View file

@ -61,16 +61,6 @@ object Discord {
}
private var rpc : RPC? = null
/*suspend fun getUserData() = tryWithSuspend(true) {
if(rpc==null) {
val rpc = RPC(token!!, Dispatchers.IO).also { rpc = it }
val user: User = rpc.getUserData()
userid = user.username
avatar = user.userAvatar()
rpc.close()
true
} else true
} ?: false*/
fun warning(context: Context) = CustomBottomDialog().apply {

View file

@ -41,201 +41,6 @@ open class RPC(val token: String, val coroutineContext: CoroutineContext) {
}
data class Link(val label: String, val url: String)
/*
private val client = OkHttpClient.Builder()
.connectTimeout(10, SECONDS)
.readTimeout(10, SECONDS)
.writeTimeout(10, SECONDS)
.build()
private val request = Request.Builder()
.url("wss://gateway.discord.gg/?encoding=json&v=10")
.build()
private var webSocket = client.newWebSocket(request, Listener())
var applicationId: String? = null
var type: Type? = null
var activityName: String? = null
var details: String? = null
var state: String? = null
var largeImage: Link? = null
var smallImage: Link? = null
var status: String? = null
var startTimestamp: Long? = null
var stopTimestamp: Long? = null
var buttons = mutableListOf<Link>()
private suspend fun createPresence(): String {
val j = json.encodeToString(Presence.Response(
3,
Presence(
activities = listOf(
Activity(
name = activityName,
state = state,
details = details,
type = type?.ordinal,
timestamps = if (startTimestamp != null)
Activity.Timestamps(startTimestamp, stopTimestamp)
else null,
assets = Activity.Assets(
largeImage = largeImage?.url?.discordUrl(),
largeText = largeImage?.label,
smallImage = smallImage?.url?.discordUrl(),
smallText = smallImage?.label
),
buttons = buttons.map { it.label },
metadata = Activity.Metadata(
buttonUrls = buttons.map { it.url }
),
applicationId = applicationId,
)
),
afk = true,
since = startTimestamp,
status = status
)
))
logger("Presence: $j")
return j
}
private fun sendIdentify() {
val response = Identity.Response(
op = 2,
d = Identity(
token = token,
properties = Identity.Properties(
os = "windows",
browser = "Chrome",
device = "disco"
),
compress = false,
intents = 0
)
)
webSocket.send(json.encodeToString(response))
}
fun send(block: RPC.() -> Unit) {
block.invoke(this)
send()
}
var started = false
var whenStarted: ((User) -> Unit)? = null
fun send() {
val send = {
CoroutineScope(coroutineContext).launch {
webSocket.send(createPresence())
}
}
if (!started) whenStarted = {
snackString("Discord message sent")
send.invoke()
whenStarted = null
}
else send.invoke()
}
fun close() {
webSocket.send(
json.encodeToString(
Presence.Response(
3,
Presence(status = "offline")
)
)
)
webSocket.close(4000, "Interrupt")
}
//I kinda hate this
suspend fun getUserData(): User = suspendCancellableCoroutine { continuation ->
whenStarted = {
continuation.resume(it, onCancellation = null)
whenStarted = null
}
continuation.invokeOnCancellation {
whenStarted = null
}
}
var onReceiveUserData: ((User) -> Deferred<Unit>)? = null
inner class Listener : WebSocketListener() {
private var seq: Int? = null
private var heartbeatInterval: Long? = null
var scope = CoroutineScope(coroutineContext)
private fun sendHeartBeat() {
scope.cancel()
scope = CoroutineScope(coroutineContext)
scope.launch {
delay(heartbeatInterval!!)
webSocket.send("{\"op\":1, \"d\":$seq}")
}
}
override fun onMessage(webSocket: WebSocket, text: String) {
val map = json.decodeFromString<Res>(text)
seq = map.s
when (map.op) {
10 -> {
map.d as JsonObject
heartbeatInterval = map.d["heartbeat_interval"]!!.jsonPrimitive.long
sendHeartBeat()
sendIdentify()
snackString(map.t)
}
0 -> if (map.t == "READY") {
val user = json.decodeFromString<User.Response>(text).d.user
snackString(map.t)
started = true
whenStarted?.invoke(user)
}
1 -> {
if (scope.isActive) scope.cancel()
webSocket.send("{\"op\":1, \"d\":$seq}")
snackString(map.t)
}
11 -> sendHeartBeat()
7 -> webSocket.close(400, "Reconnect")
9 -> {
sendHeartBeat()
sendIdentify()
snackString(map.t)
}
}
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
println("Server Closed : $code $reason")
if (code == 4000) {
scope.cancel()
}
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
println("Failure : ${t.message}")
if (t.message != "Interrupt") {
this@RPC.webSocket = client.newWebSocket(request, Listener())
}
}
}
*/
companion object {
data class RPCData(