|
1 | 1 | package com.ashin.handler; |
2 | 2 |
|
| 3 | +import com.ashin.config.KeywordConfig; |
3 | 4 | import com.ashin.config.QqConfig; |
4 | 5 | import com.ashin.entity.bo.ChatBO; |
5 | 6 | import com.ashin.exception.ChatException; |
6 | 7 | import com.ashin.service.InteractService; |
7 | 8 | import com.ashin.util.BotUtil; |
| 9 | +import com.ashin.util.ImageUtil; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import net.mamoe.mirai.contact.Contact; |
8 | 12 | import net.mamoe.mirai.contact.MessageTooLargeException; |
9 | 13 | import net.mamoe.mirai.event.EventHandler; |
10 | 14 | import net.mamoe.mirai.event.ListenerHost; |
11 | 15 | import net.mamoe.mirai.event.events.*; |
12 | | -import net.mamoe.mirai.message.data.At; |
13 | | -import net.mamoe.mirai.message.data.MessageChain; |
14 | | -import net.mamoe.mirai.message.data.MessageChainBuilder; |
15 | | -import net.mamoe.mirai.message.data.QuoteReply; |
| 16 | +import net.mamoe.mirai.message.data.*; |
16 | 17 | import org.jetbrains.annotations.NotNull; |
17 | 18 | import org.springframework.stereotype.Component; |
18 | 19 | import javax.annotation.Resource; |
| 20 | +import java.io.File; |
19 | 21 |
|
20 | 22 | /** |
21 | 23 | * QQ消息处理程序 |
|
24 | 26 | * @date 2023/2/1 |
25 | 27 | */ |
26 | 28 | @Component |
| 29 | +@Slf4j |
27 | 30 | public class QqMessageHandler implements ListenerHost { |
28 | 31 | @Resource |
29 | 32 | private InteractService interactService; |
30 | 33 | @Resource |
31 | 34 | private QqConfig qqConfig; |
32 | 35 | @Resource |
| 36 | + private KeywordConfig keywordConfig; |
| 37 | + @Resource |
33 | 38 | private BotUtil botUtil; |
34 | 39 |
|
35 | 40 | /** |
@@ -62,24 +67,33 @@ public void onGroupMessageEvent(GroupMessageEvent event){ |
62 | 67 | } |
63 | 68 | } |
64 | 69 | private void response(@NotNull MessageEvent event, ChatBO chatBO, String prompt) { |
65 | | - if (qqConfig.getResetWord().equals(prompt)) { |
| 70 | + if (keywordConfig.getReset().equals(prompt)) { |
66 | 71 | //检测到重置会话指令 |
67 | 72 | botUtil.resetPrompt(chatBO.getSessionId()); |
68 | 73 | event.getSubject().sendMessage("重置会话成功"); |
69 | 74 | } else { |
70 | 75 | String response; |
71 | 76 | try { |
72 | 77 | chatBO.setPrompt(prompt); |
| 78 | + chatBO.setAiDraw(prompt.startsWith(keywordConfig.getDraw())); |
73 | 79 | response = interactService.chat(chatBO); |
74 | 80 | }catch (ChatException e){ |
75 | 81 | response = e.getMessage(); |
76 | 82 | } |
77 | 83 | try { |
78 | | - MessageChain messages = new MessageChainBuilder() |
79 | | - .append(new QuoteReply(event.getMessage())) |
80 | | - .append(response) |
81 | | - .build(); |
82 | | - event.getSubject().sendMessage(messages); |
| 84 | + if (chatBO.isAiDraw() && !qqConfig.getReturnDrawByURL()){ |
| 85 | + File file = ImageUtil.download(response); |
| 86 | + Contact.sendImage(event.getSubject(), file); |
| 87 | + if (!file.delete()){ |
| 88 | + log.warn("图片({})删除失败, 请注意存储空间", file.getAbsolutePath()); |
| 89 | + } |
| 90 | + }else { |
| 91 | + MessageChain messages = new MessageChainBuilder() |
| 92 | + .append(new QuoteReply(event.getMessage())) |
| 93 | + .append(response) |
| 94 | + .build(); |
| 95 | + event.getSubject().sendMessage(messages); |
| 96 | + } |
83 | 97 | }catch (MessageTooLargeException e){ |
84 | 98 | //信息太大,无法引用,采用直接回复 |
85 | 99 | event.getSubject().sendMessage(response); |
|
0 commit comments