Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Shamrock: support /set_guild_member_role
Browse files Browse the repository at this point in the history
  • Loading branch information
whitechi73 committed Feb 2, 2024
1 parent c436898 commit 7bfb9b7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ public interface IKernelGuildService {

void deleteRole(long guild, long role, IGProResultCallback cb);

void setMemberRoles(long guild, long u1, long u2, long tinyId, ArrayList<Long> addRoles, ArrayList<Long> removeRoles, IGProResultCallback cb);

}
12 changes: 12 additions & 0 deletions xposed/src/main/java/moe/fuqiuluo/qqinterface/servlet/GProSvc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,16 @@ internal object GProSvc: BaseSvc() {
}
}
}

fun setMemberRole(guildId: ULong, tinyId: ULong, roleId: ULong, isSet: Boolean) {
val kernelGProService = NTServiceFetcher.kernelService.wrapperSession.guildService
val addList = arrayListOf<Long>()
val rmList = arrayListOf<Long>()
(if (isSet) addList else rmList).add(roleId.toLong())
kernelGProService.setMemberRoles(guildId.toLong(), 0, 0, tinyId.toLong(), addList, rmList) { code, msg, result ->
if (code != 0) {
LogCenter.log("setMemberRole failed: $code($msg) => $result", Level.WARN)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package moe.fuqiuluo.shamrock.remote.action.handlers

import kotlinx.serialization.json.JsonElement
import moe.fuqiuluo.qqinterface.servlet.GProSvc
import moe.fuqiuluo.shamrock.remote.action.ActionSession
import moe.fuqiuluo.shamrock.remote.action.IActionHandler
import moe.fuqiuluo.shamrock.tools.EmptyJsonString
import moe.fuqiuluo.shamrock.tools.asString
import moe.fuqiuluo.symbols.OneBotHandler

@OneBotHandler("set_guild_member_role")
internal object SetGuildMemberRole: IActionHandler() {
override suspend fun internalHandle(session: ActionSession): String {
val guildId = session.getString("guild_id").toULong()
val role = session.getString("role_id").toULong()
val set = session.getBooleanOrDefault("set", false)
return if (session.has("user_id")) {
val userId = session.getString("user_id").toULong()
invoke(guildId, userId, role, set, echo = session.echo)
} else if (session.isArray("users")) {
invoke(guildId, session.getArray("users").map {
it.asString.toULong()
}, role, set, echo = session.echo)
} else {
logic("missing user_id or users", echo = session.echo)
}
}

operator fun invoke(guildId: ULong, users: List<ULong>, roleId: ULong, set: Boolean, echo: JsonElement = EmptyJsonString): String {
users.forEach {
GProSvc.setMemberRole(guildId, it, roleId, set)
}
return ok("success", echo = echo)
}

operator fun invoke(guildId: ULong, user: ULong, roleId: ULong, set: Boolean, echo: JsonElement = EmptyJsonString): String {
GProSvc.setMemberRole(guildId, user, roleId, set)
return ok("success", echo = echo)
}

override val requiredParams: Array<String> = arrayOf("guild_id", "role_id")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import moe.fuqiuluo.shamrock.remote.action.handlers.GetGuildRoles
import moe.fuqiuluo.shamrock.remote.action.handlers.GetGuildServiceProfile
import moe.fuqiuluo.shamrock.remote.action.handlers.SendGuildMessage
import moe.fuqiuluo.shamrock.remote.action.handlers.SendMessage
import moe.fuqiuluo.shamrock.remote.action.handlers.SetGuildMemberRole
import moe.fuqiuluo.shamrock.tools.fetchGetOrNull
import moe.fuqiuluo.shamrock.tools.fetchGetOrThrow
import moe.fuqiuluo.shamrock.tools.fetchOrNull
import moe.fuqiuluo.shamrock.tools.fetchOrThrow
import moe.fuqiuluo.shamrock.tools.fetchPostJsonArray
import moe.fuqiuluo.shamrock.tools.fetchPostJsonObject
import moe.fuqiuluo.shamrock.tools.fetchPostJsonString
import moe.fuqiuluo.shamrock.tools.fetchPostOrNull
import moe.fuqiuluo.shamrock.tools.fetchPostOrThrow
import moe.fuqiuluo.shamrock.tools.getOrPost
Expand Down Expand Up @@ -130,4 +132,22 @@ fun Routing.guildAction() {
val roleId = fetchOrThrow("role_id").toULong()
call.respondText(DeleteGuildRole(guildId, roleId), ContentType.Application.Json)
}

getOrPost("/set_guild_member_role") {
val guildId = fetchOrThrow("guild_id").toULong()
val roleId = fetchOrThrow("role_id").toULong()
val set = fetchOrNull("set")?.toBoolean() ?: false
val userId = fetchOrNull("user_id")?.toULong()
val users = fetchOrNull("users")?.split(",")?.map { it.toULong() }
call.respondText(
if (userId != null) {
SetGuildMemberRole(guildId, userId, roleId, set)
} else if (users != null) {
SetGuildMemberRole(guildId, users, roleId, set)
} else {
throw IllegalArgumentException("missing user_id or users")
},
ContentType.Application.Json
)
}
}

0 comments on commit 7bfb9b7

Please sign in to comment.