// bobcord_kt.kt — SDK BobCord (REST + builders) // User + Bot + EmbedBuilder + ActionRow / Button / WebAction // API: https://aml-s9xx-box.taildd440a.ts.net/api/v1.1 // deps: OkHttp + org.json (ou kotlinx.serialization) import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONArray import org.json.JSONObject const val DEFAULT_BASE = "https://aml-s9xx-box.taildd440a.ts.net/api/v1.1" private val JSON_MT = "application/json; charset=utf-8".toMediaType() fun webActionConfirm(title: String = "Confirmer", body: String = "", then: JSONObject? = null) = JSONObject().put("type", "confirm").put("title", title).put("body", body).also { if (then != null) it.put("then", then) } fun webActionSendMessage(content: String, channel: String? = null, serverId: Long? = null, confirm: JSONObject? = null) = JSONObject().put("type", "send_message").put("content", content).also { if (channel != null) it.put("channel", channel) if (serverId != null) it.put("server_id", serverId) if (confirm != null) it.put("confirm", confirm) } fun webActionReact(emoji: String = "👍", messageId: Any = "self", confirm: JSONObject? = null) = JSONObject().put("type", "react").put("emoji", emoji).put("message_id", messageId).also { if (confirm != null) it.put("confirm", confirm) } fun button(label: String, style: String = "SECONDARY", customId: String? = null, url: String? = null, webAction: JSONObject? = null, disabled: Boolean = false): JSONObject { val st = style.uppercase() val o = JSONObject().put("type", "BUTTON").put("style", st).put("label", label).put("disabled", disabled) when (st) { "LINK" -> o.put("url", url ?: error("LINK requires url")) else -> o.put("custom_id", customId ?: error("BUTTON requires custom_id")) } if (st == "WEB_ACTION") o.put("web_action", webAction ?: error("WEB_ACTION requires web_action")) return o } fun actionRow(vararg components: JSONObject) = JSONObject().put("type", "ACTION_ROW").put( "components", JSONArray(components.take(5)) ) fun actionRows(vararg rows: JSONObject) = rows.take(4) class EmbedBuilder { private val data = JSONObject().put("fields", JSONArray()) fun title(t: String) = apply { data.put("title", t) } fun description(d: String) = apply { data.put("description", d) } fun color(c: Int) = apply { data.put("color", c and 0xFFFFFF) } fun addField(name: String, value: String, inline: Boolean = false) = apply { data.getJSONArray("fields").put(JSONObject().put("name", name).put("value", value).put("inline", inline)) } fun footer(text: String) = apply { data.put("footer", JSONObject().put("text", text)) } fun build() = data } open class User(token: String, var baseUrl: String = DEFAULT_BASE) { val token = token.trim() var me: JSONObject? = null private val client = OkHttpClient() fun request(method: String, path: String, body: JSONObject? = null): JSONObject? { val url = "${baseUrl.trimEnd('/')}/${path.trimStart('/')}" val b = body?.toString()?.toRequestBody(JSON_MT) val req = Request.Builder().url(url).header("Authorization", "Bearer $token") .method(method, if (method == "GET" || method == "DELETE" && body == null) null else b ?: "".toRequestBody(JSON_MT)) .apply { if (method == "DELETE" && body != null) method("DELETE", b) } .build() client.newCall(req).execute().use { res -> if (res.code == 204) return null val text = res.body?.string().orEmpty() val data = if (text.isBlank()) JSONObject() else JSONObject(text) if (!res.isSuccessful) error("HTTP ${res.code}: $data") return data } } fun login(): JSONObject { val data = request("GET", "/me")!! me = data.optJSONObject("user") ?: data return me!! } fun send( content: String = "", channel: String = "général", serverId: Long = 1, embeds: List? = null, actionRows: List? = null, attachments: Any? = null, ): JSONObject? { val body = JSONObject().put("content", content).put("channel", channel).put("server_id", serverId) if (embeds != null) body.put("embeds", JSONArray(embeds)) if (attachments != null) body.put("attachments", attachments) if (actionRows != null) body.put("action_rows", JSONArray(actionRows)) return request("POST", "/message", body) } } class Bot(token: String, baseUrl: String = DEFAULT_BASE) : User(token, baseUrl)