-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathStarter.java
More file actions
241 lines (220 loc) · 8.23 KB
/
Starter.java
File metadata and controls
241 lines (220 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package io.github.kloping.qqbot;
import io.github.kloping.common.Public;
import io.github.kloping.judge.Judge;
import io.github.kloping.qqbot.entities.Bot;
import io.github.kloping.qqbot.impl.ListenerHost;
import io.github.kloping.qqbot.interfaces.FileUploadInterceptor;
import io.github.kloping.qqbot.network.Events;
import io.github.kloping.qqbot.network.WebSocketListener;
import io.github.kloping.qqbot.network.WssWorker;
import io.github.kloping.qqbot.utils.LoggerImpl;
import io.github.kloping.spt.StarterObjectApplication;
import io.github.kloping.spt.annotations.Entity;
import io.github.kloping.spt.interfaces.AutomaticWiringValue;
import io.github.kloping.spt.interfaces.component.ContextManager;
import io.github.kloping.spt.interfaces.component.HttpClientManager;
import lombok.Data;
import lombok.Getter;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;
import static io.github.kloping.spt.PartUtils.getExceptionLine;
/**
* <h3>一般启动方式</h3>
* <pre>{@code
* Starter starter = new Starter("appid", "token");
* starter.getConfig().setCode(Intents.PRIVATE_INTENTS.getCode());
* starter.run();
* }</pre>
* </pre>
* <h3>V1.4+ 注册监听器主机方式 [荐]</h3>
* <pre>{@code
*
* starter.registerListenerHost(new ListenerHost(){
* @Override
* public void handleException(Throwable e){
* }
*
* @EventReceiver
* public void onEvent(MessageChannelReceiveEvent event){
* event.send("测试");
* }
*
* @EventReceiver
* public void onEvent(MessageDirectReceiveEvent event){
* event.send("测试通过");
* }
* });
* }</pre>
* <p>
* 可通过
* <pre>{@code
* starter.setReconnect(true);
* }</pre>
* 设置是否在断开时是否重连 默认true
* <br>
* <br>
* <hr>
* <br>
* <h2>更多文档请待后续版本或 参考<a href="https://bot.q.qq.com/wiki/develop/api/">QQ频道官方文档</a></h2>
* <hr>
* <br>
*
* @author github.kloping
*/
public class Starter implements Runnable {
public static final String SANDBOX_NET_MAIN = "https://sandbox.api.sgroup.qq.com/";
public static final String NET_MAIN = "https://api.sgroup.qq.com/";
public String net = NET_MAIN;
public static final String NET_POINT = "{io.github.kloping.qqbot.Starter.net}";
public static final String APPID_ID = "appid";
public static final String TOKEN_ID = "token";
public static final String SECRET_ID = "secret";
public static final String AUTH_ID = "appid-token";
public static final String INTENTS_ID = "intents";
public static final String SHARD_ID = "shard";
public static final String PROPERTIES_ID = "properties";
public static final String MAIN_FUTURE_ID = "main_future";
public static final String RECONNECT_K_ID = "is_reconnect";
public static final String CONFIG_ID = "config";
@Getter
private Config config = new Config();
@Getter
private WssWorker wssWorker;
public final StarterObjectApplication APPLICATION = new StarterObjectApplication(Resource.class);
private ContextManager contextManager;
public Starter(String appid, String token) {
this(appid, token, null);
}
/**
* qq群使用必要构建方式
*
* @param appid
* @param token
* @param secret
*/
public Starter(String appid, String token, String secret) {
this.getConfig().setAppid(appid);
this.getConfig().setToken(token);
this.getConfig().setSecret(secret);
APPLICATION.logger = LoggerImpl.INSTANCE;
}
@Override
public void run() {
APPLICATION.PRE_SCAN_RUNNABLE.add(() -> {
APPLICATION.INSTANCE.getContextManager().append(APPLICATION.logger);
APPLICATION.INSTANCE.getContextManager().append(APPLICATION.INSTANCE);
APPLICATION.INSTANCE.getContextManager().append(getConfig(), CONFIG_ID);
});
APPLICATION.logger.setLogLevel(1);
APPLICATION.logger.setPrefix("[qgpd-bot]");
APPLICATION.run0(Start0.class);
after();
}
protected void after() {
String appid = getConfig().getAppid();
String token = getConfig().getToken();
String secret = getConfig().getSecret();
net = getConfig().sandbox ? SANDBOX_NET_MAIN : NET_MAIN;
contextManager = APPLICATION.INSTANCE.getContextManager();
contextManager.append(this);
contextManager.append(appid, APPID_ID);
contextManager.append(token, TOKEN_ID);
if (Judge.isNotEmpty(config.getSecret()))
contextManager.append(secret, SECRET_ID);
contextManager.append(getConfig().getCode(), INTENTS_ID);
contextManager.append(new Integer[]{0, 1}, SHARD_ID);
contextManager.append("Bot " + appid + "." + token, AUTH_ID);
contextManager.append(getConfig().getReconnect(), RECONNECT_K_ID);
wssWorker = contextManager.getContextEntity(WssWorker.class);
contextManager.getContextEntity(HttpClientManager.class).setPrint(false);
wssWork();
Resource.print();
}
protected void wssWork() {
AutomaticWiringValue automaticWiringValue = contextManager.getContextEntity(AutomaticWiringValue.class);
if (config.getWebSocketListener() != null) {
try {
automaticWiringValue.wiring(config.getWebSocketListener(), contextManager);
} catch (Exception e) {
APPLICATION.logger.error(e.getMessage() + "\n\tat " + getExceptionLine(e));
}
}
Future future = Public.EXECUTOR_SERVICE1.submit(wssWorker);
APPLICATION.INSTANCE.getContextManager().append(future, MAIN_FUTURE_ID);
}
public void setReconnect(Boolean reconnect) {
getConfig().setReconnect(reconnect);
}
public void registerListenerHost(ListenerHost listenerHost) {
getConfig().getListenerHosts().add(listenerHost);
}
/**
* 该类必须注解为 @{@link Entity}
* <br> 参考 {@link io.github.kloping.qqbot.impl.registers}
*
* @param cla
*/
public void registerEventsRegister(Class<? extends Events.EventRegister> cla) {
APPLICATION.POST_SCAN_RUNNABLE.add(() -> {
try {
APPLICATION.INSTANCE.getClassManager().add(cla);
} catch (Exception e) {
APPLICATION.logger.error("An error occurred in the registration class " + cla.getSimpleName());
APPLICATION.logger.error("\n\tat " + getExceptionLine(e));
}
});
}
@Data
public static class Config {
public boolean sandbox = false;
private String appid;
private String token;
/**
* 不使用v2群聊时可不设置
*/
private String secret;
/**
* code 从 {@link io.github.kloping.qqbot.api.Intents#getCode }
*/
private Integer code;
private Boolean reconnect = true;
private Boolean anyCloseReconnect = false;
private String wslink = null;
private Integer webhookport = 0;
/**
* webhook服务路径 默认/webhook0
*/
private String webhookpath = "/webhook0";
private Set<ListenerHost> listenerHosts = new HashSet<>();
private FileUploadInterceptor interceptor0;
private WebSocketListener webSocketListener;
/**
* 在沙箱环境与正式环境 之前切换 默认正式环境
*/
public void sandbox() {
sandbox = !sandbox;
}
/**
* 设置WebSocket链接地址 ##通常用于webhook转发 也可用于固定地址减少请求 加快启动速度
*
* @param wslink 配置后将不再通过/gateway请求获的地址(wss://api.sgroup.qq.com/websocket)
*/
public void setWslink(String wslink) {
this.wslink = wslink;
}
/**
* 设置webhook服务端口 默认为0时不开启webhook
* <br/>需要将q.qq配置链接设置为 "https://you-domain/webhook0" 路径
* <br/>若设置此项 则 配置项 code sandbox reconnect webhookport 可能会失效
*
* @param webhookport
*/
public void setWebhookport(Integer webhookport) {
this.webhookport = webhookport;
}
}
public Bot getBot() {
return contextManager.getContextEntity(Bot.class);
}
}