Skip to content

Commit

Permalink
发送消息时若目标用户或群不存在则抛出异常
Browse files Browse the repository at this point in the history
  • Loading branch information
kosaka-bun committed Jan 14, 2025
1 parent 787e627 commit b0f4b3f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import de.honoka.qqrobot.framework.config.MiraiProperties;
import de.honoka.qqrobot.framework.impl.mirai.component.MiraiEventListener;
import de.honoka.qqrobot.framework.impl.mirai.model.MiraiMessage;
import de.honoka.qqrobot.starter.common.NoContactException;
import de.honoka.qqrobot.starter.common.RobotMutedException;
import de.honoka.qqrobot.starter.config.RobotBasicProperties;
import de.honoka.sdk.util.file.FileUtils;
import de.honoka.sdk.util.text.TextUtils;
Expand Down Expand Up @@ -255,7 +257,9 @@ public void sendPrivateMsg(long qq, RobotMultipartMessage message) {
//查找此用户
Contact contact = getPrivateContact(qq);
//若不存在,不予发送
if(contact == null) return;
if(contact == null) {
throw new NoContactException(qq, null);
}
//发送消息
sendMessage(contact, typedTransform(null, qq, message));
}
Expand All @@ -264,9 +268,13 @@ public void sendPrivateMsg(long qq, RobotMultipartMessage message) {
public void sendGroupMsg(long group, RobotMultipartMessage message) {
//若群对象不存在,不予发送
Group groupObj = miraiApi.getGroup(group);
if(groupObj == null) return;
if(groupObj == null) {
throw new NoContactException(null, group);
}
//机器人在该群被禁言,不予发送
if(isMuted(group)) return;
if(isMuted(group)) {
throw new RobotMutedException(group);
}
//发送消息
sendMessage(groupObj, typedTransform(group, 0, message));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import de.honoka.qqrobot.framework.api.model.RobotMultipartMessage
import de.honoka.qqrobot.framework.config.OnebotProperties
import de.honoka.qqrobot.framework.impl.onebot.component.ContactManager
import de.honoka.qqrobot.framework.impl.onebot.model.OnebotMessage
import de.honoka.qqrobot.starter.common.NoContactException
import de.honoka.qqrobot.starter.common.RobotMutedException
import de.honoka.qqrobot.starter.util.GlobalThreadPools
import de.honoka.sdk.util.kotlin.basic.exception
import de.honoka.sdk.util.kotlin.basic.log
Expand Down Expand Up @@ -320,12 +322,13 @@ class OnebotFramework(
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import de.honoka.qqrobot.framework.impl.tester.model.TesterRobotMessage;
import de.honoka.qqrobot.framework.impl.tester.server.TesterServer;
import de.honoka.qqrobot.framework.impl.tester.server.TesterServerConnection;
import de.honoka.qqrobot.starter.common.NoContactException;
import de.honoka.qqrobot.starter.common.RobotMutedException;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -147,13 +149,13 @@ public void sendPrivateMsg(long qq, RobotMultipartMessage message) {
);
}
if(found) return;
throw new RuntimeException("User " + qq + " is not online.");
throw new NoContactException(qq, null);
}

@Override
public void sendGroupMsg(long group, RobotMultipartMessage message) {
if(testerServer.getConnections().isEmpty()) {
throw new RuntimeException("No users online.");
throw new RobotMutedException(group);
}
for(TesterServerConnection connection : testerServer.getConnections()) {
JSONObject data = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.honoka.qqrobot.starter.common

class NoContactException(qq: Long? = null, group: Long? = null) : RuntimeException(buildString {
qq?.let { append("qq: $qq") }
group?.let { append(", group: $group") }
})

class RobotMutedException(group: Long) : RuntimeException("group: $group")
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import de.honoka.qqrobot.starter.util.GlobalThreadPools;
import de.honoka.sdk.spring.starter.core.context.ConditionalComponent;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;

@Slf4j
@ConditionalComponent(ConditionalBeansConfig.class)
public class DefaultFrameworkCallback implements RobotFrameworkCallback {

Expand All @@ -31,7 +33,11 @@ public void onPrivateMsg(long qq, RobotMultipartMessage msg) {
if(reply != null) {
reply.removeEmptyPart();
if(reply.isEmpty()) return;
framework.reply(null, qq, reply);
try {
framework.reply(null, qq, reply);
} catch(Throwable t) {
log.error("", t);
}
}
});
}
Expand All @@ -49,7 +55,11 @@ public void onGroupMsg(long group, long qq, RobotMultipartMessage msg) {
if(reply != null) {
reply.removeEmptyPart();
if(reply.isEmpty()) return;
framework.reply(group, qq, reply);
try {
framework.reply(group, qq, reply);
} catch(Throwable t) {
log.error("", t);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class ExceptionReporter(

private fun doReport(t: Throwable) {
val cause = ExceptionUtil.getRootCause(t)
runCatching {
robotLogger.logException(cause)
}
if(!basicProperties.reportException) return
synchronized(this) {
val clazz = cause::class
if(exceptionCache.containsKey(clazz)) return
exceptionCache.put(clazz, Unit)
}
runCatching {
robotLogger.logException(cause)
}
if(!basicProperties.reportException) return
exceptionQueue.offer(cause)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ class RobotSession private constructor(
}

inline fun waitForReply(
prompt: String? = null,
prompt: String,
promptOnInvalidValue: String = "提供的参数有误,请重新输入",
resultPredicate: (String) -> Boolean = { true },
timeout: Int = 60
): String {
reply(prompt!!)
reply(prompt)
var result: String
while(true) {
try {
Expand Down

0 comments on commit b0f4b3f

Please sign in to comment.