Skip to content

Commit 525ab10

Browse files
committed
feat GroupOpRobotEvent;
Support logs do not output files
1 parent c25b766 commit 525ab10

18 files changed

+244
-20
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.kloping</groupId>
88
<artifactId>bot-qqpd-java</artifactId>
9-
<version>1.5.1-Beta1</version>
9+
<version>1.5.1-Beta2</version>
1010

1111
<packaging>jar</packaging>
1212
<name>bot-qqpd-java</name>

src/main/java/io/github/kloping/qqbot/api/v2/GroupEvent.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
/**
77
* @author github.kloping
88
*/
9-
public interface GroupEvent extends Event {
9+
public interface GroupEvent extends Event, V2Event {
1010
Group getGroup();
1111

1212
String getGroupId();
13+
14+
default String getGroupOpenId() {
15+
return getGroupId();
16+
}
1317
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.kloping.qqbot.api.v2;
2+
3+
/**
4+
* @author github.kloping
5+
*/
6+
public interface GroupOpRobotEvent extends GroupEvent {
7+
String getOpMemberOpenid();
8+
9+
Long getTimestamp();
10+
}

src/main/java/io/github/kloping/qqbot/api/v2/MessageV2Event.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @author github.kloping
1212
*/
13-
public interface MessageV2Event extends Event, Sender {
13+
public interface MessageV2Event extends Event, Sender, V2Event {
1414
/**
1515
* 发送纯文本
1616
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.kloping.qqbot.api.v2;
2+
3+
/**
4+
* 机器人群聊事件
5+
*
6+
* @author github.kloping
7+
*/
8+
public interface V2Event {
9+
/**
10+
* 事件id
11+
*
12+
* @return
13+
*/
14+
String getId();
15+
16+
/**
17+
* 事情群聊openid
18+
*
19+
* @return
20+
*/
21+
String getGroupOpenId();
22+
}

src/main/java/io/github/kloping/qqbot/entities/Pack.java

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class Pack {
1616
private Number s;
1717
private Object d;
1818
private String t;
19+
private String id;
1920

2021
public <T> T dAsMapGet(Object key, Class<T> cla) {
2122
if (d instanceof Map) {

src/main/java/io/github/kloping/qqbot/entities/ex/Image.java

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public Result send(SenderAndCidMidGetter er) {
9999
if (Judge.isNotEmpty(er.getMid())) data.setMsg_id(er.getMid());
100100
data.setMedia(new V2MsgData.Media(result.getFile_info()));
101101
data.setMsg_seq(v2.getMsgSeq());
102-
103102
return new Result<V2Result>(v2.getV2().send(er.getCid(), data.toString(), SEND_MESSAGE_HEADERS));
104103
}
105104
}

src/main/java/io/github/kloping/qqbot/entities/ex/MessageAsyncBuilder.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class MessageAsyncBuilder implements Builder<SendAble, SendAble> {
1515

1616
private final MessageChain chain = new MessageChain();
1717

18-
1918
@Override
2019
public MessageAsyncBuilder append(SendAble sendAble) {
2120
chain.append(sendAble);
@@ -47,4 +46,11 @@ public MessageAsyncBuilder image(byte[] bytes) {
4746
public MessageAsyncBuilder text(String text) {
4847
return append(new PlainText(text));
4948
}
49+
50+
/**
51+
* @return
52+
*/
53+
public boolean isEmpty() {
54+
return chain.isEmpty();
55+
}
5056
}

src/main/java/io/github/kloping/qqbot/entities/ex/msg/MessageChain.java

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public Result send(SenderAndCidMidGetter er) {
137137
} else if (e0 instanceof Markdown) {
138138
result = ((Markdown) e0).send(er, v2.getMsgSeq());
139139
sent++;
140+
} else if (e0 instanceof Keyboard) {
141+
data.setKeyboard(e0);
142+
data.setMsg_type(2);
140143
} else {
141144
data.setContent(data.getContent() + e0.toString());
142145
}

src/main/java/io/github/kloping/qqbot/impl/BaseConnectedEvent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public String getClassName() {
4141

4242
@Override
4343
public String toString() {
44-
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.1-Beta1", bot.getConfig().getAppid());
44+
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.1-Beta2", bot.getConfig().getAppid());
4545
}
4646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.kloping.qqbot.impl.message.v2;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import io.github.kloping.qqbot.api.v2.GroupEvent;
5+
import io.github.kloping.qqbot.api.v2.GroupOpRobotEvent;
6+
import io.github.kloping.qqbot.entities.Bot;
7+
import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
8+
9+
/**
10+
* @author github.kloping
11+
*/
12+
public class BaseGroupAddRobotEvent extends BaseGroupOpRobotEvent implements GroupOpRobotEvent {
13+
public BaseGroupAddRobotEvent(RawMessage message, JSONObject jo, Bot bot) {
14+
super(message, jo, bot);
15+
}
16+
17+
@Override
18+
public String getClassName() {
19+
return "GroupAddRobotEvent";
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.kloping.qqbot.impl.message.v2;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import io.github.kloping.qqbot.api.v2.GroupOpRobotEvent;
5+
import io.github.kloping.qqbot.entities.Bot;
6+
import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
7+
8+
/**
9+
* @author github.kloping
10+
*/
11+
public class BaseGroupDelRobotEvent extends BaseGroupOpRobotEvent implements GroupOpRobotEvent {
12+
public BaseGroupDelRobotEvent(RawMessage message, JSONObject jo, Bot bot) {
13+
super(message, jo, bot);
14+
}
15+
16+
@Override
17+
public String getClassName() {
18+
return "GroupDelRobotEvent";
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.kloping.qqbot.impl.message.v2;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import io.github.kloping.qqbot.api.v2.GroupEvent;
5+
import io.github.kloping.qqbot.entities.Bot;
6+
import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
7+
import io.github.kloping.qqbot.entities.qqpd.v2.Group;
8+
import io.github.kloping.qqbot.network.Events;
9+
10+
/**
11+
* @author github.kloping
12+
*/
13+
public abstract class BaseGroupEvent implements GroupEvent {
14+
15+
private final Group group;
16+
private final JSONObject rawData;
17+
private final Bot bot;
18+
19+
public BaseGroupEvent(RawMessage message, JSONObject jo, Bot bot) {
20+
this.rawData = jo;
21+
this.bot = bot;
22+
this.group = new Group(getMetadata());
23+
}
24+
25+
@Override
26+
public JSONObject getMetadata() {
27+
return rawData;
28+
}
29+
30+
@Override
31+
public Bot getBot() {
32+
return bot;
33+
}
34+
35+
@Override
36+
public Group getGroup() {
37+
return group;
38+
}
39+
40+
@Override
41+
public String getGroupId() {
42+
return getGroup().getOpenid();
43+
}
44+
45+
@Override
46+
public String getId() {
47+
return rawData.getString(Events.EXTEND_ID);
48+
}
49+
}

src/main/java/io/github/kloping/qqbot/impl/message/v2/BaseGroupMessageEvent.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.github.kloping.qqbot.http.data.Result;
1717
import io.github.kloping.qqbot.http.data.V2MsgData;
1818
import io.github.kloping.qqbot.http.data.V2Result;
19+
import io.github.kloping.qqbot.network.Events;
1920
import lombok.Getter;
2021
import lombok.Setter;
2122

@@ -49,6 +50,11 @@ public BaseGroupMessageEvent(RawMessage message, JSONObject jo, Bot bot) {
4950
this.sender.setBot(bot);
5051
}
5152

53+
@Override
54+
public String getId() {
55+
return metadata.getString(Events.EXTEND_ID);
56+
}
57+
5258
@Override
5359
public String getGroupId() {
5460
return getSubject().getId();
@@ -118,7 +124,6 @@ public Integer getMsgSeq() {
118124
return seq++;
119125
}
120126

121-
122127
@Override
123128
public String toString() {
124129
return String.format("[type(%s) %s].%s:%s"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.kloping.qqbot.impl.message.v2;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import io.github.kloping.qqbot.api.v2.GroupOpRobotEvent;
5+
import io.github.kloping.qqbot.entities.Bot;
6+
import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
7+
8+
/**
9+
* @author github.kloping
10+
*/
11+
public abstract class BaseGroupOpRobotEvent extends BaseGroupEvent implements GroupOpRobotEvent {
12+
public BaseGroupOpRobotEvent(RawMessage message, JSONObject jo, Bot bot) {
13+
super(message, jo, bot);
14+
}
15+
16+
@Override
17+
public String getOpMemberOpenid() {
18+
return getMetadata().getString("op_member_openid");
19+
}
20+
21+
@Override
22+
public Long getTimestamp() {
23+
return getMetadata().getLong("timestamp");
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return String.format("op robot in %s from %s", getOpMemberOpenid(), getGroupId());
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.kloping.qqbot.impl.registers;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import io.github.kloping.MySpringTool.annotations.AutoStand;
5+
import io.github.kloping.MySpringTool.annotations.AutoStandAfter;
6+
import io.github.kloping.MySpringTool.annotations.Entity;
7+
import io.github.kloping.qqbot.api.event.Event;
8+
import io.github.kloping.qqbot.entities.Bot;
9+
import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
10+
import io.github.kloping.qqbot.impl.message.v2.BaseGroupAddRobotEvent;
11+
import io.github.kloping.qqbot.impl.message.v2.BaseGroupDelRobotEvent;
12+
import io.github.kloping.qqbot.network.Events;
13+
14+
/**
15+
* @author github.kloping
16+
*/
17+
@Entity
18+
public class GroupRobotEventRegister implements Events.EventRegister {
19+
20+
public static final String GROUP_DEL_ROBOT = "GROUP_DEL_ROBOT";
21+
public static final String GROUP_ADD_ROBOT = "GROUP_ADD_ROBOT";
22+
23+
@AutoStandAfter
24+
private void r0(Events events) {
25+
events.register(GROUP_ADD_ROBOT, this)
26+
.register(GROUP_DEL_ROBOT, this);
27+
}
28+
29+
@AutoStand
30+
Bot bot;
31+
32+
@Override
33+
public Event handle(String t, JSONObject mateData, RawMessage message) {
34+
Event event = null;
35+
if (GROUP_ADD_ROBOT.equals(t)) {
36+
event = new BaseGroupAddRobotEvent(message, mateData, bot);
37+
} else if (GROUP_DEL_ROBOT.equals(t)) {
38+
event = new BaseGroupDelRobotEvent(message, mateData, bot);
39+
}
40+
return event;
41+
}
42+
43+
}

src/main/java/io/github/kloping/qqbot/network/Events.java

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
@Entity
3434
public class Events implements OnPackReceive {
35+
public static final String EXTEND_ID = "raw-id";
36+
3537
@AutoStandAfter
3638
public void r1(WssWorker wssWorker) {
3739
wssWorker.getOnPackReceives().add(this);
@@ -42,6 +44,7 @@ public boolean onReceive(Pack pack) {
4244
String t = pack.getT();
4345
if (t == null) return false;
4446
JSONObject jo = JSON.parseObject(JSON.toJSONString(pack.getD()));
47+
jo.put(EXTEND_ID, pack.getId());
4548
Public.EXECUTOR_SERVICE.submit(() -> {
4649
try {
4750
onEvent(t, jo);

src/main/java/io/github/kloping/qqbot/utils/LoggerImpl.java

+21-13
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ private void step0() {
6262
file = new File(String.format(logFileDir, dfn.format(new Date())));
6363
}
6464

65+
/**
66+
* 必须设置为 %s 以替换 日期的字符串格式 默认 "./logs/%s.log"
67+
* 设置为 null 时不输出日志文件
68+
*
69+
* @param path
70+
*/
6571
@Override
6672
public void setOutFile(String path) {
67-
error("The log file cannot be customized now!");
73+
this.logFileDir = path;
6874
}
6975

7076
@Override
@@ -108,20 +114,22 @@ public void Log(String mess, Integer level) {
108114
if (level != -1 && level < logLevel) {
109115
} else e.printStackTrace();
110116
}
111-
try {
112-
BufferedWriter writer = getWriter();
113-
if (writer != null) {
114-
try {
115-
log = log.replaceAll("\\\u001B\\[38\\;2\\;[0-9]+\\;[0-9]+\\;[0-9]+m", "")
116-
.replaceAll("\\\u001B\\[m", "");
117-
} catch (Exception e) {
117+
if (logFileDir != null) {
118+
try {
119+
BufferedWriter writer = getWriter();
120+
if (writer != null) {
121+
try {
122+
log = log.replaceAll("\\\u001B\\[38\\;2\\;[0-9]+\\;[0-9]+\\;[0-9]+m", "")
123+
.replaceAll("\\\u001B\\[m", "");
124+
} catch (Exception e) {
125+
}
126+
writer.write(log);
127+
writer.newLine();
128+
writer.flush();
118129
}
119-
writer.write(log);
120-
writer.newLine();
121-
writer.flush();
130+
} catch (IOException e) {
131+
e.printStackTrace();
122132
}
123-
} catch (IOException e) {
124-
e.printStackTrace();
125133
}
126134
if (level != -1 && level < logLevel) return;
127135
System.out.println(out);

0 commit comments

Comments
 (0)