Skip to content

Commit

Permalink
去除RobotFramework类的类型参数
Browse files Browse the repository at this point in the history
  • Loading branch information
kosaka-bun committed Jan 4, 2025
1 parent 4a8e1d6 commit 0cd14fe
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

/**
* 机器人服务框架必须提供的方法
* @param <M> 表示平台的消息格式
*/
public interface RobotFramework<M> {
public interface RobotFramework {

/**
* 启动框架
Expand All @@ -28,12 +27,12 @@ public interface RobotFramework<M> {
/**
* 将统一消息格式转换为平台消息格式(group和qq参数用于提供给需要提前上传文件的框架)
*/
M transform(Long group, long qq, RobotMultipartMessage message);
Object transform(Long group, long qq, RobotMultipartMessage message);

/**
* 使平台支持的多部分消息转换为统一消息格式
*/
RobotMultipartMessage transform(M message);
RobotMultipartMessage transform(Object message);

/**
* 向指定QQ发送一条私聊消息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import java.util.Objects;

public abstract class BaseFramework<M> implements RobotFramework<M> {
public abstract class AbstractRobotFramework<M> implements RobotFramework {

@Resource
protected RobotBasicProperties basicProperties;
Expand All @@ -18,6 +18,21 @@ public abstract class BaseFramework<M> implements RobotFramework<M> {
@Resource
protected RobotFrameworkCallback frameworkCallback;

public abstract M typedTransform(Long group, long qq, RobotMultipartMessage message);

public abstract RobotMultipartMessage typedTransform(M message);

@Override
public final Object transform(Long group, long qq, RobotMultipartMessage message) {
return typedTransform(group, qq, message);
}

@SuppressWarnings("unchecked")
@Override
public final RobotMultipartMessage transform(Object message) {
return typedTransform((M) message);
}

public void sendMsgToAdmin(RobotMultipartMessage message) {
long qq = Objects.requireNonNull(basicProperties.getAdminQq());
sendPrivateMsg(qq, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import de.honoka.qqrobot.framework.BaseFramework;
import de.honoka.qqrobot.framework.AbstractRobotFramework;
import de.honoka.qqrobot.framework.api.model.RobotMessage;
import de.honoka.qqrobot.framework.api.model.RobotMessageType;
import de.honoka.qqrobot.framework.api.model.RobotMultipartMessage;
Expand Down Expand Up @@ -50,7 +50,7 @@
@Slf4j
@Getter
@Component
public class MiraiFramework extends BaseFramework<MiraiMessage> {
public class MiraiFramework extends AbstractRobotFramework<MiraiMessage> {

@Resource
private MiraiProperties miraiProperties;
Expand Down Expand Up @@ -190,7 +190,7 @@ public synchronized void checkIsActive() {

@SneakyThrows
@Override
public MiraiMessage transform(Long group, long qq, RobotMultipartMessage message) {
public MiraiMessage typedTransform(Long group, long qq, RobotMultipartMessage message) {
if(message == null || message.isEmpty()) return null;
MessageChainBuilder builder = new MessageChainBuilder();
List<ExternalResource> externalResources = new ArrayList<>();
Expand Down Expand Up @@ -236,7 +236,7 @@ public MiraiMessage transform(Long group, long qq, RobotMultipartMessage message
}

@Override
public RobotMultipartMessage transform(MiraiMessage message) {
public RobotMultipartMessage typedTransform(MiraiMessage message) {
MessageChain miraiMultiPartMsg = message.getMessageChain();
RobotMultipartMessage multipartMessage = new RobotMultipartMessage();
for(SingleMessage sm : miraiMultiPartMsg) {
Expand All @@ -256,7 +256,7 @@ public void sendPrivateMsg(long qq, RobotMultipartMessage message) {
//若不存在,不予发送
if(contact == null) return;
//发送消息
MiraiMessage msgAndRes = transform(null, qq, message);
MiraiMessage msgAndRes = typedTransform(null, qq, message);
sendMessage(contact, msgAndRes);
}

Expand All @@ -268,7 +268,7 @@ public void sendGroupMsg(long group, RobotMultipartMessage message) {
//机器人在该群被禁言,不予发送
if(isMuted(group)) return;
//发送消息
MiraiMessage msgAndRes = transform(group, 0, message);
MiraiMessage msgAndRes = typedTransform(group, 0, message);
sendMessage(groupObj, msgAndRes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void onGroupMessage(GroupMessageEvent e) {
miraiFramework.getFrameworkCallback().onGroupMsg(
e.getGroup().getId(),
e.getSender().getId(),
miraiFramework.transform(new MiraiMessage(e.getMessage()))
miraiFramework.typedTransform(new MiraiMessage(e.getMessage()))
);
}

Expand All @@ -88,7 +88,7 @@ public void onFriendMessage(FriendMessageEvent e) {
private void onUserMessage(UserMessageEvent e) {
miraiFramework.getFrameworkCallback().onPrivateMsg(
e.getSender().getId(),
miraiFramework.transform(new MiraiMessage(e.getMessage()))
miraiFramework.typedTransform(new MiraiMessage(e.getMessage()))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import cn.hutool.http.HttpUtil
import cn.hutool.json.JSONArray
import cn.hutool.json.JSONObject
import cn.hutool.json.JSONUtil
import de.honoka.qqrobot.framework.BaseFramework
import de.honoka.qqrobot.framework.AbstractRobotFramework
import de.honoka.qqrobot.framework.api.model.RobotMessageType.*
import de.honoka.qqrobot.framework.api.model.RobotMultipartMessage
import de.honoka.qqrobot.framework.config.OnebotProperties
Expand Down Expand Up @@ -35,7 +35,7 @@ import kotlin.io.path.Path
class OnebotFramework(
private val onebotProperties: OnebotProperties,
private val contactManager: ContactManager
) : BaseFramework<OnebotMessage>() {
) : AbstractRobotFramework<OnebotMessage>() {

companion object {

Expand Down Expand Up @@ -81,7 +81,7 @@ class OnebotFramework(
val robotMessage = json["message"].let {
when(it) {
is String -> RobotMultipartMessage.of(it)
is JSONArray -> transform(OnebotMessage(it))
is JSONArray -> typedTransform(OnebotMessage(it))
else -> throw Exception("Unknown message type")
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ class OnebotFramework(
}

//group与qq参数均未使用
override fun transform(group: Long?, qq: Long, message: RobotMultipartMessage): OnebotMessage {
override fun typedTransform(group: Long?, qq: Long, message: RobotMultipartMessage): OnebotMessage {
val onebotMessage = OnebotMessage()
val remoteMode = onebotProperties.fileReceiverPort != null
message.messageList.forEach {
Expand Down Expand Up @@ -275,9 +275,11 @@ class OnebotFramework(
return onebotMessage
}

private fun transform(message: RobotMultipartMessage) = transform(null, 0, message)
private fun typedTransform(message: RobotMultipartMessage): OnebotMessage = run {
typedTransform(null, 0, message)
}

override fun transform(onebotMessage: OnebotMessage): RobotMultipartMessage {
override fun typedTransform(onebotMessage: OnebotMessage): RobotMultipartMessage {
val message = RobotMultipartMessage()
onebotMessage.parts.forEach {
when(it.type) {
Expand Down Expand Up @@ -318,12 +320,12 @@ class OnebotFramework(

override fun sendPrivateMsg(qq: Long, message: RobotMultipartMessage) {
val contact = contactManager.searchContact(qq) ?: return
sendMessage(contact[0], qq, transform(message))
sendMessage(contact[0], qq, typedTransform(message))
}

override fun sendGroupMsg(group: Long, message: RobotMultipartMessage) {
if(!contactManager.containsGroup(group) || isMuted(group)) return
sendMessage(group, null, transform(message))
sendMessage(group, null, typedTransform(message))
}

private fun sendMessage(group: Long?, qq: Long?, message: OnebotMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONObject;
import de.honoka.qqrobot.framework.BaseFramework;
import de.honoka.qqrobot.framework.AbstractRobotFramework;
import de.honoka.qqrobot.framework.api.model.RobotMessage;
import de.honoka.qqrobot.framework.api.model.RobotMessageType;
import de.honoka.qqrobot.framework.api.model.RobotMultipartMessage;
Expand Down Expand Up @@ -32,7 +32,7 @@
@Slf4j
@Getter
@Component
public class TesterFramework extends BaseFramework<TesterRobotMessage> {
public class TesterFramework extends AbstractRobotFramework<TesterRobotMessage> {

@Resource
private TesterProperties testerProperties;
Expand Down Expand Up @@ -75,7 +75,7 @@ public void reboot() {

@SneakyThrows
@Override
public TesterRobotMessage transform(Long group, long qq, RobotMultipartMessage message) {
public TesterRobotMessage typedTransform(Long group, long qq, RobotMultipartMessage message) {
if(message == null || message.isEmpty()) return null;
TesterRobotMessage testerRobotMessage = new TesterRobotMessage();
for(RobotMessage<?> part : message.messageList) {
Expand Down Expand Up @@ -118,7 +118,7 @@ public TesterRobotMessage transform(Long group, long qq, RobotMultipartMessage m
}

@Override
public RobotMultipartMessage transform(TesterRobotMessage message) {
public RobotMultipartMessage typedTransform(TesterRobotMessage message) {
RobotMultipartMessage multipartMessage = new RobotMultipartMessage();
for(TesterRobotMessage.Part part : message.getParts()) {
if(part.getType().equals(TesterRobotMessage.PartType.AT)) {
Expand All @@ -137,7 +137,7 @@ public void sendPrivateMsg(long qq, RobotMultipartMessage message) {
if(qqOfConnection != qq) continue;
JSONObject data = new JSONObject();
data.set("name", "Robot");
data.set("content", transform(null, qq, message).toJsonArray());
data.set("content", typedTransform(null, qq, message).toJsonArray());
connection.sendMessage(
new TesterMessage(null)
.setType(TesterMessageType.PRIVATE_MESSAGE)
Expand All @@ -151,7 +151,7 @@ public void sendGroupMsg(long group, RobotMultipartMessage message) {
for(TesterServerConnection connection : testerServer.getConnections()) {
JSONObject data = new JSONObject();
data.set("name", "Robot");
data.set("content", transform(group, 0, message).toJsonArray());
data.set("content", typedTransform(group, 0, message).toJsonArray());
connection.sendMessage(
new TesterMessage(null)
.setType(TesterMessageType.GROUP_MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TesterServer {

@Lazy
@Resource
private RobotFramework<?> framework;
private RobotFramework framework;

@Resource
private TesterProperties testerProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import de.honoka.qqrobot.framework.BaseFramework;
import de.honoka.qqrobot.framework.AbstractRobotFramework;
import de.honoka.qqrobot.framework.config.TesterProperties;
import de.honoka.qqrobot.framework.impl.tester.model.TesterMessage;
import de.honoka.qqrobot.framework.impl.tester.model.TesterMessageType;
Expand Down Expand Up @@ -175,11 +175,11 @@ private void onGroupMessage(TesterMessage message) {
}
});
//处理消息
BaseFramework<TesterRobotMessage> framework = (BaseFramework<TesterRobotMessage>) testerServer.getFramework();
AbstractRobotFramework<TesterRobotMessage> framework = (AbstractRobotFramework<TesterRobotMessage>) testerServer.getFramework();
framework.getFrameworkCallback().onGroupMsg(
testerServer.getTesterProperties().getGroupNumber(),
data.getLong("qq"),
framework.transform(TesterRobotMessage.of(content))
framework.typedTransform(TesterRobotMessage.of(content))
);
}

Expand All @@ -195,10 +195,10 @@ private void onPrivateMessage(TesterMessage message) {
);
//处理消息
JSONArray content = message.getData().getJSONArray("content");
BaseFramework<TesterRobotMessage> framework = (BaseFramework<TesterRobotMessage>) testerServer.getFramework();
AbstractRobotFramework<TesterRobotMessage> framework = (AbstractRobotFramework<TesterRobotMessage>) testerServer.getFramework();
framework.getFrameworkCallback().onPrivateMsg(
data.getLong("qq"),
framework.transform(TesterRobotMessage.of(content))
framework.typedTransform(TesterRobotMessage.of(content))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DefaultFrameworkCallback implements RobotFrameworkCallback {

@Lazy
@Resource
private RobotFramework<?> framework;
private RobotFramework framework;

/**
* 收到私聊消息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ExceptionReporter {

@Lazy
@Resource
private RobotFramework<?> framework;
private RobotFramework framework;

/**
* 异常信息记录表,只记录第一行
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.system.exitProcess
@Component
class RobotApplicationRunner(
private val basicProperties: RobotBasicProperties,
private val framework: RobotFramework<*>
private val framework: RobotFramework
) : ApplicationRunner {

override fun run(args: ApplicationArguments) {
Expand All @@ -32,4 +32,4 @@ class RobotApplicationRunner(
throw it
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void create() {
//启动应用
startApplication();
//添加托盘图标菜单项
RobotFramework<?> framework = context.getBean(RobotFramework.class);
RobotFramework framework = context.getBean(RobotFramework.class);
RobotBasicProperties basicProperties = context.getBean(RobotBasicProperties.class);
consoleWindow.addTrayIconMenuItem("重新登录", true, framework::reboot);
consoleWindow.addTrayIconMenuItem("发送测试消息", false, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class AdminApiController {

@Lazy
@Resource
private RobotFramework<?> framework;
private RobotFramework framework;

//endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DefaultRobotLogger implements RobotLogger {

@Lazy
@Resource
private RobotFramework<?> framework;
private RobotFramework framework;

@Resource
private ExceptionRecordDao exceptionRecordDao;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SessionManager {
@Getter(AccessLevel.PACKAGE)
@Lazy
@Resource
private RobotFramework<?> framework;
private RobotFramework framework;

/**
* 当前会话列表
Expand Down

0 comments on commit 0cd14fe

Please sign in to comment.