feat(bot): 增加定时清理功能并优化消息发送逻辑- 在 AbsGPTManager 中添加定时任务调度功能,用于定期清理资源

- 修改 BaiduGPT 类中的消息发送逻辑,增加定时清理调度
- 更新 BaiduGPTManager 中的 sendMessage 方法,添加用户锁注释
- 升级 qianfan 依赖至0.1.4 版本
- 更新 QQBot 版本号至 1.7.14
This commit is contained in:
Yutou 2025-02-19 10:24:13 +08:00
parent 5d7ec8e67d
commit 9ab99b22e2
5 changed files with 29 additions and 4 deletions

View File

@ -167,7 +167,7 @@
<dependency> <dependency>
<groupId>com.baidubce</groupId> <groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId> <artifactId>qianfan</artifactId>
<version>0.1.3</version> <version>0.1.4</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency> <dependency>

View File

@ -10,7 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class QQBotApplication { public class QQBotApplication {
public static final String version = "QQBot v.1.7.13"; public static final String version = "QQBot v.1.7.14";
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("version = " + version); System.out.println("version = " + version);

View File

@ -6,11 +6,13 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public abstract class AbsGPTManager { public abstract class AbsGPTManager {
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private ScheduledFuture<?> currentTask;
protected static final AtomicInteger MAX_MESSAGE = new AtomicInteger(20); protected static final AtomicInteger MAX_MESSAGE = new AtomicInteger(20);
protected final ConcurrentHashMap<String, List<Message>> msgMap= new ConcurrentHashMap<>(); protected final ConcurrentHashMap<String, List<Message>> msgMap= new ConcurrentHashMap<>();
// 新增锁映射表 // 新增锁映射表
@ -135,4 +137,16 @@ public abstract class AbsGPTManager {
this.model=model; this.model=model;
return this; return this;
} }
public void sendMessageAndScheduleClear() {
// 取消当前的定时任务如果存在
if (currentTask != null && !currentTask.isCancelled()) {
currentTask.cancel(false);
}
// 重新调度一个新的定时任务一小时后执行
currentTask = scheduler.schedule(() -> {
clear();
currentTask = null; // 清空当前任务引用
}, 1, TimeUnit.HOURS);
}
} }

View File

@ -79,7 +79,15 @@ public class BaiduGPTManager extends AbsGPTManager {
.execute(); .execute();
System.out.println("输出内容:" + response.getResult()); System.out.println("输出内容:" + response.getResult());
} }
/**
* 发送消息方法
* 该方法用于处理用户发送的消息并返回相应的回复消息
* 它通过用户锁来限制每个用户同时只能有一个请求正在处理中
*
* @param user 用户标识符用于区分不同的用户
* @param message 用户发送的消息内容
* @return 返回处理后的消息对象包含回复内容和是否为回复消息的标记
*/
@Override @Override
public Message sendMessage(String user, String message) { public Message sendMessage(String user, String message) {
// 获取或创建用户锁 // 获取或创建用户锁

View File

@ -13,6 +13,7 @@ import com.yutou.qqbot.gpt.BaiduGPTManager;
import com.yutou.napcat.event.MessageEvent; import com.yutou.napcat.event.MessageEvent;
import com.yutou.qqbot.utlis.ConfigTools; import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.HttpTools; import com.yutou.qqbot.utlis.HttpTools;
import com.yutou.qqbot.utlis.Log;
import com.yutou.qqbot.utlis.StringUtils; import com.yutou.qqbot.utlis.StringUtils;
import lombok.val; import lombok.val;
@ -70,6 +71,7 @@ public class BaiduGPT extends Model {
} }
if (event.getTextMessage().contains("画画")) { if (event.getTextMessage().contains("画画")) {
val file = BaiduGPTManager.getManager().textToImage(String.valueOf(qq), event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").replace("画画", "").trim()); val file = BaiduGPTManager.getManager().textToImage(String.valueOf(qq), event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").replace("画画", "").trim());
Log.i("画图",event.getTextMessage());
if (file == null) { if (file == null) {
QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text("画不出")); QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text("画不出"));
} else { } else {
@ -123,6 +125,7 @@ public class BaiduGPT extends Model {
"\n"+ "\n"+
message.getContent(); message.getContent();
QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text(sb)); QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text(sb));
AbsGPTManager.getManager(gptManager).sendMessageAndScheduleClear();
} }
} }