Skip to content

Commit

Permalink
feat: add sender actionable methods and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Sep 18, 2024
1 parent b3d24bc commit baec73e
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlin.code.style=official

libVersion=1.7.2
libVersion=1.8.0
11 changes: 4 additions & 7 deletions src/main/kotlin/cn/rtast/rob/entity/GroupMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ package cn.rtast.rob.entity

import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.ROneBotFactory.actionCoroutineScope
import cn.rtast.rob.entity.internal.Actionable
import cn.rtast.rob.exceptions.IllegalDelayException
import cn.rtast.rob.entity.internal.MessageActionable
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OBMessage
import com.google.gson.annotations.SerializedName
Expand All @@ -29,14 +28,12 @@ data class GroupMessage(
val message: List<ArrayMessage>,
@SerializedName("raw_message")
val rawMessage: String,
val sender: Sender,
var sender: Sender,
val time: Long,
val listener: OBMessage
) : Actionable {
) : MessageActionable {
override suspend fun revoke(delay: Int) {
if (delay < 0) {
throw IllegalDelayException("Delay second(s) must great than 0 or equals to 0! >>> $delay")
}
super.revoke(delay)
if (delay != 0) actionCoroutineScope.launch {
delay(delay * 1000L)
ROneBotFactory.action.revokeMessage(messageId)
Expand Down
9 changes: 3 additions & 6 deletions src/main/kotlin/cn/rtast/rob/entity/PrivateMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ package cn.rtast.rob.entity

import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.ROneBotFactory.actionCoroutineScope
import cn.rtast.rob.entity.internal.Actionable
import cn.rtast.rob.exceptions.IllegalDelayException
import cn.rtast.rob.entity.internal.MessageActionable
import cn.rtast.rob.util.ob.MessageChain
import com.google.gson.annotations.SerializedName
import kotlinx.coroutines.delay
Expand All @@ -28,11 +27,9 @@ data class PrivateMessage(
val rawMessage: String,
val sender: Sender,
val time: Long,
) : Actionable {
) : MessageActionable {
override suspend fun revoke(delay: Int) {
if (delay < 0) {
throw IllegalDelayException("Delay second(s) must great than 0 or equals to 0! >>> $delay")
}
super.revoke(delay)
if (delay != 0) actionCoroutineScope.launch {
delay(delay * 1000L)
ROneBotFactory.action.revokeMessage(messageId)
Expand Down
36 changes: 35 additions & 1 deletion src/main/kotlin/cn/rtast/rob/entity/Sender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

package cn.rtast.rob.entity

import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.entity.internal.GroupUserActionable
import cn.rtast.rob.enums.UserRole
import cn.rtast.rob.util.ob.MessageChain
import com.google.gson.annotations.SerializedName

data class Sender(
Expand All @@ -19,4 +22,35 @@ data class Sender(
val card: String?,
val level: String,
val age: String,
)
val groupId: Long = 114514L
) : GroupUserActionable {
override suspend fun kick(rejectJoinRequest: Boolean) {
ROneBotFactory.action.kickGroupMember(groupId, userId, rejectJoinRequest)
}

override suspend fun ban(duration: Int) {
super.ban(duration)
ROneBotFactory.action.setGroupBan(groupId, userId, duration)
}

override suspend fun setGroupCard(card: String?) {
ROneBotFactory.action.setGroupMemberCard(groupId, userId, card ?: "")
}

override suspend fun setGroupAdmin(enable: Boolean) {
ROneBotFactory.action.setGroupAdmin(groupId, userId, enable)
}

override suspend fun sendLike(times: Int) {
super.sendLike(times)
ROneBotFactory.action.sendLike(userId, times)
}

override suspend fun sendMessage(content: String) {
ROneBotFactory.action.sendPrivateMessage(userId, content)
}

override suspend fun sendMessage(content: MessageChain) {
ROneBotFactory.action.sendPrivateMessage(userId, content)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@

package cn.rtast.rob.entity.internal

import cn.rtast.rob.exceptions.IllegalDelayException
import cn.rtast.rob.util.ob.MessageChain

interface Actionable {
interface MessageActionable {

/**
* 撤回消息的接口, 提供一个延迟秒数在n秒后撤回消息
* 在私聊消息中无法撤回对方的消息
* 如果OneBot实现开启了上报自身消息则可以使用这个方法来撤回自身的消息
*/
suspend fun revoke(delay: Int = 0) {}
suspend fun revoke(delay: Int) {
if (delay < 0) {
throw IllegalDelayException("Delay second(s) must great than 0 or equals to 0! >>> $delay")
}
}

/**
* 没有延迟秒数的撤回方法
Expand Down
81 changes: 81 additions & 0 deletions src/main/kotlin/cn/rtast/rob/entity/internal/UserActionable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/18
*/


package cn.rtast.rob.entity.internal

import cn.rtast.rob.exceptions.IllegalDurationException
import cn.rtast.rob.exceptions.IllegalLikeTimesException
import cn.rtast.rob.util.ob.MessageChain


interface UserActionable {

/**
* 赞用户的名片
*/
suspend fun sendLike(times: Int) {
if (times !in 1..10) {
throw IllegalLikeTimesException("Like times must in 1 ~ 10 >>> $times")
}
}

/**
* 发送纯文本消息
*/
suspend fun sendMessage(content: String)

/**
* 发送数组消息链消息
*/
suspend fun sendMessage(content: MessageChain)
}

interface GroupUserActionable : UserActionable {

/**
* 将用户踢出群聊可以设置是否拒绝加群请求
*/
suspend fun kick(rejectJoinRequest: Boolean)

/**
* 带有默认值的踢出群员(允许加群请求)
*/
suspend fun kick() {
this.kick(false)
}

/**
* 设置群员禁言,时长单位为秒(s)
*/
suspend fun ban(duration: Int) {
if (duration <= 0) {
throw IllegalDurationException("Duration must great than 0(>0) >>> $duration")
}
}

/**
* 带有默认值的禁言(30分钟 30 * 60s)
*/
suspend fun ban() {
this.ban(30 * 60)
}

/**
* 设置群员的群昵称
*/
suspend fun setGroupCard(card: String?)

/**
* 设置群员管理员, enable为true则为设置为false则取消设置
*/
suspend fun setGroupAdmin(enable: Boolean)
}

/**
* 私聊消息的可执行操作
*/
interface PrivateUserActionable : UserActionable
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal data class KickGroupMemberOut(
val params: Params,
) {
data class Params(
@SerializedName("message_id")
@SerializedName("group_id")
val groupId: Long,
@SerializedName("user_id")
val userId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ internal data class RevokeMessageOut(
) {
data class Params(
@SerializedName("message_id")
val groupId: Long,
val messageId: Long,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/18
*/


package cn.rtast.rob.exceptions

class IllegalDurationException(override val message: String): Exception(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/18
*/


package cn.rtast.rob.exceptions

class IllegalLikeTimesException(override val message: String) : Exception(message)
12 changes: 12 additions & 0 deletions src/main/kotlin/cn/rtast/rob/util/ob/MessageHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ object MessageHandler {
when (serializedMessage.messageType) {
MessageType.group -> {
val msg = message.fromJson<GroupMessage>()
val oldSender = msg.sender
val newSenderWithGroupId = Sender(
oldSender.userId,
oldSender.nickname,
oldSender.sex,
oldSender.role,
oldSender.card,
oldSender.level,
oldSender.age,
msg.groupId
)
msg.sender = newSenderWithGroupId
if (msg.groupId !in listeningGroups && listeningGroups.isNotEmpty()) return
msg.message.distinctBy { it.type }.forEach {
if (it.type == ArrayMessageType.reply) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/cn/rtast/rob/util/ob/OBAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import cn.rtast.rob.entity.ArrayMessage
import cn.rtast.rob.entity.out.ArrayGroupMessageOut
import cn.rtast.rob.entity.out.ArrayPrivateMessageOut
import cn.rtast.rob.entity.out.CQCodeGroupMessageOut
import cn.rtast.rob.entity.out.CQCodePrivateMessageOut
import cn.rtast.rob.entity.out.CanSendImageOut
import cn.rtast.rob.entity.out.CanSendRecordOut
import cn.rtast.rob.entity.out.GetForwardMessageOut
Expand Down Expand Up @@ -72,7 +73,7 @@ interface OBAction {
}

suspend fun sendPrivateMessage(userId: Long, content: String) {
this.sendToWs(CQCodeGroupMessageOut(params = CQCodeGroupMessageOut.Params(userId, content)))
this.sendToWs(CQCodePrivateMessageOut(params = CQCodePrivateMessageOut.Params(userId, content)))
}

suspend fun sendPrivateMessage(userId: Long, content: CQMessageChain) {
Expand Down
5 changes: 2 additions & 3 deletions src/test/kotlin/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ fun main() {
val rob = ROneBotFactory.createClient(wsAddress, wsAccessToken, object : OBMessage {

override suspend fun onGroupMessage(message: GroupMessage, json: String) {
this.setGroupBan(message.groupId, message.sender.userId, 1)
message.revoke(3)
message.reply("114514")
message.sender.ban(1)
message.sender.sendMessage("114514")
}

override suspend fun onGroupFileUpload(groupId: Long, userId: Long, file: FileEvent) {
Expand Down

0 comments on commit baec73e

Please sign in to comment.