woodenfish-bot 5.0.1 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/index.js +177 -68
- package/lib/index.js +178 -68
- package/package.json +1 -1
- package/typings/index.d.ts +26 -15
package/es/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
|
|
2
|
+
import _regeneratorRuntime from '@babel/runtime/regenerator';
|
|
2
3
|
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
3
4
|
import _createClass from '@babel/runtime/helpers/createClass';
|
|
4
5
|
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
5
|
-
import _regeneratorRuntime from '@babel/runtime/regenerator';
|
|
6
6
|
import resty from 'resty-client';
|
|
7
7
|
import _typeof from '@babel/runtime/helpers/typeof';
|
|
8
8
|
import https from 'node:https';
|
|
@@ -282,7 +282,7 @@ var Guild = /*#__PURE__*/function () {
|
|
|
282
282
|
}]);
|
|
283
283
|
}();
|
|
284
284
|
|
|
285
|
-
var version = "5.0.
|
|
285
|
+
var version = "5.0.2";
|
|
286
286
|
|
|
287
287
|
function getDefaultExportFromCjs (x) {
|
|
288
288
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
@@ -681,10 +681,13 @@ var addUserAgent = function addUserAgent(header) {
|
|
|
681
681
|
header['User-Agent'] = "BotNodeSDK/v".concat(version);
|
|
682
682
|
};
|
|
683
683
|
var addV1Authorization = function addV1Authorization(header, appID, token) {
|
|
684
|
+
header['Authorization'] = getV1Authorization(appID, token);
|
|
685
|
+
};
|
|
686
|
+
var getV1Authorization = function getV1Authorization(appID, token) {
|
|
684
687
|
if (!token) {
|
|
685
688
|
throw new Error('QQ OpenAPI v1 需要配置 token');
|
|
686
689
|
}
|
|
687
|
-
|
|
690
|
+
return "Bot ".concat(appID, ".").concat(token);
|
|
688
691
|
};
|
|
689
692
|
// 组装完整Url
|
|
690
693
|
var buildUrl = function buildUrl() {
|
|
@@ -1960,30 +1963,52 @@ var getV2AccessToken = /*#__PURE__*/function () {
|
|
|
1960
1963
|
return _ref.apply(this, arguments);
|
|
1961
1964
|
};
|
|
1962
1965
|
}();
|
|
1963
|
-
var
|
|
1964
|
-
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(
|
|
1966
|
+
var getV2Authorization = /*#__PURE__*/function () {
|
|
1967
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(config) {
|
|
1965
1968
|
var forceRefresh,
|
|
1966
1969
|
_args2 = arguments,
|
|
1967
1970
|
_t;
|
|
1968
1971
|
return _regeneratorRuntime.wrap(function (_context2) {
|
|
1969
1972
|
while (1) switch (_context2.prev = _context2.next) {
|
|
1970
1973
|
case 0:
|
|
1971
|
-
forceRefresh = _args2.length >
|
|
1974
|
+
forceRefresh = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : false;
|
|
1972
1975
|
_t = "QQBot ";
|
|
1973
1976
|
_context2.next = 1;
|
|
1974
1977
|
return getV2AccessToken(config, forceRefresh);
|
|
1975
1978
|
case 1:
|
|
1976
|
-
|
|
1979
|
+
return _context2.abrupt("return", _t.concat.call(_t, _context2.sent));
|
|
1977
1980
|
case 2:
|
|
1978
1981
|
case "end":
|
|
1979
1982
|
return _context2.stop();
|
|
1980
1983
|
}
|
|
1981
1984
|
}, _callee2);
|
|
1982
1985
|
}));
|
|
1983
|
-
return function
|
|
1986
|
+
return function getV2Authorization(_x2) {
|
|
1984
1987
|
return _ref2.apply(this, arguments);
|
|
1985
1988
|
};
|
|
1986
1989
|
}();
|
|
1990
|
+
var addV2Authorization = /*#__PURE__*/function () {
|
|
1991
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(headers, config) {
|
|
1992
|
+
var forceRefresh,
|
|
1993
|
+
_args3 = arguments;
|
|
1994
|
+
return _regeneratorRuntime.wrap(function (_context3) {
|
|
1995
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
1996
|
+
case 0:
|
|
1997
|
+
forceRefresh = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : false;
|
|
1998
|
+
_context3.next = 1;
|
|
1999
|
+
return getV2Authorization(config, forceRefresh);
|
|
2000
|
+
case 1:
|
|
2001
|
+
headers.Authorization = _context3.sent;
|
|
2002
|
+
case 2:
|
|
2003
|
+
case "end":
|
|
2004
|
+
return _context3.stop();
|
|
2005
|
+
}
|
|
2006
|
+
}, _callee3);
|
|
2007
|
+
}));
|
|
2008
|
+
return function addV2Authorization(_x3, _x4) {
|
|
2009
|
+
return _ref3.apply(this, arguments);
|
|
2010
|
+
};
|
|
2011
|
+
}();
|
|
1987
2012
|
|
|
1988
2013
|
var apiMap = {
|
|
1989
2014
|
groupMessagesURI: '/v2/groups/:groupOpenid/messages',
|
|
@@ -2252,7 +2277,6 @@ var Message = /*#__PURE__*/function (_CommonMessage) {
|
|
|
2252
2277
|
|
|
2253
2278
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
2254
2279
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
2255
|
-
var apiVersion = 'v2';
|
|
2256
2280
|
var isAuthenticationError = function isAuthenticationError(error) {
|
|
2257
2281
|
return (error === null || error === void 0 ? void 0 : error.status) === 401 || (error === null || error === void 0 ? void 0 : error.statusCode) === 401 || (error === null || error === void 0 ? void 0 : error.code) === 11201 || (error === null || error === void 0 ? void 0 : error.err_code) === 40012001;
|
|
2258
2282
|
};
|
|
@@ -2398,8 +2422,6 @@ var OpenAPI = /*#__PURE__*/function () {
|
|
|
2398
2422
|
|
|
2399
2423
|
// 事件分发类型
|
|
2400
2424
|
|
|
2401
|
-
// 请求得到ws地址的参数
|
|
2402
|
-
|
|
2403
2425
|
// 请求ws地址回包对象
|
|
2404
2426
|
|
|
2405
2427
|
// ws信息
|
|
@@ -2728,7 +2750,7 @@ var SessionEvents = {
|
|
|
2728
2750
|
};
|
|
2729
2751
|
|
|
2730
2752
|
// ws地址配置
|
|
2731
|
-
var WsObjRequestOptions = function WsObjRequestOptions(sandbox) {
|
|
2753
|
+
var WsObjRequestOptions = function WsObjRequestOptions(sandbox, apiVersion) {
|
|
2732
2754
|
return {
|
|
2733
2755
|
method: 'GET',
|
|
2734
2756
|
url: buildUrl(getURL$1('gatewayBotURI'), sandbox),
|
|
@@ -2753,11 +2775,12 @@ var Properties = {
|
|
|
2753
2775
|
|
|
2754
2776
|
// websocket连接
|
|
2755
2777
|
var Ws = /*#__PURE__*/function () {
|
|
2756
|
-
function Ws(config, event, sessionRecord) {
|
|
2778
|
+
function Ws(config, authorization, event, sessionRecord) {
|
|
2757
2779
|
_classCallCheck(this, Ws);
|
|
2758
2780
|
_defineProperty(this, "ws", void 0);
|
|
2759
2781
|
_defineProperty(this, "event", void 0);
|
|
2760
2782
|
_defineProperty(this, "config", void 0);
|
|
2783
|
+
_defineProperty(this, "authorization", void 0);
|
|
2761
2784
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
2762
2785
|
// 心跳参数,默认为心跳测试
|
|
2763
2786
|
_defineProperty(this, "heartbeatParam", {
|
|
@@ -2773,6 +2796,7 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2773
2796
|
});
|
|
2774
2797
|
_defineProperty(this, "alive", false);
|
|
2775
2798
|
this.config = config;
|
|
2799
|
+
this.authorization = authorization;
|
|
2776
2800
|
this.isReconnect = false;
|
|
2777
2801
|
this.event = event;
|
|
2778
2802
|
// 如果是重连,则拿到重新的会话记录,然后进入重连步骤
|
|
@@ -2816,7 +2840,15 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2816
2840
|
// websocket连接成功,拿到心跳周期
|
|
2817
2841
|
_this.heartbeatInterval = wsRes === null || wsRes === void 0 || (_wsRes$d2 = wsRes.d) === null || _wsRes$d2 === void 0 ? void 0 : _wsRes$d2.heartbeat_interval;
|
|
2818
2842
|
// 非断线重连时,需要鉴权
|
|
2819
|
-
_this.isReconnect ? _this.reconnectWs() : _this.authWs()
|
|
2843
|
+
void (_this.isReconnect ? _this.reconnectWs() : _this.authWs())["catch"](function (error) {
|
|
2844
|
+
var message = error instanceof Error ? error.message : String(error);
|
|
2845
|
+
BotLogger.info('[CLIENT] WebSocket鉴权失败', message);
|
|
2846
|
+
_this.event.emit(SessionEvents.ERROR, {
|
|
2847
|
+
eventType: SessionEvents.ERROR,
|
|
2848
|
+
msg: message
|
|
2849
|
+
});
|
|
2850
|
+
_this.ws.close();
|
|
2851
|
+
});
|
|
2820
2852
|
return;
|
|
2821
2853
|
}
|
|
2822
2854
|
|
|
@@ -2913,30 +2945,47 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2913
2945
|
// 鉴权
|
|
2914
2946
|
}, {
|
|
2915
2947
|
key: "authWs",
|
|
2916
|
-
value: function
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2948
|
+
value: function () {
|
|
2949
|
+
var _authWs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
2950
|
+
var authOp, _t, _t2, _t3, _t4, _t5, _t6;
|
|
2951
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
2952
|
+
while (1) switch (_context.prev = _context.next) {
|
|
2953
|
+
case 0:
|
|
2954
|
+
_t = OpCode.IDENTIFY;
|
|
2955
|
+
_context.next = 1;
|
|
2956
|
+
return this.authorization.getAuthorization();
|
|
2957
|
+
case 1:
|
|
2958
|
+
_t2 = _context.sent;
|
|
2959
|
+
_t3 = this.getValidIntents();
|
|
2960
|
+
_t4 = this.checkShards(this.config.shards) || [0, 1];
|
|
2961
|
+
_t5 = {
|
|
2962
|
+
$os: Properties.os,
|
|
2963
|
+
$browser: Properties.browser,
|
|
2964
|
+
$device: Properties.device
|
|
2965
|
+
};
|
|
2966
|
+
_t6 = {
|
|
2967
|
+
token: _t2,
|
|
2968
|
+
intents: _t3,
|
|
2969
|
+
shard: _t4,
|
|
2970
|
+
properties: _t5
|
|
2971
|
+
};
|
|
2972
|
+
authOp = {
|
|
2973
|
+
op: _t,
|
|
2974
|
+
d: _t6
|
|
2975
|
+
};
|
|
2976
|
+
// 发送鉴权请求
|
|
2977
|
+
this.sendWs(authOp);
|
|
2978
|
+
case 2:
|
|
2979
|
+
case "end":
|
|
2980
|
+
return _context.stop();
|
|
2932
2981
|
}
|
|
2933
|
-
}
|
|
2934
|
-
};
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
// 校验intents类型
|
|
2982
|
+
}, _callee, this);
|
|
2983
|
+
}));
|
|
2984
|
+
function authWs() {
|
|
2985
|
+
return _authWs.apply(this, arguments);
|
|
2986
|
+
}
|
|
2987
|
+
return authWs;
|
|
2988
|
+
}() // 校验intents类型
|
|
2940
2989
|
}, {
|
|
2941
2990
|
key: "getValidIntents",
|
|
2942
2991
|
value: function getValidIntents() {
|
|
@@ -3028,19 +3077,40 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3028
3077
|
// 重新重连Ws
|
|
3029
3078
|
}, {
|
|
3030
3079
|
key: "reconnectWs",
|
|
3031
|
-
value: function
|
|
3032
|
-
var
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3080
|
+
value: function () {
|
|
3081
|
+
var _reconnectWs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
|
3082
|
+
var reconnectParam, _t7, _t8, _t9, _t0, _t1;
|
|
3083
|
+
return _regeneratorRuntime.wrap(function (_context2) {
|
|
3084
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
3085
|
+
case 0:
|
|
3086
|
+
_t7 = OpCode.RESUME;
|
|
3087
|
+
_context2.next = 1;
|
|
3088
|
+
return this.authorization.getAuthorization();
|
|
3089
|
+
case 1:
|
|
3090
|
+
_t8 = _context2.sent;
|
|
3091
|
+
_t9 = this.sessionRecord.sessionID;
|
|
3092
|
+
_t0 = this.sessionRecord.seq;
|
|
3093
|
+
_t1 = {
|
|
3094
|
+
token: _t8,
|
|
3095
|
+
session_id: _t9,
|
|
3096
|
+
seq: _t0
|
|
3097
|
+
};
|
|
3098
|
+
reconnectParam = {
|
|
3099
|
+
op: _t7,
|
|
3100
|
+
d: _t1
|
|
3101
|
+
};
|
|
3102
|
+
this.sendWs(reconnectParam);
|
|
3103
|
+
case 2:
|
|
3104
|
+
case "end":
|
|
3105
|
+
return _context2.stop();
|
|
3106
|
+
}
|
|
3107
|
+
}, _callee2, this);
|
|
3108
|
+
}));
|
|
3109
|
+
function reconnectWs() {
|
|
3110
|
+
return _reconnectWs.apply(this, arguments);
|
|
3111
|
+
}
|
|
3112
|
+
return reconnectWs;
|
|
3113
|
+
}() // OpenAPI事件分发
|
|
3044
3114
|
}, {
|
|
3045
3115
|
key: "dispatchEvent",
|
|
3046
3116
|
value: function dispatchEvent(eventType, wsRes) {
|
|
@@ -3071,7 +3141,10 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3071
3141
|
}, {
|
|
3072
3142
|
key: "handleWsCloseEvent",
|
|
3073
3143
|
value: function handleWsCloseEvent(code) {
|
|
3074
|
-
var
|
|
3144
|
+
var _this$authorization$i,
|
|
3145
|
+
_this$authorization,
|
|
3146
|
+
_this2 = this;
|
|
3147
|
+
if (code === 4004) (_this$authorization$i = (_this$authorization = this.authorization).invalidate) === null || _this$authorization$i === void 0 || _this$authorization$i.call(_this$authorization);
|
|
3075
3148
|
WebsocketCloseReason.forEach(function (e) {
|
|
3076
3149
|
if (e.code === code) {
|
|
3077
3150
|
_this2.event.emit(SessionEvents.ERROR, {
|
|
@@ -3085,14 +3158,16 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3085
3158
|
}();
|
|
3086
3159
|
|
|
3087
3160
|
var Session = /*#__PURE__*/function () {
|
|
3088
|
-
function Session(config, event, sessionRecord) {
|
|
3161
|
+
function Session(config, authorization, event, sessionRecord) {
|
|
3089
3162
|
_classCallCheck(this, Session);
|
|
3090
3163
|
_defineProperty(this, "config", void 0);
|
|
3164
|
+
_defineProperty(this, "authorization", void 0);
|
|
3091
3165
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
3092
3166
|
_defineProperty(this, "ws", void 0);
|
|
3093
3167
|
_defineProperty(this, "event", void 0);
|
|
3094
3168
|
_defineProperty(this, "sessionRecord", void 0);
|
|
3095
3169
|
this.config = config;
|
|
3170
|
+
this.authorization = authorization;
|
|
3096
3171
|
this.event = event;
|
|
3097
3172
|
// 如果会话记录存在的话,继续透传
|
|
3098
3173
|
if (sessionRecord) {
|
|
@@ -3106,16 +3181,17 @@ var Session = /*#__PURE__*/function () {
|
|
|
3106
3181
|
key: "createSession",
|
|
3107
3182
|
value: function () {
|
|
3108
3183
|
var _createSession = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3109
|
-
var reqOptions, response, wsData, _t;
|
|
3184
|
+
var reqOptions, response, wsData, _response, _t;
|
|
3110
3185
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
3111
3186
|
while (1) switch (_context.prev = _context.next) {
|
|
3112
3187
|
case 0:
|
|
3113
3188
|
_context.prev = 0;
|
|
3114
|
-
this.ws = new Ws(this.config, this.event, this.sessionRecord || undefined);
|
|
3115
|
-
reqOptions = WsObjRequestOptions(this.config.sandbox);
|
|
3189
|
+
this.ws = new Ws(this.config, this.authorization, this.event, this.sessionRecord || undefined);
|
|
3190
|
+
reqOptions = WsObjRequestOptions(this.config.sandbox, this.authorization.apiVersion);
|
|
3116
3191
|
_context.next = 1;
|
|
3117
|
-
return
|
|
3192
|
+
return this.authorization.getAuthorization();
|
|
3118
3193
|
case 1:
|
|
3194
|
+
reqOptions.headers.Authorization = _context.sent;
|
|
3119
3195
|
_context.next = 2;
|
|
3120
3196
|
return resty.create(reqOptions).get(reqOptions.url, {});
|
|
3121
3197
|
case 2:
|
|
@@ -3133,7 +3209,12 @@ var Session = /*#__PURE__*/function () {
|
|
|
3133
3209
|
case 4:
|
|
3134
3210
|
_context.prev = 4;
|
|
3135
3211
|
_t = _context["catch"](0);
|
|
3136
|
-
|
|
3212
|
+
_response = _t === null || _t === void 0 ? void 0 : _t.response;
|
|
3213
|
+
BotLogger.info('[ERROR] createSession: ', {
|
|
3214
|
+
message: _t instanceof Error ? _t.message : String(_t),
|
|
3215
|
+
status: _response === null || _response === void 0 ? void 0 : _response.status,
|
|
3216
|
+
data: _response === null || _response === void 0 ? void 0 : _response.data
|
|
3217
|
+
});
|
|
3137
3218
|
this.event.emit(SessionEvents.EVENT_WS, {
|
|
3138
3219
|
eventType: SessionEvents.DISCONNECT,
|
|
3139
3220
|
eventMsg: this.sessionRecord
|
|
@@ -3161,13 +3242,13 @@ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstruct
|
|
|
3161
3242
|
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
3162
3243
|
var MAX_RETRY = 2;
|
|
3163
3244
|
var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
3164
|
-
function WebsocketClient(config) {
|
|
3245
|
+
function WebsocketClient(config, authorization) {
|
|
3165
3246
|
var _this;
|
|
3166
3247
|
_classCallCheck(this, WebsocketClient);
|
|
3167
3248
|
_this = _callSuper(this, WebsocketClient);
|
|
3168
3249
|
_defineProperty(_this, "session", void 0);
|
|
3169
3250
|
_defineProperty(_this, "retry", 0);
|
|
3170
|
-
_this.connect(config);
|
|
3251
|
+
_this.connect(config, authorization);
|
|
3171
3252
|
_this.on(SessionEvents.EVENT_WS, function (data) {
|
|
3172
3253
|
switch (data.eventType) {
|
|
3173
3254
|
case SessionEvents.RECONNECT:
|
|
@@ -3177,9 +3258,9 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3177
3258
|
if (_this.retry < (config.maxRetry || MAX_RETRY)) {
|
|
3178
3259
|
var _WebsocketCloseReason;
|
|
3179
3260
|
BotLogger.info('[CLIENT] 重新连接中,尝试次数:', _this.retry + 1);
|
|
3180
|
-
_this.connect(config, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3261
|
+
_this.connect(config, authorization, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3181
3262
|
return v.code === data.code;
|
|
3182
|
-
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg :
|
|
3263
|
+
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg : undefined);
|
|
3183
3264
|
_this.retry += 1;
|
|
3184
3265
|
} else {
|
|
3185
3266
|
BotLogger.info('[CLIENT] 超过重试次数,连接终止');
|
|
@@ -3202,10 +3283,10 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3202
3283
|
_inherits(WebsocketClient, _EventEmitter);
|
|
3203
3284
|
return _createClass(WebsocketClient, [{
|
|
3204
3285
|
key: "connect",
|
|
3205
|
-
value: function connect(config, sessionRecord) {
|
|
3286
|
+
value: function connect(config, authorization, sessionRecord) {
|
|
3206
3287
|
var event = this;
|
|
3207
3288
|
// 新建一个会话
|
|
3208
|
-
this.session = new Session(config, event, sessionRecord);
|
|
3289
|
+
this.session = new Session(config, authorization, event, sessionRecord);
|
|
3209
3290
|
return this.session;
|
|
3210
3291
|
}
|
|
3211
3292
|
|
|
@@ -3225,10 +3306,38 @@ function createOpenAPIV1(config) {
|
|
|
3225
3306
|
function createOpenAPIV2(config) {
|
|
3226
3307
|
return OpenAPI.newClient(config);
|
|
3227
3308
|
}
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3309
|
+
function createWebsocketV1(config) {
|
|
3310
|
+
return new WebsocketClient(config, {
|
|
3311
|
+
apiVersion: 'v1',
|
|
3312
|
+
getAuthorization: function () {
|
|
3313
|
+
var _getAuthorization = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3314
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
3315
|
+
while (1) switch (_context.prev = _context.next) {
|
|
3316
|
+
case 0:
|
|
3317
|
+
return _context.abrupt("return", getV1Authorization(config.appID, config.token));
|
|
3318
|
+
case 1:
|
|
3319
|
+
case "end":
|
|
3320
|
+
return _context.stop();
|
|
3321
|
+
}
|
|
3322
|
+
}, _callee);
|
|
3323
|
+
}));
|
|
3324
|
+
function getAuthorization() {
|
|
3325
|
+
return _getAuthorization.apply(this, arguments);
|
|
3326
|
+
}
|
|
3327
|
+
return getAuthorization;
|
|
3328
|
+
}()
|
|
3329
|
+
});
|
|
3330
|
+
}
|
|
3331
|
+
function createWebsocketV2(config) {
|
|
3332
|
+
return new WebsocketClient(config, {
|
|
3333
|
+
apiVersion: 'v2',
|
|
3334
|
+
getAuthorization: function getAuthorization() {
|
|
3335
|
+
return getV2Authorization(config);
|
|
3336
|
+
},
|
|
3337
|
+
invalidate: config.clientSecret && !config.accessToken && !config.access_token ? function () {
|
|
3338
|
+
return invalidateV2AccessToken(config.appID);
|
|
3339
|
+
} : undefined
|
|
3340
|
+
});
|
|
3232
3341
|
}
|
|
3233
3342
|
|
|
3234
|
-
export { AvailableIntentsEventsEnum, IntentEvents, Intents, OpCode, SessionEvents, WSCodes, WebsocketCloseReason, WebsocketCode, WsEventType, WsObjRequestOptions, createOpenAPIV1, createOpenAPIV2,
|
|
3343
|
+
export { AvailableIntentsEventsEnum, IntentEvents, Intents, OpCode, SessionEvents, WSCodes, WebsocketCloseReason, WebsocketCode, WsEventType, WsObjRequestOptions, createOpenAPIV1, createOpenAPIV2, createWebsocketV1, createWebsocketV2 };
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
|
|
4
|
+
var _regeneratorRuntime = require('@babel/runtime/regenerator');
|
|
4
5
|
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
5
6
|
var _createClass = require('@babel/runtime/helpers/createClass');
|
|
6
7
|
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
7
|
-
var _regeneratorRuntime = require('@babel/runtime/regenerator');
|
|
8
8
|
var resty = require('resty-client');
|
|
9
9
|
var _typeof = require('@babel/runtime/helpers/typeof');
|
|
10
10
|
var https = require('node:https');
|
|
@@ -284,7 +284,7 @@ var Guild = /*#__PURE__*/function () {
|
|
|
284
284
|
}]);
|
|
285
285
|
}();
|
|
286
286
|
|
|
287
|
-
var version = "5.0.
|
|
287
|
+
var version = "5.0.2";
|
|
288
288
|
|
|
289
289
|
function getDefaultExportFromCjs (x) {
|
|
290
290
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
@@ -683,10 +683,13 @@ var addUserAgent = function addUserAgent(header) {
|
|
|
683
683
|
header['User-Agent'] = "BotNodeSDK/v".concat(version);
|
|
684
684
|
};
|
|
685
685
|
var addV1Authorization = function addV1Authorization(header, appID, token) {
|
|
686
|
+
header['Authorization'] = getV1Authorization(appID, token);
|
|
687
|
+
};
|
|
688
|
+
var getV1Authorization = function getV1Authorization(appID, token) {
|
|
686
689
|
if (!token) {
|
|
687
690
|
throw new Error('QQ OpenAPI v1 需要配置 token');
|
|
688
691
|
}
|
|
689
|
-
|
|
692
|
+
return "Bot ".concat(appID, ".").concat(token);
|
|
690
693
|
};
|
|
691
694
|
// 组装完整Url
|
|
692
695
|
var buildUrl = function buildUrl() {
|
|
@@ -1962,30 +1965,52 @@ var getV2AccessToken = /*#__PURE__*/function () {
|
|
|
1962
1965
|
return _ref.apply(this, arguments);
|
|
1963
1966
|
};
|
|
1964
1967
|
}();
|
|
1965
|
-
var
|
|
1966
|
-
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(
|
|
1968
|
+
var getV2Authorization = /*#__PURE__*/function () {
|
|
1969
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(config) {
|
|
1967
1970
|
var forceRefresh,
|
|
1968
1971
|
_args2 = arguments,
|
|
1969
1972
|
_t;
|
|
1970
1973
|
return _regeneratorRuntime.wrap(function (_context2) {
|
|
1971
1974
|
while (1) switch (_context2.prev = _context2.next) {
|
|
1972
1975
|
case 0:
|
|
1973
|
-
forceRefresh = _args2.length >
|
|
1976
|
+
forceRefresh = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : false;
|
|
1974
1977
|
_t = "QQBot ";
|
|
1975
1978
|
_context2.next = 1;
|
|
1976
1979
|
return getV2AccessToken(config, forceRefresh);
|
|
1977
1980
|
case 1:
|
|
1978
|
-
|
|
1981
|
+
return _context2.abrupt("return", _t.concat.call(_t, _context2.sent));
|
|
1979
1982
|
case 2:
|
|
1980
1983
|
case "end":
|
|
1981
1984
|
return _context2.stop();
|
|
1982
1985
|
}
|
|
1983
1986
|
}, _callee2);
|
|
1984
1987
|
}));
|
|
1985
|
-
return function
|
|
1988
|
+
return function getV2Authorization(_x2) {
|
|
1986
1989
|
return _ref2.apply(this, arguments);
|
|
1987
1990
|
};
|
|
1988
1991
|
}();
|
|
1992
|
+
var addV2Authorization = /*#__PURE__*/function () {
|
|
1993
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(headers, config) {
|
|
1994
|
+
var forceRefresh,
|
|
1995
|
+
_args3 = arguments;
|
|
1996
|
+
return _regeneratorRuntime.wrap(function (_context3) {
|
|
1997
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
1998
|
+
case 0:
|
|
1999
|
+
forceRefresh = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : false;
|
|
2000
|
+
_context3.next = 1;
|
|
2001
|
+
return getV2Authorization(config, forceRefresh);
|
|
2002
|
+
case 1:
|
|
2003
|
+
headers.Authorization = _context3.sent;
|
|
2004
|
+
case 2:
|
|
2005
|
+
case "end":
|
|
2006
|
+
return _context3.stop();
|
|
2007
|
+
}
|
|
2008
|
+
}, _callee3);
|
|
2009
|
+
}));
|
|
2010
|
+
return function addV2Authorization(_x3, _x4) {
|
|
2011
|
+
return _ref3.apply(this, arguments);
|
|
2012
|
+
};
|
|
2013
|
+
}();
|
|
1989
2014
|
|
|
1990
2015
|
var apiMap = {
|
|
1991
2016
|
groupMessagesURI: '/v2/groups/:groupOpenid/messages',
|
|
@@ -2254,7 +2279,6 @@ var Message = /*#__PURE__*/function (_CommonMessage) {
|
|
|
2254
2279
|
|
|
2255
2280
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
2256
2281
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
2257
|
-
var apiVersion = 'v2';
|
|
2258
2282
|
var isAuthenticationError = function isAuthenticationError(error) {
|
|
2259
2283
|
return (error === null || error === void 0 ? void 0 : error.status) === 401 || (error === null || error === void 0 ? void 0 : error.statusCode) === 401 || (error === null || error === void 0 ? void 0 : error.code) === 11201 || (error === null || error === void 0 ? void 0 : error.err_code) === 40012001;
|
|
2260
2284
|
};
|
|
@@ -2400,8 +2424,6 @@ var OpenAPI = /*#__PURE__*/function () {
|
|
|
2400
2424
|
|
|
2401
2425
|
// 事件分发类型
|
|
2402
2426
|
|
|
2403
|
-
// 请求得到ws地址的参数
|
|
2404
|
-
|
|
2405
2427
|
// 请求ws地址回包对象
|
|
2406
2428
|
|
|
2407
2429
|
// ws信息
|
|
@@ -2730,7 +2752,7 @@ var SessionEvents = {
|
|
|
2730
2752
|
};
|
|
2731
2753
|
|
|
2732
2754
|
// ws地址配置
|
|
2733
|
-
var WsObjRequestOptions = function WsObjRequestOptions(sandbox) {
|
|
2755
|
+
var WsObjRequestOptions = function WsObjRequestOptions(sandbox, apiVersion) {
|
|
2734
2756
|
return {
|
|
2735
2757
|
method: 'GET',
|
|
2736
2758
|
url: buildUrl(getURL$1('gatewayBotURI'), sandbox),
|
|
@@ -2755,11 +2777,12 @@ var Properties = {
|
|
|
2755
2777
|
|
|
2756
2778
|
// websocket连接
|
|
2757
2779
|
var Ws = /*#__PURE__*/function () {
|
|
2758
|
-
function Ws(config, event, sessionRecord) {
|
|
2780
|
+
function Ws(config, authorization, event, sessionRecord) {
|
|
2759
2781
|
_classCallCheck(this, Ws);
|
|
2760
2782
|
_defineProperty(this, "ws", void 0);
|
|
2761
2783
|
_defineProperty(this, "event", void 0);
|
|
2762
2784
|
_defineProperty(this, "config", void 0);
|
|
2785
|
+
_defineProperty(this, "authorization", void 0);
|
|
2763
2786
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
2764
2787
|
// 心跳参数,默认为心跳测试
|
|
2765
2788
|
_defineProperty(this, "heartbeatParam", {
|
|
@@ -2775,6 +2798,7 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2775
2798
|
});
|
|
2776
2799
|
_defineProperty(this, "alive", false);
|
|
2777
2800
|
this.config = config;
|
|
2801
|
+
this.authorization = authorization;
|
|
2778
2802
|
this.isReconnect = false;
|
|
2779
2803
|
this.event = event;
|
|
2780
2804
|
// 如果是重连,则拿到重新的会话记录,然后进入重连步骤
|
|
@@ -2818,7 +2842,15 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2818
2842
|
// websocket连接成功,拿到心跳周期
|
|
2819
2843
|
_this.heartbeatInterval = wsRes === null || wsRes === void 0 || (_wsRes$d2 = wsRes.d) === null || _wsRes$d2 === void 0 ? void 0 : _wsRes$d2.heartbeat_interval;
|
|
2820
2844
|
// 非断线重连时,需要鉴权
|
|
2821
|
-
_this.isReconnect ? _this.reconnectWs() : _this.authWs()
|
|
2845
|
+
void (_this.isReconnect ? _this.reconnectWs() : _this.authWs())["catch"](function (error) {
|
|
2846
|
+
var message = error instanceof Error ? error.message : String(error);
|
|
2847
|
+
BotLogger.info('[CLIENT] WebSocket鉴权失败', message);
|
|
2848
|
+
_this.event.emit(SessionEvents.ERROR, {
|
|
2849
|
+
eventType: SessionEvents.ERROR,
|
|
2850
|
+
msg: message
|
|
2851
|
+
});
|
|
2852
|
+
_this.ws.close();
|
|
2853
|
+
});
|
|
2822
2854
|
return;
|
|
2823
2855
|
}
|
|
2824
2856
|
|
|
@@ -2915,30 +2947,47 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2915
2947
|
// 鉴权
|
|
2916
2948
|
}, {
|
|
2917
2949
|
key: "authWs",
|
|
2918
|
-
value: function
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2950
|
+
value: function () {
|
|
2951
|
+
var _authWs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
2952
|
+
var authOp, _t, _t2, _t3, _t4, _t5, _t6;
|
|
2953
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
2954
|
+
while (1) switch (_context.prev = _context.next) {
|
|
2955
|
+
case 0:
|
|
2956
|
+
_t = OpCode.IDENTIFY;
|
|
2957
|
+
_context.next = 1;
|
|
2958
|
+
return this.authorization.getAuthorization();
|
|
2959
|
+
case 1:
|
|
2960
|
+
_t2 = _context.sent;
|
|
2961
|
+
_t3 = this.getValidIntents();
|
|
2962
|
+
_t4 = this.checkShards(this.config.shards) || [0, 1];
|
|
2963
|
+
_t5 = {
|
|
2964
|
+
$os: Properties.os,
|
|
2965
|
+
$browser: Properties.browser,
|
|
2966
|
+
$device: Properties.device
|
|
2967
|
+
};
|
|
2968
|
+
_t6 = {
|
|
2969
|
+
token: _t2,
|
|
2970
|
+
intents: _t3,
|
|
2971
|
+
shard: _t4,
|
|
2972
|
+
properties: _t5
|
|
2973
|
+
};
|
|
2974
|
+
authOp = {
|
|
2975
|
+
op: _t,
|
|
2976
|
+
d: _t6
|
|
2977
|
+
};
|
|
2978
|
+
// 发送鉴权请求
|
|
2979
|
+
this.sendWs(authOp);
|
|
2980
|
+
case 2:
|
|
2981
|
+
case "end":
|
|
2982
|
+
return _context.stop();
|
|
2934
2983
|
}
|
|
2935
|
-
}
|
|
2936
|
-
};
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
// 校验intents类型
|
|
2984
|
+
}, _callee, this);
|
|
2985
|
+
}));
|
|
2986
|
+
function authWs() {
|
|
2987
|
+
return _authWs.apply(this, arguments);
|
|
2988
|
+
}
|
|
2989
|
+
return authWs;
|
|
2990
|
+
}() // 校验intents类型
|
|
2942
2991
|
}, {
|
|
2943
2992
|
key: "getValidIntents",
|
|
2944
2993
|
value: function getValidIntents() {
|
|
@@ -3030,19 +3079,40 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3030
3079
|
// 重新重连Ws
|
|
3031
3080
|
}, {
|
|
3032
3081
|
key: "reconnectWs",
|
|
3033
|
-
value: function
|
|
3034
|
-
var
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3082
|
+
value: function () {
|
|
3083
|
+
var _reconnectWs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
|
3084
|
+
var reconnectParam, _t7, _t8, _t9, _t0, _t1;
|
|
3085
|
+
return _regeneratorRuntime.wrap(function (_context2) {
|
|
3086
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
3087
|
+
case 0:
|
|
3088
|
+
_t7 = OpCode.RESUME;
|
|
3089
|
+
_context2.next = 1;
|
|
3090
|
+
return this.authorization.getAuthorization();
|
|
3091
|
+
case 1:
|
|
3092
|
+
_t8 = _context2.sent;
|
|
3093
|
+
_t9 = this.sessionRecord.sessionID;
|
|
3094
|
+
_t0 = this.sessionRecord.seq;
|
|
3095
|
+
_t1 = {
|
|
3096
|
+
token: _t8,
|
|
3097
|
+
session_id: _t9,
|
|
3098
|
+
seq: _t0
|
|
3099
|
+
};
|
|
3100
|
+
reconnectParam = {
|
|
3101
|
+
op: _t7,
|
|
3102
|
+
d: _t1
|
|
3103
|
+
};
|
|
3104
|
+
this.sendWs(reconnectParam);
|
|
3105
|
+
case 2:
|
|
3106
|
+
case "end":
|
|
3107
|
+
return _context2.stop();
|
|
3108
|
+
}
|
|
3109
|
+
}, _callee2, this);
|
|
3110
|
+
}));
|
|
3111
|
+
function reconnectWs() {
|
|
3112
|
+
return _reconnectWs.apply(this, arguments);
|
|
3113
|
+
}
|
|
3114
|
+
return reconnectWs;
|
|
3115
|
+
}() // OpenAPI事件分发
|
|
3046
3116
|
}, {
|
|
3047
3117
|
key: "dispatchEvent",
|
|
3048
3118
|
value: function dispatchEvent(eventType, wsRes) {
|
|
@@ -3073,7 +3143,10 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3073
3143
|
}, {
|
|
3074
3144
|
key: "handleWsCloseEvent",
|
|
3075
3145
|
value: function handleWsCloseEvent(code) {
|
|
3076
|
-
var
|
|
3146
|
+
var _this$authorization$i,
|
|
3147
|
+
_this$authorization,
|
|
3148
|
+
_this2 = this;
|
|
3149
|
+
if (code === 4004) (_this$authorization$i = (_this$authorization = this.authorization).invalidate) === null || _this$authorization$i === void 0 || _this$authorization$i.call(_this$authorization);
|
|
3077
3150
|
WebsocketCloseReason.forEach(function (e) {
|
|
3078
3151
|
if (e.code === code) {
|
|
3079
3152
|
_this2.event.emit(SessionEvents.ERROR, {
|
|
@@ -3087,14 +3160,16 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3087
3160
|
}();
|
|
3088
3161
|
|
|
3089
3162
|
var Session = /*#__PURE__*/function () {
|
|
3090
|
-
function Session(config, event, sessionRecord) {
|
|
3163
|
+
function Session(config, authorization, event, sessionRecord) {
|
|
3091
3164
|
_classCallCheck(this, Session);
|
|
3092
3165
|
_defineProperty(this, "config", void 0);
|
|
3166
|
+
_defineProperty(this, "authorization", void 0);
|
|
3093
3167
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
3094
3168
|
_defineProperty(this, "ws", void 0);
|
|
3095
3169
|
_defineProperty(this, "event", void 0);
|
|
3096
3170
|
_defineProperty(this, "sessionRecord", void 0);
|
|
3097
3171
|
this.config = config;
|
|
3172
|
+
this.authorization = authorization;
|
|
3098
3173
|
this.event = event;
|
|
3099
3174
|
// 如果会话记录存在的话,继续透传
|
|
3100
3175
|
if (sessionRecord) {
|
|
@@ -3108,16 +3183,17 @@ var Session = /*#__PURE__*/function () {
|
|
|
3108
3183
|
key: "createSession",
|
|
3109
3184
|
value: function () {
|
|
3110
3185
|
var _createSession = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3111
|
-
var reqOptions, response, wsData, _t;
|
|
3186
|
+
var reqOptions, response, wsData, _response, _t;
|
|
3112
3187
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
3113
3188
|
while (1) switch (_context.prev = _context.next) {
|
|
3114
3189
|
case 0:
|
|
3115
3190
|
_context.prev = 0;
|
|
3116
|
-
this.ws = new Ws(this.config, this.event, this.sessionRecord || undefined);
|
|
3117
|
-
reqOptions = WsObjRequestOptions(this.config.sandbox);
|
|
3191
|
+
this.ws = new Ws(this.config, this.authorization, this.event, this.sessionRecord || undefined);
|
|
3192
|
+
reqOptions = WsObjRequestOptions(this.config.sandbox, this.authorization.apiVersion);
|
|
3118
3193
|
_context.next = 1;
|
|
3119
|
-
return
|
|
3194
|
+
return this.authorization.getAuthorization();
|
|
3120
3195
|
case 1:
|
|
3196
|
+
reqOptions.headers.Authorization = _context.sent;
|
|
3121
3197
|
_context.next = 2;
|
|
3122
3198
|
return resty.create(reqOptions).get(reqOptions.url, {});
|
|
3123
3199
|
case 2:
|
|
@@ -3135,7 +3211,12 @@ var Session = /*#__PURE__*/function () {
|
|
|
3135
3211
|
case 4:
|
|
3136
3212
|
_context.prev = 4;
|
|
3137
3213
|
_t = _context["catch"](0);
|
|
3138
|
-
|
|
3214
|
+
_response = _t === null || _t === void 0 ? void 0 : _t.response;
|
|
3215
|
+
BotLogger.info('[ERROR] createSession: ', {
|
|
3216
|
+
message: _t instanceof Error ? _t.message : String(_t),
|
|
3217
|
+
status: _response === null || _response === void 0 ? void 0 : _response.status,
|
|
3218
|
+
data: _response === null || _response === void 0 ? void 0 : _response.data
|
|
3219
|
+
});
|
|
3139
3220
|
this.event.emit(SessionEvents.EVENT_WS, {
|
|
3140
3221
|
eventType: SessionEvents.DISCONNECT,
|
|
3141
3222
|
eventMsg: this.sessionRecord
|
|
@@ -3163,13 +3244,13 @@ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstruct
|
|
|
3163
3244
|
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
3164
3245
|
var MAX_RETRY = 2;
|
|
3165
3246
|
var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
3166
|
-
function WebsocketClient(config) {
|
|
3247
|
+
function WebsocketClient(config, authorization) {
|
|
3167
3248
|
var _this;
|
|
3168
3249
|
_classCallCheck(this, WebsocketClient);
|
|
3169
3250
|
_this = _callSuper(this, WebsocketClient);
|
|
3170
3251
|
_defineProperty(_this, "session", void 0);
|
|
3171
3252
|
_defineProperty(_this, "retry", 0);
|
|
3172
|
-
_this.connect(config);
|
|
3253
|
+
_this.connect(config, authorization);
|
|
3173
3254
|
_this.on(SessionEvents.EVENT_WS, function (data) {
|
|
3174
3255
|
switch (data.eventType) {
|
|
3175
3256
|
case SessionEvents.RECONNECT:
|
|
@@ -3179,9 +3260,9 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3179
3260
|
if (_this.retry < (config.maxRetry || MAX_RETRY)) {
|
|
3180
3261
|
var _WebsocketCloseReason;
|
|
3181
3262
|
BotLogger.info('[CLIENT] 重新连接中,尝试次数:', _this.retry + 1);
|
|
3182
|
-
_this.connect(config, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3263
|
+
_this.connect(config, authorization, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3183
3264
|
return v.code === data.code;
|
|
3184
|
-
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg :
|
|
3265
|
+
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg : undefined);
|
|
3185
3266
|
_this.retry += 1;
|
|
3186
3267
|
} else {
|
|
3187
3268
|
BotLogger.info('[CLIENT] 超过重试次数,连接终止');
|
|
@@ -3204,10 +3285,10 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3204
3285
|
_inherits(WebsocketClient, _EventEmitter);
|
|
3205
3286
|
return _createClass(WebsocketClient, [{
|
|
3206
3287
|
key: "connect",
|
|
3207
|
-
value: function connect(config, sessionRecord) {
|
|
3288
|
+
value: function connect(config, authorization, sessionRecord) {
|
|
3208
3289
|
var event = this;
|
|
3209
3290
|
// 新建一个会话
|
|
3210
|
-
this.session = new Session(config, event, sessionRecord);
|
|
3291
|
+
this.session = new Session(config, authorization, event, sessionRecord);
|
|
3211
3292
|
return this.session;
|
|
3212
3293
|
}
|
|
3213
3294
|
|
|
@@ -3227,10 +3308,38 @@ function createOpenAPIV1(config) {
|
|
|
3227
3308
|
function createOpenAPIV2(config) {
|
|
3228
3309
|
return OpenAPI.newClient(config);
|
|
3229
3310
|
}
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3311
|
+
function createWebsocketV1(config) {
|
|
3312
|
+
return new WebsocketClient(config, {
|
|
3313
|
+
apiVersion: 'v1',
|
|
3314
|
+
getAuthorization: function () {
|
|
3315
|
+
var _getAuthorization = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3316
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
3317
|
+
while (1) switch (_context.prev = _context.next) {
|
|
3318
|
+
case 0:
|
|
3319
|
+
return _context.abrupt("return", getV1Authorization(config.appID, config.token));
|
|
3320
|
+
case 1:
|
|
3321
|
+
case "end":
|
|
3322
|
+
return _context.stop();
|
|
3323
|
+
}
|
|
3324
|
+
}, _callee);
|
|
3325
|
+
}));
|
|
3326
|
+
function getAuthorization() {
|
|
3327
|
+
return _getAuthorization.apply(this, arguments);
|
|
3328
|
+
}
|
|
3329
|
+
return getAuthorization;
|
|
3330
|
+
}()
|
|
3331
|
+
});
|
|
3332
|
+
}
|
|
3333
|
+
function createWebsocketV2(config) {
|
|
3334
|
+
return new WebsocketClient(config, {
|
|
3335
|
+
apiVersion: 'v2',
|
|
3336
|
+
getAuthorization: function getAuthorization() {
|
|
3337
|
+
return getV2Authorization(config);
|
|
3338
|
+
},
|
|
3339
|
+
invalidate: config.clientSecret && !config.accessToken && !config.access_token ? function () {
|
|
3340
|
+
return invalidateV2AccessToken(config.appID);
|
|
3341
|
+
} : undefined
|
|
3342
|
+
});
|
|
3234
3343
|
}
|
|
3235
3344
|
|
|
3236
3345
|
exports.AvailableIntentsEventsEnum = AvailableIntentsEventsEnum;
|
|
@@ -3245,4 +3354,5 @@ exports.WsEventType = WsEventType;
|
|
|
3245
3354
|
exports.WsObjRequestOptions = WsObjRequestOptions;
|
|
3246
3355
|
exports.createOpenAPIV1 = createOpenAPIV1;
|
|
3247
3356
|
exports.createOpenAPIV2 = createOpenAPIV2;
|
|
3248
|
-
exports.
|
|
3357
|
+
exports.createWebsocketV1 = createWebsocketV1;
|
|
3358
|
+
exports.createWebsocketV2 = createWebsocketV2;
|
package/package.json
CHANGED
package/typings/index.d.ts
CHANGED
|
@@ -845,11 +845,19 @@ interface EventTypes {
|
|
|
845
845
|
eventType: string;
|
|
846
846
|
eventMsg?: object;
|
|
847
847
|
}
|
|
848
|
-
|
|
848
|
+
interface WebsocketOptions {
|
|
849
849
|
shards?: Array<number>;
|
|
850
850
|
intents?: Array<AvailableIntentsEventsEnum>;
|
|
851
851
|
maxRetry?: number;
|
|
852
|
-
}
|
|
852
|
+
}
|
|
853
|
+
type V1WebsocketConfig = V1Config & WebsocketOptions;
|
|
854
|
+
type V2WebsocketConfig = V2Config & WebsocketOptions;
|
|
855
|
+
type WebsocketConfig = V1WebsocketConfig | V2WebsocketConfig;
|
|
856
|
+
interface WebsocketAuthorization {
|
|
857
|
+
apiVersion: 'v1' | 'v2';
|
|
858
|
+
getAuthorization: () => Promise<string>;
|
|
859
|
+
invalidate?: () => void;
|
|
860
|
+
}
|
|
853
861
|
interface WsAddressObj {
|
|
854
862
|
url: string;
|
|
855
863
|
shards: number;
|
|
@@ -967,7 +975,7 @@ declare const SessionEvents: {
|
|
|
967
975
|
RESUMED: string;
|
|
968
976
|
DEAD: string;
|
|
969
977
|
};
|
|
970
|
-
declare const WsObjRequestOptions: (sandbox: boolean) => {
|
|
978
|
+
declare const WsObjRequestOptions: (sandbox: boolean, apiVersion: "v1" | "v2") => {
|
|
971
979
|
method: "GET";
|
|
972
980
|
url: string;
|
|
973
981
|
headers: {
|
|
@@ -975,7 +983,7 @@ declare const WsObjRequestOptions: (sandbox: boolean) => {
|
|
|
975
983
|
'Accept-Encoding': string;
|
|
976
984
|
'Accept-Language': string;
|
|
977
985
|
Connection: string;
|
|
978
|
-
'User-Agent':
|
|
986
|
+
'User-Agent': "v1" | "v2";
|
|
979
987
|
Authorization: string;
|
|
980
988
|
};
|
|
981
989
|
};
|
|
@@ -1081,7 +1089,8 @@ type Nullish = null | undefined;
|
|
|
1081
1089
|
declare class Ws {
|
|
1082
1090
|
ws: WebSocket;
|
|
1083
1091
|
event: EventEmitter;
|
|
1084
|
-
config:
|
|
1092
|
+
config: WebsocketConfig;
|
|
1093
|
+
authorization: WebsocketAuthorization;
|
|
1085
1094
|
heartbeatInterval: number;
|
|
1086
1095
|
heartbeatParam: {
|
|
1087
1096
|
op: OpCode;
|
|
@@ -1093,29 +1102,30 @@ declare class Ws {
|
|
|
1093
1102
|
seq: number;
|
|
1094
1103
|
};
|
|
1095
1104
|
alive: boolean;
|
|
1096
|
-
constructor(config:
|
|
1105
|
+
constructor(config: WebsocketConfig, authorization: WebsocketAuthorization, event: EventEmitter, sessionRecord?: SessionRecord);
|
|
1097
1106
|
createWebsocket(wsData: WsAddressObj): WebSocket;
|
|
1098
1107
|
createListening(): WebSocket;
|
|
1099
1108
|
connectWs(wsData: WsAddressObj): void;
|
|
1100
|
-
authWs(): void
|
|
1109
|
+
authWs(): Promise<void>;
|
|
1101
1110
|
getValidIntents(): number | undefined;
|
|
1102
1111
|
getValidIntentsType(): AvailableIntentsEventsEnum[];
|
|
1103
1112
|
checkShards(shardsArr: Array<number> | undefined): void | number[];
|
|
1104
1113
|
sendWs(msg: unknown): void;
|
|
1105
1114
|
reconnect(): void;
|
|
1106
|
-
reconnectWs(): void
|
|
1115
|
+
reconnectWs(): Promise<void>;
|
|
1107
1116
|
dispatchEvent(eventType: string, wsRes: wsResData): void;
|
|
1108
1117
|
closeWs(): void;
|
|
1109
1118
|
handleWsCloseEvent(code: number): void;
|
|
1110
1119
|
}
|
|
1111
1120
|
|
|
1112
1121
|
declare class Session {
|
|
1113
|
-
config:
|
|
1122
|
+
config: WebsocketConfig;
|
|
1123
|
+
authorization: WebsocketAuthorization;
|
|
1114
1124
|
heartbeatInterval: number;
|
|
1115
1125
|
ws: Ws;
|
|
1116
1126
|
event: EventEmitter;
|
|
1117
1127
|
sessionRecord: SessionRecord | undefined;
|
|
1118
|
-
constructor(config:
|
|
1128
|
+
constructor(config: WebsocketConfig, authorization: WebsocketAuthorization, event: EventEmitter, sessionRecord?: SessionRecord);
|
|
1119
1129
|
createSession(): Promise<void>;
|
|
1120
1130
|
closeSession(): void;
|
|
1121
1131
|
}
|
|
@@ -1123,14 +1133,15 @@ declare class Session {
|
|
|
1123
1133
|
declare class WebsocketClient extends EventEmitter {
|
|
1124
1134
|
session: Session;
|
|
1125
1135
|
retry: number;
|
|
1126
|
-
constructor(config:
|
|
1127
|
-
connect(config:
|
|
1136
|
+
constructor(config: WebsocketConfig, authorization: WebsocketAuthorization);
|
|
1137
|
+
connect(config: WebsocketConfig, authorization: WebsocketAuthorization, sessionRecord?: SessionRecord): Session;
|
|
1128
1138
|
disconnect(): void;
|
|
1129
1139
|
}
|
|
1130
1140
|
|
|
1131
1141
|
declare function createOpenAPIV1(config: V1Config): V1OpenAPI;
|
|
1132
1142
|
declare function createOpenAPIV2(config: V2Config): V2OpenAPI;
|
|
1133
|
-
declare function
|
|
1143
|
+
declare function createWebsocketV1(config: V1WebsocketConfig): WebsocketClient;
|
|
1144
|
+
declare function createWebsocketV2(config: V2WebsocketConfig): WebsocketClient;
|
|
1134
1145
|
|
|
1135
|
-
export { AvailableIntentsEventsEnum, IntentEvents, Intents, OpCode, SessionEvents, WSCodes, WebsocketCloseReason, WebsocketCode, WsEventType, WsObjRequestOptions, createOpenAPIV1, createOpenAPIV2,
|
|
1136
|
-
export type { Action, AnnounceAPI, Ark, ArkKV, ArkObj, ArkObjKV, AudioAPI, AudioControl, Button, C2CMessageToCreate, ChannelAPI, ChannelPermissionsAPI, ChannelSubType, ChannelType, CommonOpenAPI, Config, CreateGroupJoinApprovalStrategy, CustomKeyboard, DirectMessageAPI, DirectMessageToCreate, Embed, EmbedField, EmbedThumbnail, EventTypes,
|
|
1146
|
+
export { AvailableIntentsEventsEnum, IntentEvents, Intents, OpCode, SessionEvents, WSCodes, WebsocketCloseReason, WebsocketCode, WsEventType, WsObjRequestOptions, createOpenAPIV1, createOpenAPIV2, createWebsocketV1, createWebsocketV2 };
|
|
1147
|
+
export type { Action, AnnounceAPI, Ark, ArkKV, ArkObj, ArkObjKV, AudioAPI, AudioControl, Button, C2CMessageToCreate, ChannelAPI, ChannelPermissionsAPI, ChannelSubType, ChannelType, CommonOpenAPI, Config, CreateGroupJoinApprovalStrategy, CustomKeyboard, DirectMessageAPI, DirectMessageToCreate, Embed, EmbedField, EmbedThumbnail, EventTypes, GroupAddRobotEvent, GroupAtMessageCreateEvent, GroupBotState, GroupCursorPager, GroupDelRobotEvent, GroupEventPayloadMap, GroupEventType, GroupGlobalMuteRule, GroupInfo, GroupJoinApplySource, GroupJoinApprovalStrategy, GroupJoinApprovalStrategyGroupAction, GroupJoinApprovalStrategyGroups, GroupJoinApprovalStrategyList, GroupJoinApprovalStrategyResult, GroupJoinApprovalStrategyState, GroupJoinApprovalStrategyWhitelistResult, GroupJoinRequest, GroupJoinRequestApproval, GroupJoinRequestEvent, GroupJoinRequestList, GroupJoinReviewQA, GroupJoinVerifyInfo, GroupJoinVerifyMethod, GroupMemberAddEvent, GroupMemberEvent, GroupMemberMuteState, GroupMemberRemoveEvent, GroupMemberRole, GroupMessageArkData, GroupMessageAttachment, GroupMessageCreateEvent, GroupMessageElement, GroupMessageEvent, GroupMessageResponse, GroupMessageScene, GroupMessageToCreate, GroupMessageUser, GroupMsgReceiveEvent, GroupMsgRejectEvent, GroupMuteMode, GroupMuteRecurringRule, GroupMuteScheduleRule, GroupReceiveMessageSetting, GroupRestrictChatSetting, GroupRobotEvent, GuildAPI, GuildMembersPager, GuildPermission, GuildPermissionDemand, GuildPermissionDemandIdentify, GuildPermissionRes, GuildPermissionsAPI, GuildRoles, HeartbeatParam, IAnnounce, IChannel, IChannelPermissions, IChannelRolePermissions, IDirectMessage, IGuild, IMember, IMessage, IMessageRes, IOpenAPI, IPinsMessage, IRole, IRoleFilter, ISchedule, IUser, IVoiceMember, InputNotify, IntentEventsMapType, InteractionAPI, InteractionData, KeyboardAction, KeyboardButton, KeyboardContent, KeyboardPermission, KeyboardRenderData, KeyboardRow, MeAPI, MeGuildsReq, Media, MediaInfo, MediaResponse, MediaToCreate, MemberAPI, MemberAddRoleBody, MessageAPI, MessageAttachment, MessageExtInfo, MessageKeyboard, MessageMarkdown, MessageReference, MessageToCreate, MessagesPager, MuteAPI, MuteOptions, Nullish, OpenAPIRequest, OpenAPIRequestOptions, PatchChannelObj, Permission, PermissionDemandToCreate, PinsMessageAPI, PostChannelObj, ReactionAPI, ReactionObj, ReactionUserListObj, RecommendChannel, RecommendObj, RenderData, RequestTimeoutOptions, RoleAPI, Row, ScheduleAPI, ScheduleRemindType, ScheduleToCreate, ScheduleToPatch, SessionRecord, SetGroupMemberMuteState, SetGroupRestrictChatSetting, StreamMessageResponse, StreamMessageToCreate, ThreadReplyToCreate, ThreadsAPI, ThreadsToCreate, Token, UpdateChannelPermissions, UpdateGroupJoinApprovalStrategy, UpdateGroupJoinApprovalStrategyWhitelist, UpdateRoleRes, V1Config, V1OpenAPI, V1WebsocketConfig, V2Config, V2GroupAPI, V2GroupMemberRole, V2InputNotify, V2MessageAPI, V2MessageKeyboard, V2MessageReference, V2OpenAPI, V2WebsocketConfig, WebsocketAPI, WebsocketAuthorization, WebsocketConfig, WebsocketEventData, WebsocketOptions, WsAddressObj, WsDataInfo, wsResData };
|