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

Commit

Permalink
Shamrock: support /create_guild_role
Browse files Browse the repository at this point in the history
  • Loading branch information
whitechi73 committed Feb 3, 2024
1 parent 7bfb9b7 commit fb00e5c
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ public String toString() {
return "GProRoleCreateInfo{name=" + this.name + ",color=" + this.color + ",bHoist=" + this.bHoist + ",rolePermissions=" + this.rolePermissions + ",}";
}

public GProRoleCreateInfo(String str, long j2, boolean z, GProRolePermission gProRolePermission) {
public GProRoleCreateInfo(String name, long color, boolean hoist, GProRolePermission permissions) {
this.name = "";
this.rolePermissions = new GProRolePermission();
this.name = str;
this.color = j2;
this.bHoist = z;
this.rolePermissions = gProRolePermission;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tencent.qqnt.kernel.nativeinterface;

public interface IGProCreateRoleCallback {
void onCreateRoleResult(int code, String msg, GProSecurityResult result, GProGuildRole role);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ public interface IKernelGuildService {

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

void setRoleInfo(long guild, long role, GProRoleCreateInfo info, IGProResultCallback cb);

void createRole(long guildId, GProRoleCreateInfo info, ArrayList<Long> initialUsers, IGProCreateRoleCallback cb);
}
50 changes: 50 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 @@ -4,7 +4,9 @@ package moe.fuqiuluo.qqinterface.servlet

import com.tencent.mobileqq.qqguildsdk.api.IGPSService
import com.tencent.qqnt.kernel.nativeinterface.GProGuildRole
import com.tencent.qqnt.kernel.nativeinterface.GProRoleCreateInfo
import com.tencent.qqnt.kernel.nativeinterface.GProRoleMemberList
import com.tencent.qqnt.kernel.nativeinterface.GProRolePermission
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.suspendCancellableCoroutine
Expand Down Expand Up @@ -317,4 +319,52 @@ internal object GProSvc: BaseSvc() {
}
}
}

suspend fun getGuildRolePermission(guildId: ULong, roleId: ULong): Result<GProGuildRole> {
val kernelGProService = NTServiceFetcher.kernelService.wrapperSession.guildService
val role:GProGuildRole = withTimeoutOrNull(5000) {
suspendCancellableCoroutine {
kernelGProService.fetchRoleWithPermission(guildId.toLong(), roleId.toLong(), 1) { code, msg, role, _, _, _ ->
if (code != 0) {
LogCenter.log("getGuildRolePermission failed: $code($msg)", Level.WARN)
it.resume(null)
} else it.resume(role)
}
}
} ?: return Result.failure(Exception("unable to fetch guild role permission"))
return Result.success(role)
}

suspend fun updateGuildRole(guildId: ULong, roleId: ULong, name: String, color: Long): Result<Unit> {
val oldInfo = getGuildRolePermission(guildId, roleId).onFailure {
return Result.failure(it)
}.getOrThrow()
val kernelGProService = NTServiceFetcher.kernelService.wrapperSession.guildService
val info = GProRoleCreateInfo(
name, color, oldInfo.bHoist, oldInfo.rolePermissions
)
kernelGProService.setRoleInfo(guildId.toLong(), roleId.toLong(), info) { code, msg, result ->
if (code != 0) {
LogCenter.log("updateGuildRole failed: $code($msg) => $result", Level.WARN)
}
}
return Result.success(Unit)
}

suspend fun createGuildRole(guildId: ULong, name: String, color: Long, initialUsers: ArrayList<Long>): Result<GProGuildRole> {
val kernelGProService = NTServiceFetcher.kernelService.wrapperSession.guildService
val permission = GProRolePermission(false, arrayListOf())
val info = GProRoleCreateInfo(name, color, false, permission)
val role: GProGuildRole = withTimeoutOrNull(5000) {
suspendCancellableCoroutine {
kernelGProService.createRole(guildId.toLong(), info, initialUsers) { code, msg, result, role ->
if (code != 0) {
LogCenter.log("createGuildRole failed: $code($msg) => $result", Level.WARN)
it.resume(null)
} else it.resume(role)
}
}
} ?: return Result.failure(Exception("unable to create guild role"))
return Result.success(role)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@file:Suppress("UNCHECKED_CAST")

package moe.fuqiuluo.shamrock.remote.action.handlers

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
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("create_guild_role")
internal object CreateGuildRole: IActionHandler() {
override suspend fun internalHandle(session: ActionSession): String {
val guildId = session.getString("guild_id").toULong()
val name = session.getString("name")
val color = session.getLong("color")
val initialUsers = session.getArray("initial_users").map {
it.asString.toULong()
}
return invoke(guildId, color, name, initialUsers, session.echo)
}

suspend operator fun invoke(guildId: ULong, color: Long, name: String, initialUsers: List<ULong>, echo: JsonElement = EmptyJsonString): String {
val result = GProSvc.createGuildRole(guildId, name, color, initialUsers as ArrayList<Long>).onFailure {
return error(it.message ?: "Unknown error", echo)
}.getOrThrow()
return ok(data = CreateGuildRoleResult(
result.roleId.toULong()
), echo = echo)
}

override val requiredParams: Array<String> = arrayOf("guild_id", "color", "name", "initial_users")

@Serializable
data class CreateGuildRoleResult(
@SerialName("role_id") val roleId: ULong
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ internal object GetGuildRoles: IActionHandler() {
return ok(GetGuildRolesResult(result.map {
GuildRole(
color = it.color,
disabled = it.count <= 0,
disabled = it.bHoist,
independent = it.isChannelRole,
maxCount = it.memberLimit,
memberCount = it.count,
owned = it.bHoist,
owned = it.isNotSort,
roleId = it.roleId,
roleName = it.name,
permission = it.rolePermissions.permissionList.map {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.symbols.OneBotHandler

@OneBotHandler("update_guild_role")
internal object UpdateGuildRole: IActionHandler() {
override suspend fun internalHandle(session: ActionSession): String {
val guildId = session.getString("guild_id").toULong()
val roleId = session.getString("role_id").toULong()
val name = session.getString("name")
val color = session.getLong("color")
return invoke(guildId, roleId, name, color, session.echo)
}

suspend operator fun invoke(guildId: ULong, roleId: ULong, name: String, color: Long, echo: JsonElement = EmptyJsonString): String {
val result = GProSvc.updateGuildRole(guildId, roleId, name, color).onFailure {
return error(it.message ?: "Unknown error", echo)
}
return ok("success", echo = echo)
}

override val requiredParams: Array<String> = arrayOf("role_id", "guild_id", "name", "color")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import moe.fuqiuluo.shamrock.helper.MessageHelper
import moe.fuqiuluo.shamrock.remote.action.handlers.CreateGuildRole
import moe.fuqiuluo.shamrock.remote.action.handlers.DeleteGuildRole
import moe.fuqiuluo.shamrock.remote.action.handlers.GetGProChannelList
import moe.fuqiuluo.shamrock.remote.action.handlers.GetGuildFeeds
Expand All @@ -20,6 +21,7 @@ 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.remote.action.handlers.UpdateGuildRole
import moe.fuqiuluo.shamrock.tools.fetchGetOrNull
import moe.fuqiuluo.shamrock.tools.fetchGetOrThrow
import moe.fuqiuluo.shamrock.tools.fetchOrNull
Expand Down Expand Up @@ -150,4 +152,20 @@ fun Routing.guildAction() {
ContentType.Application.Json
)
}

getOrPost("/update_guild_role") {
val guildId = fetchOrThrow("guild_id").toULong()
val roleId = fetchOrThrow("role_id").toULong()
val name = fetchOrThrow("name")
val color = fetchOrThrow("color").toLong()
call.respondText(UpdateGuildRole(guildId, roleId, name, color), ContentType.Application.Json)
}

getOrPost("/create_guild_role") {
val guildId = fetchOrThrow("guild_id").toULong()
val name = fetchOrThrow("name")
val color = fetchOrThrow("color").toLong()
val initialUsers = fetchOrThrow("initial_users").split(",").map { it.toULong() }
call.respondText(CreateGuildRole(guildId, color, name, initialUsers), ContentType.Application.Json)
}
}

0 comments on commit fb00e5c

Please sign in to comment.