woodenfish-bot 5.0.1 → 5.0.3
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 +253 -123
- package/lib/index.js +254 -123
- 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.3";
|
|
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() {
|
|
@@ -1306,8 +1309,8 @@ var Audio = /*#__PURE__*/function () {
|
|
|
1306
1309
|
}]);
|
|
1307
1310
|
}();
|
|
1308
1311
|
|
|
1309
|
-
function ownKeys$
|
|
1310
|
-
function _objectSpread$
|
|
1312
|
+
function ownKeys$5(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; }
|
|
1313
|
+
function _objectSpread$5(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$5(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$5(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
1311
1314
|
var Mute = /*#__PURE__*/function () {
|
|
1312
1315
|
function Mute(request, config) {
|
|
1313
1316
|
_classCallCheck(this, Mute);
|
|
@@ -1368,7 +1371,7 @@ var Mute = /*#__PURE__*/function () {
|
|
|
1368
1371
|
guildID: guildID
|
|
1369
1372
|
},
|
|
1370
1373
|
// 将options和userIDList合并到一起
|
|
1371
|
-
data: _objectSpread$
|
|
1374
|
+
data: _objectSpread$5(_objectSpread$5({}, options), {}, {
|
|
1372
1375
|
user_ids: userIDList
|
|
1373
1376
|
})
|
|
1374
1377
|
};
|
|
@@ -1564,8 +1567,8 @@ var Schedule = /*#__PURE__*/function () {
|
|
|
1564
1567
|
}]);
|
|
1565
1568
|
}();
|
|
1566
1569
|
|
|
1567
|
-
function ownKeys$
|
|
1568
|
-
function _objectSpread$
|
|
1570
|
+
function ownKeys$4(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; }
|
|
1571
|
+
function _objectSpread$4(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$4(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$4(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
1569
1572
|
var Threads = /*#__PURE__*/function () {
|
|
1570
1573
|
function Threads(request, config) {
|
|
1571
1574
|
_classCallCheck(this, Threads);
|
|
@@ -1579,7 +1582,7 @@ var Threads = /*#__PURE__*/function () {
|
|
|
1579
1582
|
return _createClass(Threads, [{
|
|
1580
1583
|
key: "threads",
|
|
1581
1584
|
value: function threads(channelID, requestOptions) {
|
|
1582
|
-
var options = _objectSpread$
|
|
1585
|
+
var options = _objectSpread$4({
|
|
1583
1586
|
method: 'GET',
|
|
1584
1587
|
url: getURL$1('threadsURI'),
|
|
1585
1588
|
rest: {
|
|
@@ -1593,7 +1596,7 @@ var Threads = /*#__PURE__*/function () {
|
|
|
1593
1596
|
}, {
|
|
1594
1597
|
key: "thread",
|
|
1595
1598
|
value: function thread(channelID, threadID, requestOptions) {
|
|
1596
|
-
var options = _objectSpread$
|
|
1599
|
+
var options = _objectSpread$4({
|
|
1597
1600
|
method: 'GET',
|
|
1598
1601
|
url: getURL$1('threadURI'),
|
|
1599
1602
|
rest: {
|
|
@@ -1608,7 +1611,7 @@ var Threads = /*#__PURE__*/function () {
|
|
|
1608
1611
|
}, {
|
|
1609
1612
|
key: "putThreads",
|
|
1610
1613
|
value: function putThreads(channelID, _Threads2, requestOptions) {
|
|
1611
|
-
var options = _objectSpread$
|
|
1614
|
+
var options = _objectSpread$4({
|
|
1612
1615
|
method: 'PUT',
|
|
1613
1616
|
url: getURL$1('threadsURI'),
|
|
1614
1617
|
rest: {
|
|
@@ -1625,7 +1628,7 @@ var Threads = /*#__PURE__*/function () {
|
|
|
1625
1628
|
}, {
|
|
1626
1629
|
key: "putPostReply",
|
|
1627
1630
|
value: function putPostReply(channelID, threadID, postID, reply, requestOptions) {
|
|
1628
|
-
var options = _objectSpread$
|
|
1631
|
+
var options = _objectSpread$4({
|
|
1629
1632
|
method: 'PUT',
|
|
1630
1633
|
url: getURL$1('threadPostRepliesURI'),
|
|
1631
1634
|
rest: {
|
|
@@ -1642,7 +1645,7 @@ var Threads = /*#__PURE__*/function () {
|
|
|
1642
1645
|
}, {
|
|
1643
1646
|
key: "deleteThreads",
|
|
1644
1647
|
value: function deleteThreads(channelID, threadID, requestOptions) {
|
|
1645
|
-
var options = _objectSpread$
|
|
1648
|
+
var options = _objectSpread$4({
|
|
1646
1649
|
method: 'DELETE',
|
|
1647
1650
|
url: getURL$1('threadURI'),
|
|
1648
1651
|
rest: {
|
|
@@ -1724,6 +1727,43 @@ var Interaction = /*#__PURE__*/function () {
|
|
|
1724
1727
|
}]);
|
|
1725
1728
|
}();
|
|
1726
1729
|
|
|
1730
|
+
function ownKeys$3(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; }
|
|
1731
|
+
function _objectSpread$3(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$3(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$3(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
1732
|
+
var getTraceID = function getTraceID(error) {
|
|
1733
|
+
var _error$response;
|
|
1734
|
+
return error === null || error === void 0 || (_error$response = error.response) === null || _error$response === void 0 || (_error$response = _error$response.headers) === null || _error$response === void 0 ? void 0 : _error$response['x-tps-trace-id'];
|
|
1735
|
+
};
|
|
1736
|
+
var normalizeV1OpenAPIError = function normalizeV1OpenAPIError(error) {
|
|
1737
|
+
var _error$response2;
|
|
1738
|
+
var traceid = getTraceID(error);
|
|
1739
|
+
if (error !== null && error !== void 0 && (_error$response2 = error.response) !== null && _error$response2 !== void 0 && _error$response2.data) {
|
|
1740
|
+
return _objectSpread$3(_objectSpread$3({}, error.response.data), {}, {
|
|
1741
|
+
traceid: traceid
|
|
1742
|
+
});
|
|
1743
|
+
}
|
|
1744
|
+
if (error !== null && error !== void 0 && error.response) {
|
|
1745
|
+
return _objectSpread$3(_objectSpread$3({}, error.response), {}, {
|
|
1746
|
+
traceid: traceid
|
|
1747
|
+
});
|
|
1748
|
+
}
|
|
1749
|
+
return error;
|
|
1750
|
+
};
|
|
1751
|
+
var normalizeV2OpenAPIError = function normalizeV2OpenAPIError(error) {
|
|
1752
|
+
var _error$response3;
|
|
1753
|
+
var traceID = getTraceID(error);
|
|
1754
|
+
if (error !== null && error !== void 0 && (_error$response3 = error.response) !== null && _error$response3 !== void 0 && _error$response3.data) {
|
|
1755
|
+
return _objectSpread$3(_objectSpread$3({}, error.response.data), {}, {
|
|
1756
|
+
trace_id: error.response.data.trace_id || traceID
|
|
1757
|
+
});
|
|
1758
|
+
}
|
|
1759
|
+
if (error !== null && error !== void 0 && error.response) {
|
|
1760
|
+
return _objectSpread$3(_objectSpread$3({}, error.response), {}, {
|
|
1761
|
+
trace_id: traceID
|
|
1762
|
+
});
|
|
1763
|
+
}
|
|
1764
|
+
return error;
|
|
1765
|
+
};
|
|
1766
|
+
|
|
1727
1767
|
function ownKeys$2(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; }
|
|
1728
1768
|
function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
1729
1769
|
var OpenAPI$1 = /*#__PURE__*/function () {
|
|
@@ -1754,7 +1794,7 @@ var OpenAPI$1 = /*#__PURE__*/function () {
|
|
|
1754
1794
|
// 基础rest请求
|
|
1755
1795
|
_defineProperty(this, "request", /*#__PURE__*/function () {
|
|
1756
1796
|
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(options) {
|
|
1757
|
-
var _this$config, appID, token, botUrl, client;
|
|
1797
|
+
var _this$config, appID, token, botUrl, client, _t;
|
|
1758
1798
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
1759
1799
|
while (1) switch (_context.prev = _context.next) {
|
|
1760
1800
|
case 0:
|
|
@@ -1766,31 +1806,22 @@ var OpenAPI$1 = /*#__PURE__*/function () {
|
|
|
1766
1806
|
// 添加鉴权信息
|
|
1767
1807
|
addV1Authorization(options.headers, appID, token);
|
|
1768
1808
|
// 组装完整Url
|
|
1769
|
-
botUrl = buildUrl(options.url, _this.config.sandbox);
|
|
1770
|
-
resty.useRes(function (result) {
|
|
1771
|
-
return result;
|
|
1772
|
-
}, function (error) {
|
|
1773
|
-
var _error$response, _error$response2;
|
|
1774
|
-
var traceid = error === null || error === void 0 || (_error$response = error.response) === null || _error$response === void 0 || (_error$response = _error$response.headers) === null || _error$response === void 0 ? void 0 : _error$response['x-tps-trace-id'];
|
|
1775
|
-
if (error !== null && error !== void 0 && (_error$response2 = error.response) !== null && _error$response2 !== void 0 && _error$response2.data) {
|
|
1776
|
-
return Promise.reject(_objectSpread$2(_objectSpread$2({}, error.response.data), {}, {
|
|
1777
|
-
traceid: traceid
|
|
1778
|
-
}));
|
|
1779
|
-
}
|
|
1780
|
-
if (error !== null && error !== void 0 && error.response) {
|
|
1781
|
-
return Promise.reject(_objectSpread$2(_objectSpread$2({}, error.response), {}, {
|
|
1782
|
-
traceid: traceid
|
|
1783
|
-
}));
|
|
1784
|
-
}
|
|
1785
|
-
return Promise.reject(error);
|
|
1786
|
-
});
|
|
1809
|
+
botUrl = buildUrl(options.url, _this.config.sandbox);
|
|
1787
1810
|
client = resty.create(options);
|
|
1788
|
-
|
|
1789
|
-
|
|
1811
|
+
_context.prev = 1;
|
|
1812
|
+
_context.next = 2;
|
|
1813
|
+
return client.request(botUrl, options);
|
|
1814
|
+
case 2:
|
|
1815
|
+
return _context.abrupt("return", _context.sent);
|
|
1816
|
+
case 3:
|
|
1817
|
+
_context.prev = 3;
|
|
1818
|
+
_t = _context["catch"](1);
|
|
1819
|
+
throw normalizeV1OpenAPIError(_t);
|
|
1820
|
+
case 4:
|
|
1790
1821
|
case "end":
|
|
1791
1822
|
return _context.stop();
|
|
1792
1823
|
}
|
|
1793
|
-
}, _callee);
|
|
1824
|
+
}, _callee, null, [[1, 3]]);
|
|
1794
1825
|
}));
|
|
1795
1826
|
return function (_x) {
|
|
1796
1827
|
return _ref.apply(this, arguments);
|
|
@@ -1960,30 +1991,52 @@ var getV2AccessToken = /*#__PURE__*/function () {
|
|
|
1960
1991
|
return _ref.apply(this, arguments);
|
|
1961
1992
|
};
|
|
1962
1993
|
}();
|
|
1963
|
-
var
|
|
1964
|
-
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(
|
|
1994
|
+
var getV2Authorization = /*#__PURE__*/function () {
|
|
1995
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(config) {
|
|
1965
1996
|
var forceRefresh,
|
|
1966
1997
|
_args2 = arguments,
|
|
1967
1998
|
_t;
|
|
1968
1999
|
return _regeneratorRuntime.wrap(function (_context2) {
|
|
1969
2000
|
while (1) switch (_context2.prev = _context2.next) {
|
|
1970
2001
|
case 0:
|
|
1971
|
-
forceRefresh = _args2.length >
|
|
2002
|
+
forceRefresh = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : false;
|
|
1972
2003
|
_t = "QQBot ";
|
|
1973
2004
|
_context2.next = 1;
|
|
1974
2005
|
return getV2AccessToken(config, forceRefresh);
|
|
1975
2006
|
case 1:
|
|
1976
|
-
|
|
2007
|
+
return _context2.abrupt("return", _t.concat.call(_t, _context2.sent));
|
|
1977
2008
|
case 2:
|
|
1978
2009
|
case "end":
|
|
1979
2010
|
return _context2.stop();
|
|
1980
2011
|
}
|
|
1981
2012
|
}, _callee2);
|
|
1982
2013
|
}));
|
|
1983
|
-
return function
|
|
2014
|
+
return function getV2Authorization(_x2) {
|
|
1984
2015
|
return _ref2.apply(this, arguments);
|
|
1985
2016
|
};
|
|
1986
2017
|
}();
|
|
2018
|
+
var addV2Authorization = /*#__PURE__*/function () {
|
|
2019
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(headers, config) {
|
|
2020
|
+
var forceRefresh,
|
|
2021
|
+
_args3 = arguments;
|
|
2022
|
+
return _regeneratorRuntime.wrap(function (_context3) {
|
|
2023
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
2024
|
+
case 0:
|
|
2025
|
+
forceRefresh = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : false;
|
|
2026
|
+
_context3.next = 1;
|
|
2027
|
+
return getV2Authorization(config, forceRefresh);
|
|
2028
|
+
case 1:
|
|
2029
|
+
headers.Authorization = _context3.sent;
|
|
2030
|
+
case 2:
|
|
2031
|
+
case "end":
|
|
2032
|
+
return _context3.stop();
|
|
2033
|
+
}
|
|
2034
|
+
}, _callee3);
|
|
2035
|
+
}));
|
|
2036
|
+
return function addV2Authorization(_x3, _x4) {
|
|
2037
|
+
return _ref3.apply(this, arguments);
|
|
2038
|
+
};
|
|
2039
|
+
}();
|
|
1987
2040
|
|
|
1988
2041
|
var apiMap = {
|
|
1989
2042
|
groupMessagesURI: '/v2/groups/:groupOpenid/messages',
|
|
@@ -2252,9 +2305,10 @@ var Message = /*#__PURE__*/function (_CommonMessage) {
|
|
|
2252
2305
|
|
|
2253
2306
|
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
2307
|
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
2308
|
var isAuthenticationError = function isAuthenticationError(error) {
|
|
2257
|
-
|
|
2309
|
+
var _error$response, _error$response2;
|
|
2310
|
+
var responseData = error === null || error === void 0 || (_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.data;
|
|
2311
|
+
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 || (_error$response2 = error.response) === null || _error$response2 === void 0 ? void 0 : _error$response2.status) === 401 || (error === null || error === void 0 ? void 0 : error.code) === 11201 || (error === null || error === void 0 ? void 0 : error.err_code) === 40012001 || (responseData === null || responseData === void 0 ? void 0 : responseData.code) === 11201 || (responseData === null || responseData === void 0 ? void 0 : responseData.err_code) === 40012001;
|
|
2258
2312
|
};
|
|
2259
2313
|
var OpenAPI = /*#__PURE__*/function () {
|
|
2260
2314
|
function OpenAPI(config) {
|
|
@@ -2308,27 +2362,10 @@ var OpenAPI = /*#__PURE__*/function () {
|
|
|
2308
2362
|
}());
|
|
2309
2363
|
_defineProperty(this, "request", /*#__PURE__*/function () {
|
|
2310
2364
|
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(options) {
|
|
2311
|
-
var botUrl, preparedOptions, _t;
|
|
2365
|
+
var botUrl, preparedOptions, _t, _t2;
|
|
2312
2366
|
return _regeneratorRuntime.wrap(function (_context2) {
|
|
2313
2367
|
while (1) switch (_context2.prev = _context2.next) {
|
|
2314
2368
|
case 0:
|
|
2315
|
-
resty.useRes(function (result) {
|
|
2316
|
-
return result;
|
|
2317
|
-
}, function (error) {
|
|
2318
|
-
var _error$response, _error$response2;
|
|
2319
|
-
var traceID = error === null || error === void 0 || (_error$response = error.response) === null || _error$response === void 0 || (_error$response = _error$response.headers) === null || _error$response === void 0 ? void 0 : _error$response['x-tps-trace-id'];
|
|
2320
|
-
if (error !== null && error !== void 0 && (_error$response2 = error.response) !== null && _error$response2 !== void 0 && _error$response2.data) {
|
|
2321
|
-
return Promise.reject(_objectSpread(_objectSpread({}, error.response.data), {}, {
|
|
2322
|
-
trace_id: error.response.data.trace_id || traceID
|
|
2323
|
-
}));
|
|
2324
|
-
}
|
|
2325
|
-
if (error !== null && error !== void 0 && error.response) {
|
|
2326
|
-
return Promise.reject(_objectSpread(_objectSpread({}, error.response), {}, {
|
|
2327
|
-
trace_id: traceID
|
|
2328
|
-
}));
|
|
2329
|
-
}
|
|
2330
|
-
return Promise.reject(error);
|
|
2331
|
-
});
|
|
2332
2369
|
botUrl = buildUrl(options.url, _this.config.sandbox);
|
|
2333
2370
|
_context2.next = 1;
|
|
2334
2371
|
return _this.prepareOptions(options);
|
|
@@ -2346,19 +2383,27 @@ var OpenAPI = /*#__PURE__*/function () {
|
|
|
2346
2383
|
_context2.next = 5;
|
|
2347
2384
|
break;
|
|
2348
2385
|
}
|
|
2349
|
-
throw _t;
|
|
2386
|
+
throw normalizeV2OpenAPIError(_t);
|
|
2350
2387
|
case 5:
|
|
2351
2388
|
invalidateV2AccessToken(_this.config.appID);
|
|
2352
2389
|
_context2.next = 6;
|
|
2353
2390
|
return _this.prepareOptions(options, true);
|
|
2354
2391
|
case 6:
|
|
2355
2392
|
preparedOptions = _context2.sent;
|
|
2356
|
-
|
|
2357
|
-
|
|
2393
|
+
_context2.prev = 7;
|
|
2394
|
+
_context2.next = 8;
|
|
2395
|
+
return resty.create(preparedOptions).request(botUrl, preparedOptions);
|
|
2396
|
+
case 8:
|
|
2397
|
+
return _context2.abrupt("return", _context2.sent);
|
|
2398
|
+
case 9:
|
|
2399
|
+
_context2.prev = 9;
|
|
2400
|
+
_t2 = _context2["catch"](7);
|
|
2401
|
+
throw normalizeV2OpenAPIError(_t2);
|
|
2402
|
+
case 10:
|
|
2358
2403
|
case "end":
|
|
2359
2404
|
return _context2.stop();
|
|
2360
2405
|
}
|
|
2361
|
-
}, _callee2, null, [[2, 4]]);
|
|
2406
|
+
}, _callee2, null, [[2, 4], [7, 9]]);
|
|
2362
2407
|
}));
|
|
2363
2408
|
return function (_x2) {
|
|
2364
2409
|
return _ref2.apply(this, arguments);
|
|
@@ -2398,8 +2443,6 @@ var OpenAPI = /*#__PURE__*/function () {
|
|
|
2398
2443
|
|
|
2399
2444
|
// 事件分发类型
|
|
2400
2445
|
|
|
2401
|
-
// 请求得到ws地址的参数
|
|
2402
|
-
|
|
2403
2446
|
// 请求ws地址回包对象
|
|
2404
2447
|
|
|
2405
2448
|
// ws信息
|
|
@@ -2728,7 +2771,7 @@ var SessionEvents = {
|
|
|
2728
2771
|
};
|
|
2729
2772
|
|
|
2730
2773
|
// ws地址配置
|
|
2731
|
-
var WsObjRequestOptions = function WsObjRequestOptions(sandbox) {
|
|
2774
|
+
var WsObjRequestOptions = function WsObjRequestOptions(sandbox, apiVersion) {
|
|
2732
2775
|
return {
|
|
2733
2776
|
method: 'GET',
|
|
2734
2777
|
url: buildUrl(getURL$1('gatewayBotURI'), sandbox),
|
|
@@ -2753,11 +2796,12 @@ var Properties = {
|
|
|
2753
2796
|
|
|
2754
2797
|
// websocket连接
|
|
2755
2798
|
var Ws = /*#__PURE__*/function () {
|
|
2756
|
-
function Ws(config, event, sessionRecord) {
|
|
2799
|
+
function Ws(config, authorization, event, sessionRecord) {
|
|
2757
2800
|
_classCallCheck(this, Ws);
|
|
2758
2801
|
_defineProperty(this, "ws", void 0);
|
|
2759
2802
|
_defineProperty(this, "event", void 0);
|
|
2760
2803
|
_defineProperty(this, "config", void 0);
|
|
2804
|
+
_defineProperty(this, "authorization", void 0);
|
|
2761
2805
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
2762
2806
|
// 心跳参数,默认为心跳测试
|
|
2763
2807
|
_defineProperty(this, "heartbeatParam", {
|
|
@@ -2773,6 +2817,7 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2773
2817
|
});
|
|
2774
2818
|
_defineProperty(this, "alive", false);
|
|
2775
2819
|
this.config = config;
|
|
2820
|
+
this.authorization = authorization;
|
|
2776
2821
|
this.isReconnect = false;
|
|
2777
2822
|
this.event = event;
|
|
2778
2823
|
// 如果是重连,则拿到重新的会话记录,然后进入重连步骤
|
|
@@ -2816,7 +2861,15 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2816
2861
|
// websocket连接成功,拿到心跳周期
|
|
2817
2862
|
_this.heartbeatInterval = wsRes === null || wsRes === void 0 || (_wsRes$d2 = wsRes.d) === null || _wsRes$d2 === void 0 ? void 0 : _wsRes$d2.heartbeat_interval;
|
|
2818
2863
|
// 非断线重连时,需要鉴权
|
|
2819
|
-
_this.isReconnect ? _this.reconnectWs() : _this.authWs()
|
|
2864
|
+
void (_this.isReconnect ? _this.reconnectWs() : _this.authWs())["catch"](function (error) {
|
|
2865
|
+
var message = error instanceof Error ? error.message : String(error);
|
|
2866
|
+
BotLogger.info('[CLIENT] WebSocket鉴权失败', message);
|
|
2867
|
+
_this.event.emit(SessionEvents.ERROR, {
|
|
2868
|
+
eventType: SessionEvents.ERROR,
|
|
2869
|
+
msg: message
|
|
2870
|
+
});
|
|
2871
|
+
_this.ws.close();
|
|
2872
|
+
});
|
|
2820
2873
|
return;
|
|
2821
2874
|
}
|
|
2822
2875
|
|
|
@@ -2913,30 +2966,47 @@ var Ws = /*#__PURE__*/function () {
|
|
|
2913
2966
|
// 鉴权
|
|
2914
2967
|
}, {
|
|
2915
2968
|
key: "authWs",
|
|
2916
|
-
value: function
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2969
|
+
value: function () {
|
|
2970
|
+
var _authWs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
2971
|
+
var authOp, _t, _t2, _t3, _t4, _t5, _t6;
|
|
2972
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
2973
|
+
while (1) switch (_context.prev = _context.next) {
|
|
2974
|
+
case 0:
|
|
2975
|
+
_t = OpCode.IDENTIFY;
|
|
2976
|
+
_context.next = 1;
|
|
2977
|
+
return this.authorization.getAuthorization();
|
|
2978
|
+
case 1:
|
|
2979
|
+
_t2 = _context.sent;
|
|
2980
|
+
_t3 = this.getValidIntents();
|
|
2981
|
+
_t4 = this.checkShards(this.config.shards) || [0, 1];
|
|
2982
|
+
_t5 = {
|
|
2983
|
+
$os: Properties.os,
|
|
2984
|
+
$browser: Properties.browser,
|
|
2985
|
+
$device: Properties.device
|
|
2986
|
+
};
|
|
2987
|
+
_t6 = {
|
|
2988
|
+
token: _t2,
|
|
2989
|
+
intents: _t3,
|
|
2990
|
+
shard: _t4,
|
|
2991
|
+
properties: _t5
|
|
2992
|
+
};
|
|
2993
|
+
authOp = {
|
|
2994
|
+
op: _t,
|
|
2995
|
+
d: _t6
|
|
2996
|
+
};
|
|
2997
|
+
// 发送鉴权请求
|
|
2998
|
+
this.sendWs(authOp);
|
|
2999
|
+
case 2:
|
|
3000
|
+
case "end":
|
|
3001
|
+
return _context.stop();
|
|
2932
3002
|
}
|
|
2933
|
-
}
|
|
2934
|
-
};
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
// 校验intents类型
|
|
3003
|
+
}, _callee, this);
|
|
3004
|
+
}));
|
|
3005
|
+
function authWs() {
|
|
3006
|
+
return _authWs.apply(this, arguments);
|
|
3007
|
+
}
|
|
3008
|
+
return authWs;
|
|
3009
|
+
}() // 校验intents类型
|
|
2940
3010
|
}, {
|
|
2941
3011
|
key: "getValidIntents",
|
|
2942
3012
|
value: function getValidIntents() {
|
|
@@ -3028,19 +3098,40 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3028
3098
|
// 重新重连Ws
|
|
3029
3099
|
}, {
|
|
3030
3100
|
key: "reconnectWs",
|
|
3031
|
-
value: function
|
|
3032
|
-
var
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3101
|
+
value: function () {
|
|
3102
|
+
var _reconnectWs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
|
3103
|
+
var reconnectParam, _t7, _t8, _t9, _t0, _t1;
|
|
3104
|
+
return _regeneratorRuntime.wrap(function (_context2) {
|
|
3105
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
3106
|
+
case 0:
|
|
3107
|
+
_t7 = OpCode.RESUME;
|
|
3108
|
+
_context2.next = 1;
|
|
3109
|
+
return this.authorization.getAuthorization();
|
|
3110
|
+
case 1:
|
|
3111
|
+
_t8 = _context2.sent;
|
|
3112
|
+
_t9 = this.sessionRecord.sessionID;
|
|
3113
|
+
_t0 = this.sessionRecord.seq;
|
|
3114
|
+
_t1 = {
|
|
3115
|
+
token: _t8,
|
|
3116
|
+
session_id: _t9,
|
|
3117
|
+
seq: _t0
|
|
3118
|
+
};
|
|
3119
|
+
reconnectParam = {
|
|
3120
|
+
op: _t7,
|
|
3121
|
+
d: _t1
|
|
3122
|
+
};
|
|
3123
|
+
this.sendWs(reconnectParam);
|
|
3124
|
+
case 2:
|
|
3125
|
+
case "end":
|
|
3126
|
+
return _context2.stop();
|
|
3127
|
+
}
|
|
3128
|
+
}, _callee2, this);
|
|
3129
|
+
}));
|
|
3130
|
+
function reconnectWs() {
|
|
3131
|
+
return _reconnectWs.apply(this, arguments);
|
|
3132
|
+
}
|
|
3133
|
+
return reconnectWs;
|
|
3134
|
+
}() // OpenAPI事件分发
|
|
3044
3135
|
}, {
|
|
3045
3136
|
key: "dispatchEvent",
|
|
3046
3137
|
value: function dispatchEvent(eventType, wsRes) {
|
|
@@ -3071,7 +3162,10 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3071
3162
|
}, {
|
|
3072
3163
|
key: "handleWsCloseEvent",
|
|
3073
3164
|
value: function handleWsCloseEvent(code) {
|
|
3074
|
-
var
|
|
3165
|
+
var _this$authorization$i,
|
|
3166
|
+
_this$authorization,
|
|
3167
|
+
_this2 = this;
|
|
3168
|
+
if (code === 4004) (_this$authorization$i = (_this$authorization = this.authorization).invalidate) === null || _this$authorization$i === void 0 || _this$authorization$i.call(_this$authorization);
|
|
3075
3169
|
WebsocketCloseReason.forEach(function (e) {
|
|
3076
3170
|
if (e.code === code) {
|
|
3077
3171
|
_this2.event.emit(SessionEvents.ERROR, {
|
|
@@ -3085,14 +3179,16 @@ var Ws = /*#__PURE__*/function () {
|
|
|
3085
3179
|
}();
|
|
3086
3180
|
|
|
3087
3181
|
var Session = /*#__PURE__*/function () {
|
|
3088
|
-
function Session(config, event, sessionRecord) {
|
|
3182
|
+
function Session(config, authorization, event, sessionRecord) {
|
|
3089
3183
|
_classCallCheck(this, Session);
|
|
3090
3184
|
_defineProperty(this, "config", void 0);
|
|
3185
|
+
_defineProperty(this, "authorization", void 0);
|
|
3091
3186
|
_defineProperty(this, "heartbeatInterval", void 0);
|
|
3092
3187
|
_defineProperty(this, "ws", void 0);
|
|
3093
3188
|
_defineProperty(this, "event", void 0);
|
|
3094
3189
|
_defineProperty(this, "sessionRecord", void 0);
|
|
3095
3190
|
this.config = config;
|
|
3191
|
+
this.authorization = authorization;
|
|
3096
3192
|
this.event = event;
|
|
3097
3193
|
// 如果会话记录存在的话,继续透传
|
|
3098
3194
|
if (sessionRecord) {
|
|
@@ -3106,16 +3202,17 @@ var Session = /*#__PURE__*/function () {
|
|
|
3106
3202
|
key: "createSession",
|
|
3107
3203
|
value: function () {
|
|
3108
3204
|
var _createSession = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3109
|
-
var reqOptions, response, wsData, _t;
|
|
3205
|
+
var reqOptions, response, wsData, _response, _t;
|
|
3110
3206
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
3111
3207
|
while (1) switch (_context.prev = _context.next) {
|
|
3112
3208
|
case 0:
|
|
3113
3209
|
_context.prev = 0;
|
|
3114
|
-
this.ws = new Ws(this.config, this.event, this.sessionRecord || undefined);
|
|
3115
|
-
reqOptions = WsObjRequestOptions(this.config.sandbox);
|
|
3210
|
+
this.ws = new Ws(this.config, this.authorization, this.event, this.sessionRecord || undefined);
|
|
3211
|
+
reqOptions = WsObjRequestOptions(this.config.sandbox, this.authorization.apiVersion);
|
|
3116
3212
|
_context.next = 1;
|
|
3117
|
-
return
|
|
3213
|
+
return this.authorization.getAuthorization();
|
|
3118
3214
|
case 1:
|
|
3215
|
+
reqOptions.headers.Authorization = _context.sent;
|
|
3119
3216
|
_context.next = 2;
|
|
3120
3217
|
return resty.create(reqOptions).get(reqOptions.url, {});
|
|
3121
3218
|
case 2:
|
|
@@ -3133,7 +3230,12 @@ var Session = /*#__PURE__*/function () {
|
|
|
3133
3230
|
case 4:
|
|
3134
3231
|
_context.prev = 4;
|
|
3135
3232
|
_t = _context["catch"](0);
|
|
3136
|
-
|
|
3233
|
+
_response = _t === null || _t === void 0 ? void 0 : _t.response;
|
|
3234
|
+
BotLogger.info('[ERROR] createSession: ', {
|
|
3235
|
+
message: _t instanceof Error ? _t.message : String(_t),
|
|
3236
|
+
status: _response === null || _response === void 0 ? void 0 : _response.status,
|
|
3237
|
+
data: _response === null || _response === void 0 ? void 0 : _response.data
|
|
3238
|
+
});
|
|
3137
3239
|
this.event.emit(SessionEvents.EVENT_WS, {
|
|
3138
3240
|
eventType: SessionEvents.DISCONNECT,
|
|
3139
3241
|
eventMsg: this.sessionRecord
|
|
@@ -3161,13 +3263,13 @@ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstruct
|
|
|
3161
3263
|
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
3162
3264
|
var MAX_RETRY = 2;
|
|
3163
3265
|
var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
3164
|
-
function WebsocketClient(config) {
|
|
3266
|
+
function WebsocketClient(config, authorization) {
|
|
3165
3267
|
var _this;
|
|
3166
3268
|
_classCallCheck(this, WebsocketClient);
|
|
3167
3269
|
_this = _callSuper(this, WebsocketClient);
|
|
3168
3270
|
_defineProperty(_this, "session", void 0);
|
|
3169
3271
|
_defineProperty(_this, "retry", 0);
|
|
3170
|
-
_this.connect(config);
|
|
3272
|
+
_this.connect(config, authorization);
|
|
3171
3273
|
_this.on(SessionEvents.EVENT_WS, function (data) {
|
|
3172
3274
|
switch (data.eventType) {
|
|
3173
3275
|
case SessionEvents.RECONNECT:
|
|
@@ -3177,9 +3279,9 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3177
3279
|
if (_this.retry < (config.maxRetry || MAX_RETRY)) {
|
|
3178
3280
|
var _WebsocketCloseReason;
|
|
3179
3281
|
BotLogger.info('[CLIENT] 重新连接中,尝试次数:', _this.retry + 1);
|
|
3180
|
-
_this.connect(config, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3282
|
+
_this.connect(config, authorization, (_WebsocketCloseReason = WebsocketCloseReason.find(function (v) {
|
|
3181
3283
|
return v.code === data.code;
|
|
3182
|
-
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg :
|
|
3284
|
+
})) !== null && _WebsocketCloseReason !== void 0 && _WebsocketCloseReason.resume ? data.eventMsg : undefined);
|
|
3183
3285
|
_this.retry += 1;
|
|
3184
3286
|
} else {
|
|
3185
3287
|
BotLogger.info('[CLIENT] 超过重试次数,连接终止');
|
|
@@ -3202,10 +3304,10 @@ var WebsocketClient = /*#__PURE__*/function (_EventEmitter) {
|
|
|
3202
3304
|
_inherits(WebsocketClient, _EventEmitter);
|
|
3203
3305
|
return _createClass(WebsocketClient, [{
|
|
3204
3306
|
key: "connect",
|
|
3205
|
-
value: function connect(config, sessionRecord) {
|
|
3307
|
+
value: function connect(config, authorization, sessionRecord) {
|
|
3206
3308
|
var event = this;
|
|
3207
3309
|
// 新建一个会话
|
|
3208
|
-
this.session = new Session(config, event, sessionRecord);
|
|
3310
|
+
this.session = new Session(config, authorization, event, sessionRecord);
|
|
3209
3311
|
return this.session;
|
|
3210
3312
|
}
|
|
3211
3313
|
|
|
@@ -3225,10 +3327,38 @@ function createOpenAPIV1(config) {
|
|
|
3225
3327
|
function createOpenAPIV2(config) {
|
|
3226
3328
|
return OpenAPI.newClient(config);
|
|
3227
3329
|
}
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3330
|
+
function createWebsocketV1(config) {
|
|
3331
|
+
return new WebsocketClient(config, {
|
|
3332
|
+
apiVersion: 'v1',
|
|
3333
|
+
getAuthorization: function () {
|
|
3334
|
+
var _getAuthorization = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
3335
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
|
3336
|
+
while (1) switch (_context.prev = _context.next) {
|
|
3337
|
+
case 0:
|
|
3338
|
+
return _context.abrupt("return", getV1Authorization(config.appID, config.token));
|
|
3339
|
+
case 1:
|
|
3340
|
+
case "end":
|
|
3341
|
+
return _context.stop();
|
|
3342
|
+
}
|
|
3343
|
+
}, _callee);
|
|
3344
|
+
}));
|
|
3345
|
+
function getAuthorization() {
|
|
3346
|
+
return _getAuthorization.apply(this, arguments);
|
|
3347
|
+
}
|
|
3348
|
+
return getAuthorization;
|
|
3349
|
+
}()
|
|
3350
|
+
});
|
|
3351
|
+
}
|
|
3352
|
+
function createWebsocketV2(config) {
|
|
3353
|
+
return new WebsocketClient(config, {
|
|
3354
|
+
apiVersion: 'v2',
|
|
3355
|
+
getAuthorization: function getAuthorization() {
|
|
3356
|
+
return getV2Authorization(config);
|
|
3357
|
+
},
|
|
3358
|
+
invalidate: config.clientSecret && !config.accessToken && !config.access_token ? function () {
|
|
3359
|
+
return invalidateV2AccessToken(config.appID);
|
|
3360
|
+
} : undefined
|
|
3361
|
+
});
|
|
3232
3362
|
}
|
|
3233
3363
|
|
|
3234
|
-
export { AvailableIntentsEventsEnum, IntentEvents, Intents, OpCode, SessionEvents, WSCodes, WebsocketCloseReason, WebsocketCode, WsEventType, WsObjRequestOptions, createOpenAPIV1, createOpenAPIV2,
|
|
3364
|
+
export { AvailableIntentsEventsEnum, IntentEvents, Intents, OpCode, SessionEvents, WSCodes, WebsocketCloseReason, WebsocketCode, WsEventType, WsObjRequestOptions, createOpenAPIV1, createOpenAPIV2, createWebsocketV1, createWebsocketV2 };
|