woodenfish-bot 5.0.0 → 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 +180 -72
- package/lib/index.js +181 -72
- package/package.json +1 -1
- package/typings/index.d.ts +44 -26
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,33 +1963,54 @@ 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
|
-
gatewayBotURI: '/gateway/bot',
|
|
1990
2014
|
groupMessagesURI: '/v2/groups/:groupOpenid/messages',
|
|
1991
2015
|
groupRichMediaURI: '/v2/groups/:groupOpenid/files',
|
|
1992
2016
|
groupMessageURI: '/v2/groups/:groupOpenid/messages/:messageID',
|
|
@@ -2158,12 +2182,12 @@ var Group = /*#__PURE__*/function () {
|
|
|
2158
2182
|
|
|
2159
2183
|
function _callSuper$1(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct$1() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
2160
2184
|
function _isNativeReflectConstruct$1() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct$1 = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
2161
|
-
var Message = /*#__PURE__*/function (
|
|
2185
|
+
var Message = /*#__PURE__*/function (_CommonMessage) {
|
|
2162
2186
|
function Message() {
|
|
2163
2187
|
_classCallCheck(this, Message);
|
|
2164
2188
|
return _callSuper$1(this, Message, arguments);
|
|
2165
2189
|
}
|
|
2166
|
-
_inherits(Message,
|
|
2190
|
+
_inherits(Message, _CommonMessage);
|
|
2167
2191
|
return _createClass(Message, [{
|
|
2168
2192
|
key: "postGroupMessage",
|
|
2169
2193
|
value: function postGroupMessage(groupOpenid, message) {
|
|
@@ -2253,7 +2277,6 @@ var Message = /*#__PURE__*/function (_V1Message) {
|
|
|
2253
2277
|
|
|
2254
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; }
|
|
2255
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; }
|
|
2256
|
-
var apiVersion = 'v2';
|
|
2257
2280
|
var isAuthenticationError = function isAuthenticationError(error) {
|
|
2258
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;
|
|
2259
2282
|
};
|
|
@@ -2399,8 +2422,6 @@ var OpenAPI = /*#__PURE__*/function () {
|
|
|
2399
2422
|
|
|
2400
2423
|
// 事件分发类型
|
|
2401
2424
|
|
|
2402
|
-
// 请求得到ws地址的参数
|
|
2403
|
-
|
|
2404
2425
|
// 请求ws地址回包对象
|
|
2405
2426
|
|
|
2406
2427
|
// ws信息
|
|
@@ -2729,10 +2750,10 @@ var SessionEvents = {
|
|
|
2729
2750
|
};
|
|
2730
2751
|
|
|
2731
2752
|
// ws地址配置
|
|
2732
|
-
var WsObjRequestOptions = function WsObjRequestOptions(sandbox) {
|
|
2753
|
+
var WsObjRequestOptions = function WsObjRequestOptions(sandbox, apiVersion) {
|
|
2733
2754
|
return {
|
|
2734
2755
|
method: 'GET',
|
|
2735
|
-
url: buildUrl(getURL('gatewayBotURI'), sandbox),
|
|
2756
|
+
url: buildUrl(getURL$1('gatewayBotURI'), sandbox),
|
|
2736
2757
|
headers: {
|
|
2737
2758
|
Accept: '*/*',
|
|
2738
2759
|
'Accept-Encoding': 'utf-8',
|
|
@@ -2754,11 +2775,12 @@ var Properties = {
|
|
|
2754
2775
|
|
|
2755
2776
|
// websocket连接
|
|
2756
2777
|
var Ws = /*#__PURE__*/function () {
|
|
2757
|
-
function Ws(config, event, sessionRecord) {
|
|
2778
|
+
function Ws(config, authorization, event, sessionRecord) {
|
|
2758
2779
|
_classCallCheck(this, Ws);
|
|
2759
2780
|
_defineProperty(this, "ws", void 0);
|
|
2760
2781
|
_defineProperty(this, "event", void 0);
|
|
2761
2782
|
_defineProperty(this, "config", void 0);
|
|
2783
|
+
_defineProperty(this, "authorization", void 0);
|
|
2762
2784
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
2763
2785
|
// 心跳参数,默认为心跳测试
|
|
2764
2786
|
_defineProperty(this, "heartbeatParam", {
|
|
@@ -2774,6 +2796,7 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2774
2796
|
});
|
|
2775
2797
|
_defineProperty(this, "alive", false);
|
|
2776
2798
|
this.config = config;
|
|
2799
|
+
this.authorization = authorization;
|
|
2777
2800
|
this.isReconnect = false;
|
|
2778
2801
|
this.event = event;
|
|
2779
2802
|
// 如果是重连,则拿到重新的会话记录,然后进入重连步骤
|
|
@@ -2817,7 +2840,15 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2817
2840
|
// websocket连接成功,拿到心跳周期
|
|
2818
2841
|
_this.heartbeatInterval = wsRes === null || wsRes === void 0 || (_wsRes$d2 = wsRes.d) === null || _wsRes$d2 === void 0 ? void 0 : _wsRes$d2.heartbeat_interval;
|
|
2819
2842
|
// 非断线重连时,需要鉴权
|
|
2820
|
-
_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
|
+
});
|
|
2821
2852
|
return;
|
|
2822
2853
|
}
|
|
2823
2854
|
|
|
@@ -2914,30 +2945,47 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2914
2945
|
// 鉴权
|
|
2915
2946
|
}, {
|
|
2916
2947
|
key: "authWs",
|
|
2917
|
-
value: function
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
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();
|
|
2933
2981
|
}
|
|
2934
|
-
}
|
|
2935
|
-
};
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
// 校验intents类型
|
|
2982
|
+
}, _callee, this);
|
|
2983
|
+
}));
|
|
2984
|
+
function authWs() {
|
|
2985
|
+
return _authWs.apply(this, arguments);
|
|
2986
|
+
}
|
|
2987
|
+
return authWs;
|
|
2988
|
+
}() // 校验intents类型
|
|
2941
2989
|
}, {
|
|
2942
2990
|
key: "getValidIntents",
|
|
2943
2991
|
value: function getValidIntents() {
|
|
@@ -3029,19 +3077,40 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3029
3077
|
// 重新重连Ws
|
|
3030
3078
|
}, {
|
|
3031
3079
|
key: "reconnectWs",
|
|
3032
|
-
value: function
|
|
3033
|
-
var
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
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事件分发
|
|
3045
3114
|
}, {
|
|
3046
3115
|
key: "dispatchEvent",
|
|
3047
3116
|
value: function dispatchEvent(eventType, wsRes) {
|
|
@@ -3072,7 +3141,10 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3072
3141
|
}, {
|
|
3073
3142
|
key: "handleWsCloseEvent",
|
|
3074
3143
|
value: function handleWsCloseEvent(code) {
|
|
3075
|
-
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);
|
|
3076
3148
|
WebsocketCloseReason.forEach(function (e) {
|
|
3077
3149
|
if (e.code === code) {
|
|
3078
3150
|
_this2.event.emit(SessionEvents.ERROR, {
|
|
@@ -3086,14 +3158,16 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3086
3158
|
}();
|
|
3087
3159
|
|
|
3088
3160
|
var Session = /*#__PURE__*/function () {
|
|
3089
|
-
function Session(config, event, sessionRecord) {
|
|
3161
|
+
function Session(config, authorization, event, sessionRecord) {
|
|
3090
3162
|
_classCallCheck(this, Session);
|
|
3091
3163
|
_defineProperty(this, "config", void 0);
|
|
3164
|
+
_defineProperty(this, "authorization", void 0);
|
|
3092
3165
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
3093
3166
|
_defineProperty(this, "ws", void 0);
|
|
3094
3167
|
_defineProperty(this, "event", void 0);
|
|
3095
3168
|
_defineProperty(this, "sessionRecord", void 0);
|
|
3096
3169
|
this.config = config;
|
|
3170
|
+
this.authorization = authorization;
|
|
3097
3171
|
this.event = event;
|
|
3098
3172
|
// 如果会话记录存在的话,继续透传
|
|
3099
3173
|
if (sessionRecord) {
|
|
@@ -3107,16 +3181,17 @@ var Session = /*#__PURE__*/function () {
|
|
|
3107
3181
|
key: "createSession",
|
|
3108
3182
|
value: function () {
|
|
3109
3183
|
var _createSession = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3110
|
-
var reqOptions, response, wsData, _t;
|
|
3184
|
+
var reqOptions, response, wsData, _response, _t;
|
|
3111
3185
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
3112
3186
|
while (1) switch (_context.prev = _context.next) {
|
|
3113
3187
|
case 0:
|
|
3114
3188
|
_context.prev = 0;
|
|
3115
|
-
this.ws = new Ws(this.config, this.event, this.sessionRecord || undefined);
|
|
3116
|
-
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);
|
|
3117
3191
|
_context.next = 1;
|
|
3118
|
-
return
|
|
3192
|
+
return this.authorization.getAuthorization();
|
|
3119
3193
|
case 1:
|
|
3194
|
+
reqOptions.headers.Authorization = _context.sent;
|
|
3120
3195
|
_context.next = 2;
|
|
3121
3196
|
return resty.create(reqOptions).get(reqOptions.url, {});
|
|
3122
3197
|
case 2:
|
|
@@ -3134,7 +3209,12 @@ var Session = /*#__PURE__*/function () {
|
|
|
3134
3209
|
case 4:
|
|
3135
3210
|
_context.prev = 4;
|
|
3136
3211
|
_t = _context["catch"](0);
|
|
3137
|
-
|
|
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
|
+
});
|
|
3138
3218
|
this.event.emit(SessionEvents.EVENT_WS, {
|
|
3139
3219
|
eventType: SessionEvents.DISCONNECT,
|
|
3140
3220
|
eventMsg: this.sessionRecord
|
|
@@ -3162,13 +3242,13 @@ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstruct
|
|
|
3162
3242
|
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
3163
3243
|
var MAX_RETRY = 2;
|
|
3164
3244
|
var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
3165
|
-
function WebsocketClient(config) {
|
|
3245
|
+
function WebsocketClient(config, authorization) {
|
|
3166
3246
|
var _this;
|
|
3167
3247
|
_classCallCheck(this, WebsocketClient);
|
|
3168
3248
|
_this = _callSuper(this, WebsocketClient);
|
|
3169
3249
|
_defineProperty(_this, "session", void 0);
|
|
3170
3250
|
_defineProperty(_this, "retry", 0);
|
|
3171
|
-
_this.connect(config);
|
|
3251
|
+
_this.connect(config, authorization);
|
|
3172
3252
|
_this.on(SessionEvents.EVENT_WS, function (data) {
|
|
3173
3253
|
switch (data.eventType) {
|
|
3174
3254
|
case SessionEvents.RECONNECT:
|
|
@@ -3178,9 +3258,9 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3178
3258
|
if (_this.retry < (config.maxRetry || MAX_RETRY)) {
|
|
3179
3259
|
var _WebsocketCloseReason;
|
|
3180
3260
|
BotLogger.info('[CLIENT] 重新连接中,尝试次数:', _this.retry + 1);
|
|
3181
|
-
_this.connect(config, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3261
|
+
_this.connect(config, authorization, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3182
3262
|
return v.code === data.code;
|
|
3183
|
-
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg :
|
|
3263
|
+
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg : undefined);
|
|
3184
3264
|
_this.retry += 1;
|
|
3185
3265
|
} else {
|
|
3186
3266
|
BotLogger.info('[CLIENT] 超过重试次数,连接终止');
|
|
@@ -3203,10 +3283,10 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3203
3283
|
_inherits(WebsocketClient, _EventEmitter);
|
|
3204
3284
|
return _createClass(WebsocketClient, [{
|
|
3205
3285
|
key: "connect",
|
|
3206
|
-
value: function connect(config, sessionRecord) {
|
|
3286
|
+
value: function connect(config, authorization, sessionRecord) {
|
|
3207
3287
|
var event = this;
|
|
3208
3288
|
// 新建一个会话
|
|
3209
|
-
this.session = new Session(config, event, sessionRecord);
|
|
3289
|
+
this.session = new Session(config, authorization, event, sessionRecord);
|
|
3210
3290
|
return this.session;
|
|
3211
3291
|
}
|
|
3212
3292
|
|
|
@@ -3226,10 +3306,38 @@ function createOpenAPIV1(config) {
|
|
|
3226
3306
|
function createOpenAPIV2(config) {
|
|
3227
3307
|
return OpenAPI.newClient(config);
|
|
3228
3308
|
}
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
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
|
+
});
|
|
3233
3341
|
}
|
|
3234
3342
|
|
|
3235
|
-
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,33 +1965,54 @@ 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
|
-
gatewayBotURI: '/gateway/bot',
|
|
1992
2016
|
groupMessagesURI: '/v2/groups/:groupOpenid/messages',
|
|
1993
2017
|
groupRichMediaURI: '/v2/groups/:groupOpenid/files',
|
|
1994
2018
|
groupMessageURI: '/v2/groups/:groupOpenid/messages/:messageID',
|
|
@@ -2160,12 +2184,12 @@ var Group = /*#__PURE__*/function () {
|
|
|
2160
2184
|
|
|
2161
2185
|
function _callSuper$1(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct$1() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
2162
2186
|
function _isNativeReflectConstruct$1() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct$1 = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
2163
|
-
var Message = /*#__PURE__*/function (
|
|
2187
|
+
var Message = /*#__PURE__*/function (_CommonMessage) {
|
|
2164
2188
|
function Message() {
|
|
2165
2189
|
_classCallCheck(this, Message);
|
|
2166
2190
|
return _callSuper$1(this, Message, arguments);
|
|
2167
2191
|
}
|
|
2168
|
-
_inherits(Message,
|
|
2192
|
+
_inherits(Message, _CommonMessage);
|
|
2169
2193
|
return _createClass(Message, [{
|
|
2170
2194
|
key: "postGroupMessage",
|
|
2171
2195
|
value: function postGroupMessage(groupOpenid, message) {
|
|
@@ -2255,7 +2279,6 @@ var Message = /*#__PURE__*/function (_V1Message) {
|
|
|
2255
2279
|
|
|
2256
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; }
|
|
2257
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; }
|
|
2258
|
-
var apiVersion = 'v2';
|
|
2259
2282
|
var isAuthenticationError = function isAuthenticationError(error) {
|
|
2260
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;
|
|
2261
2284
|
};
|
|
@@ -2401,8 +2424,6 @@ var OpenAPI = /*#__PURE__*/function () {
|
|
|
2401
2424
|
|
|
2402
2425
|
// 事件分发类型
|
|
2403
2426
|
|
|
2404
|
-
// 请求得到ws地址的参数
|
|
2405
|
-
|
|
2406
2427
|
// 请求ws地址回包对象
|
|
2407
2428
|
|
|
2408
2429
|
// ws信息
|
|
@@ -2731,10 +2752,10 @@ var SessionEvents = {
|
|
|
2731
2752
|
};
|
|
2732
2753
|
|
|
2733
2754
|
// ws地址配置
|
|
2734
|
-
var WsObjRequestOptions = function WsObjRequestOptions(sandbox) {
|
|
2755
|
+
var WsObjRequestOptions = function WsObjRequestOptions(sandbox, apiVersion) {
|
|
2735
2756
|
return {
|
|
2736
2757
|
method: 'GET',
|
|
2737
|
-
url: buildUrl(getURL('gatewayBotURI'), sandbox),
|
|
2758
|
+
url: buildUrl(getURL$1('gatewayBotURI'), sandbox),
|
|
2738
2759
|
headers: {
|
|
2739
2760
|
Accept: '*/*',
|
|
2740
2761
|
'Accept-Encoding': 'utf-8',
|
|
@@ -2756,11 +2777,12 @@ var Properties = {
|
|
|
2756
2777
|
|
|
2757
2778
|
// websocket连接
|
|
2758
2779
|
var Ws = /*#__PURE__*/function () {
|
|
2759
|
-
function Ws(config, event, sessionRecord) {
|
|
2780
|
+
function Ws(config, authorization, event, sessionRecord) {
|
|
2760
2781
|
_classCallCheck(this, Ws);
|
|
2761
2782
|
_defineProperty(this, "ws", void 0);
|
|
2762
2783
|
_defineProperty(this, "event", void 0);
|
|
2763
2784
|
_defineProperty(this, "config", void 0);
|
|
2785
|
+
_defineProperty(this, "authorization", void 0);
|
|
2764
2786
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
2765
2787
|
// 心跳参数,默认为心跳测试
|
|
2766
2788
|
_defineProperty(this, "heartbeatParam", {
|
|
@@ -2776,6 +2798,7 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2776
2798
|
});
|
|
2777
2799
|
_defineProperty(this, "alive", false);
|
|
2778
2800
|
this.config = config;
|
|
2801
|
+
this.authorization = authorization;
|
|
2779
2802
|
this.isReconnect = false;
|
|
2780
2803
|
this.event = event;
|
|
2781
2804
|
// 如果是重连,则拿到重新的会话记录,然后进入重连步骤
|
|
@@ -2819,7 +2842,15 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2819
2842
|
// websocket连接成功,拿到心跳周期
|
|
2820
2843
|
_this.heartbeatInterval = wsRes === null || wsRes === void 0 || (_wsRes$d2 = wsRes.d) === null || _wsRes$d2 === void 0 ? void 0 : _wsRes$d2.heartbeat_interval;
|
|
2821
2844
|
// 非断线重连时,需要鉴权
|
|
2822
|
-
_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
|
+
});
|
|
2823
2854
|
return;
|
|
2824
2855
|
}
|
|
2825
2856
|
|
|
@@ -2916,30 +2947,47 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2916
2947
|
// 鉴权
|
|
2917
2948
|
}, {
|
|
2918
2949
|
key: "authWs",
|
|
2919
|
-
value: function
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
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();
|
|
2935
2983
|
}
|
|
2936
|
-
}
|
|
2937
|
-
};
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
// 校验intents类型
|
|
2984
|
+
}, _callee, this);
|
|
2985
|
+
}));
|
|
2986
|
+
function authWs() {
|
|
2987
|
+
return _authWs.apply(this, arguments);
|
|
2988
|
+
}
|
|
2989
|
+
return authWs;
|
|
2990
|
+
}() // 校验intents类型
|
|
2943
2991
|
}, {
|
|
2944
2992
|
key: "getValidIntents",
|
|
2945
2993
|
value: function getValidIntents() {
|
|
@@ -3031,19 +3079,40 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3031
3079
|
// 重新重连Ws
|
|
3032
3080
|
}, {
|
|
3033
3081
|
key: "reconnectWs",
|
|
3034
|
-
value: function
|
|
3035
|
-
var
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
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事件分发
|
|
3047
3116
|
}, {
|
|
3048
3117
|
key: "dispatchEvent",
|
|
3049
3118
|
value: function dispatchEvent(eventType, wsRes) {
|
|
@@ -3074,7 +3143,10 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3074
3143
|
}, {
|
|
3075
3144
|
key: "handleWsCloseEvent",
|
|
3076
3145
|
value: function handleWsCloseEvent(code) {
|
|
3077
|
-
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);
|
|
3078
3150
|
WebsocketCloseReason.forEach(function (e) {
|
|
3079
3151
|
if (e.code === code) {
|
|
3080
3152
|
_this2.event.emit(SessionEvents.ERROR, {
|
|
@@ -3088,14 +3160,16 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3088
3160
|
}();
|
|
3089
3161
|
|
|
3090
3162
|
var Session = /*#__PURE__*/function () {
|
|
3091
|
-
function Session(config, event, sessionRecord) {
|
|
3163
|
+
function Session(config, authorization, event, sessionRecord) {
|
|
3092
3164
|
_classCallCheck(this, Session);
|
|
3093
3165
|
_defineProperty(this, "config", void 0);
|
|
3166
|
+
_defineProperty(this, "authorization", void 0);
|
|
3094
3167
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
3095
3168
|
_defineProperty(this, "ws", void 0);
|
|
3096
3169
|
_defineProperty(this, "event", void 0);
|
|
3097
3170
|
_defineProperty(this, "sessionRecord", void 0);
|
|
3098
3171
|
this.config = config;
|
|
3172
|
+
this.authorization = authorization;
|
|
3099
3173
|
this.event = event;
|
|
3100
3174
|
// 如果会话记录存在的话,继续透传
|
|
3101
3175
|
if (sessionRecord) {
|
|
@@ -3109,16 +3183,17 @@ var Session = /*#__PURE__*/function () {
|
|
|
3109
3183
|
key: "createSession",
|
|
3110
3184
|
value: function () {
|
|
3111
3185
|
var _createSession = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3112
|
-
var reqOptions, response, wsData, _t;
|
|
3186
|
+
var reqOptions, response, wsData, _response, _t;
|
|
3113
3187
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
3114
3188
|
while (1) switch (_context.prev = _context.next) {
|
|
3115
3189
|
case 0:
|
|
3116
3190
|
_context.prev = 0;
|
|
3117
|
-
this.ws = new Ws(this.config, this.event, this.sessionRecord || undefined);
|
|
3118
|
-
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);
|
|
3119
3193
|
_context.next = 1;
|
|
3120
|
-
return
|
|
3194
|
+
return this.authorization.getAuthorization();
|
|
3121
3195
|
case 1:
|
|
3196
|
+
reqOptions.headers.Authorization = _context.sent;
|
|
3122
3197
|
_context.next = 2;
|
|
3123
3198
|
return resty.create(reqOptions).get(reqOptions.url, {});
|
|
3124
3199
|
case 2:
|
|
@@ -3136,7 +3211,12 @@ var Session = /*#__PURE__*/function () {
|
|
|
3136
3211
|
case 4:
|
|
3137
3212
|
_context.prev = 4;
|
|
3138
3213
|
_t = _context["catch"](0);
|
|
3139
|
-
|
|
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
|
+
});
|
|
3140
3220
|
this.event.emit(SessionEvents.EVENT_WS, {
|
|
3141
3221
|
eventType: SessionEvents.DISCONNECT,
|
|
3142
3222
|
eventMsg: this.sessionRecord
|
|
@@ -3164,13 +3244,13 @@ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstruct
|
|
|
3164
3244
|
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
3165
3245
|
var MAX_RETRY = 2;
|
|
3166
3246
|
var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
3167
|
-
function WebsocketClient(config) {
|
|
3247
|
+
function WebsocketClient(config, authorization) {
|
|
3168
3248
|
var _this;
|
|
3169
3249
|
_classCallCheck(this, WebsocketClient);
|
|
3170
3250
|
_this = _callSuper(this, WebsocketClient);
|
|
3171
3251
|
_defineProperty(_this, "session", void 0);
|
|
3172
3252
|
_defineProperty(_this, "retry", 0);
|
|
3173
|
-
_this.connect(config);
|
|
3253
|
+
_this.connect(config, authorization);
|
|
3174
3254
|
_this.on(SessionEvents.EVENT_WS, function (data) {
|
|
3175
3255
|
switch (data.eventType) {
|
|
3176
3256
|
case SessionEvents.RECONNECT:
|
|
@@ -3180,9 +3260,9 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3180
3260
|
if (_this.retry < (config.maxRetry || MAX_RETRY)) {
|
|
3181
3261
|
var _WebsocketCloseReason;
|
|
3182
3262
|
BotLogger.info('[CLIENT] 重新连接中,尝试次数:', _this.retry + 1);
|
|
3183
|
-
_this.connect(config, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3263
|
+
_this.connect(config, authorization, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3184
3264
|
return v.code === data.code;
|
|
3185
|
-
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg :
|
|
3265
|
+
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg : undefined);
|
|
3186
3266
|
_this.retry += 1;
|
|
3187
3267
|
} else {
|
|
3188
3268
|
BotLogger.info('[CLIENT] 超过重试次数,连接终止');
|
|
@@ -3205,10 +3285,10 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3205
3285
|
_inherits(WebsocketClient, _EventEmitter);
|
|
3206
3286
|
return _createClass(WebsocketClient, [{
|
|
3207
3287
|
key: "connect",
|
|
3208
|
-
value: function connect(config, sessionRecord) {
|
|
3288
|
+
value: function connect(config, authorization, sessionRecord) {
|
|
3209
3289
|
var event = this;
|
|
3210
3290
|
// 新建一个会话
|
|
3211
|
-
this.session = new Session(config, event, sessionRecord);
|
|
3291
|
+
this.session = new Session(config, authorization, event, sessionRecord);
|
|
3212
3292
|
return this.session;
|
|
3213
3293
|
}
|
|
3214
3294
|
|
|
@@ -3228,10 +3308,38 @@ function createOpenAPIV1(config) {
|
|
|
3228
3308
|
function createOpenAPIV2(config) {
|
|
3229
3309
|
return OpenAPI.newClient(config);
|
|
3230
3310
|
}
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
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
|
+
});
|
|
3235
3343
|
}
|
|
3236
3344
|
|
|
3237
3345
|
exports.AvailableIntentsEventsEnum = AvailableIntentsEventsEnum;
|
|
@@ -3246,4 +3354,5 @@ exports.WsEventType = WsEventType;
|
|
|
3246
3354
|
exports.WsObjRequestOptions = WsObjRequestOptions;
|
|
3247
3355
|
exports.createOpenAPIV1 = createOpenAPIV1;
|
|
3248
3356
|
exports.createOpenAPIV2 = createOpenAPIV2;
|
|
3249
|
-
exports.
|
|
3357
|
+
exports.createWebsocketV1 = createWebsocketV1;
|
|
3358
|
+
exports.createWebsocketV2 = createWebsocketV2;
|
package/package.json
CHANGED
package/typings/index.d.ts
CHANGED
|
@@ -782,8 +782,18 @@ interface Config {
|
|
|
782
782
|
access_token?: string;
|
|
783
783
|
sandbox?: boolean;
|
|
784
784
|
}
|
|
785
|
-
interface
|
|
786
|
-
|
|
785
|
+
interface V1Config extends Config {
|
|
786
|
+
token: string;
|
|
787
|
+
}
|
|
788
|
+
type V2Config = Config & ({
|
|
789
|
+
accessToken: string;
|
|
790
|
+
} | {
|
|
791
|
+
access_token: string;
|
|
792
|
+
} | {
|
|
793
|
+
clientSecret: string;
|
|
794
|
+
});
|
|
795
|
+
interface CommonOpenAPI<TConfig extends Config = Config> {
|
|
796
|
+
config: TConfig;
|
|
787
797
|
request: OpenAPIRequest;
|
|
788
798
|
guildApi: GuildAPI;
|
|
789
799
|
channelApi: ChannelAPI;
|
|
@@ -803,7 +813,11 @@ interface IOpenAPI {
|
|
|
803
813
|
interactionApi: InteractionAPI;
|
|
804
814
|
pinsMessageApi: PinsMessageAPI;
|
|
805
815
|
}
|
|
806
|
-
interface
|
|
816
|
+
interface V1OpenAPI extends CommonOpenAPI<V1Config> {
|
|
817
|
+
}
|
|
818
|
+
/** @deprecated 请使用 V1OpenAPI。 */
|
|
819
|
+
type IOpenAPI = V1OpenAPI;
|
|
820
|
+
interface V2OpenAPI extends Omit<CommonOpenAPI<V2Config>, 'messageApi'> {
|
|
807
821
|
groupApi: V2GroupAPI;
|
|
808
822
|
messageApi: MessageAPI & V2MessageAPI;
|
|
809
823
|
}
|
|
@@ -831,18 +845,19 @@ interface EventTypes {
|
|
|
831
845
|
eventType: string;
|
|
832
846
|
eventMsg?: object;
|
|
833
847
|
}
|
|
834
|
-
interface
|
|
835
|
-
appID: string;
|
|
836
|
-
token?: string;
|
|
837
|
-
clientSecret?: string;
|
|
838
|
-
accessToken?: string;
|
|
839
|
-
/** @deprecated 请使用 accessToken。 */
|
|
840
|
-
access_token?: string;
|
|
841
|
-
sandbox?: boolean;
|
|
848
|
+
interface WebsocketOptions {
|
|
842
849
|
shards?: Array<number>;
|
|
843
850
|
intents?: Array<AvailableIntentsEventsEnum>;
|
|
844
851
|
maxRetry?: number;
|
|
845
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
|
+
}
|
|
846
861
|
interface WsAddressObj {
|
|
847
862
|
url: string;
|
|
848
863
|
shards: number;
|
|
@@ -960,7 +975,7 @@ declare const SessionEvents: {
|
|
|
960
975
|
RESUMED: string;
|
|
961
976
|
DEAD: string;
|
|
962
977
|
};
|
|
963
|
-
declare const WsObjRequestOptions: (sandbox: boolean) => {
|
|
978
|
+
declare const WsObjRequestOptions: (sandbox: boolean, apiVersion: "v1" | "v2") => {
|
|
964
979
|
method: "GET";
|
|
965
980
|
url: string;
|
|
966
981
|
headers: {
|
|
@@ -968,7 +983,7 @@ declare const WsObjRequestOptions: (sandbox: boolean) => {
|
|
|
968
983
|
'Accept-Encoding': string;
|
|
969
984
|
'Accept-Language': string;
|
|
970
985
|
Connection: string;
|
|
971
|
-
'User-Agent':
|
|
986
|
+
'User-Agent': "v1" | "v2";
|
|
972
987
|
Authorization: string;
|
|
973
988
|
};
|
|
974
989
|
};
|
|
@@ -1074,7 +1089,8 @@ type Nullish = null | undefined;
|
|
|
1074
1089
|
declare class Ws {
|
|
1075
1090
|
ws: WebSocket;
|
|
1076
1091
|
event: EventEmitter;
|
|
1077
|
-
config:
|
|
1092
|
+
config: WebsocketConfig;
|
|
1093
|
+
authorization: WebsocketAuthorization;
|
|
1078
1094
|
heartbeatInterval: number;
|
|
1079
1095
|
heartbeatParam: {
|
|
1080
1096
|
op: OpCode;
|
|
@@ -1086,29 +1102,30 @@ declare class Ws {
|
|
|
1086
1102
|
seq: number;
|
|
1087
1103
|
};
|
|
1088
1104
|
alive: boolean;
|
|
1089
|
-
constructor(config:
|
|
1105
|
+
constructor(config: WebsocketConfig, authorization: WebsocketAuthorization, event: EventEmitter, sessionRecord?: SessionRecord);
|
|
1090
1106
|
createWebsocket(wsData: WsAddressObj): WebSocket;
|
|
1091
1107
|
createListening(): WebSocket;
|
|
1092
1108
|
connectWs(wsData: WsAddressObj): void;
|
|
1093
|
-
authWs(): void
|
|
1109
|
+
authWs(): Promise<void>;
|
|
1094
1110
|
getValidIntents(): number | undefined;
|
|
1095
1111
|
getValidIntentsType(): AvailableIntentsEventsEnum[];
|
|
1096
1112
|
checkShards(shardsArr: Array<number> | undefined): void | number[];
|
|
1097
1113
|
sendWs(msg: unknown): void;
|
|
1098
1114
|
reconnect(): void;
|
|
1099
|
-
reconnectWs(): void
|
|
1115
|
+
reconnectWs(): Promise<void>;
|
|
1100
1116
|
dispatchEvent(eventType: string, wsRes: wsResData): void;
|
|
1101
1117
|
closeWs(): void;
|
|
1102
1118
|
handleWsCloseEvent(code: number): void;
|
|
1103
1119
|
}
|
|
1104
1120
|
|
|
1105
1121
|
declare class Session {
|
|
1106
|
-
config:
|
|
1122
|
+
config: WebsocketConfig;
|
|
1123
|
+
authorization: WebsocketAuthorization;
|
|
1107
1124
|
heartbeatInterval: number;
|
|
1108
1125
|
ws: Ws;
|
|
1109
1126
|
event: EventEmitter;
|
|
1110
1127
|
sessionRecord: SessionRecord | undefined;
|
|
1111
|
-
constructor(config:
|
|
1128
|
+
constructor(config: WebsocketConfig, authorization: WebsocketAuthorization, event: EventEmitter, sessionRecord?: SessionRecord);
|
|
1112
1129
|
createSession(): Promise<void>;
|
|
1113
1130
|
closeSession(): void;
|
|
1114
1131
|
}
|
|
@@ -1116,14 +1133,15 @@ declare class Session {
|
|
|
1116
1133
|
declare class WebsocketClient extends EventEmitter {
|
|
1117
1134
|
session: Session;
|
|
1118
1135
|
retry: number;
|
|
1119
|
-
constructor(config:
|
|
1120
|
-
connect(config:
|
|
1136
|
+
constructor(config: WebsocketConfig, authorization: WebsocketAuthorization);
|
|
1137
|
+
connect(config: WebsocketConfig, authorization: WebsocketAuthorization, sessionRecord?: SessionRecord): Session;
|
|
1121
1138
|
disconnect(): void;
|
|
1122
1139
|
}
|
|
1123
1140
|
|
|
1124
|
-
declare function createOpenAPIV1(config:
|
|
1125
|
-
declare function createOpenAPIV2(config:
|
|
1126
|
-
declare function
|
|
1141
|
+
declare function createOpenAPIV1(config: V1Config): V1OpenAPI;
|
|
1142
|
+
declare function createOpenAPIV2(config: V2Config): V2OpenAPI;
|
|
1143
|
+
declare function createWebsocketV1(config: V1WebsocketConfig): WebsocketClient;
|
|
1144
|
+
declare function createWebsocketV2(config: V2WebsocketConfig): WebsocketClient;
|
|
1127
1145
|
|
|
1128
|
-
export { AvailableIntentsEventsEnum, IntentEvents, Intents, OpCode, SessionEvents, WSCodes, WebsocketCloseReason, WebsocketCode, WsEventType, WsObjRequestOptions, createOpenAPIV1, createOpenAPIV2,
|
|
1129
|
-
export type { Action, AnnounceAPI, Ark, ArkKV, ArkObj, ArkObjKV, AudioAPI, AudioControl, Button, C2CMessageToCreate, ChannelAPI, ChannelPermissionsAPI, ChannelSubType, ChannelType, 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 };
|