|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
};
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
const axios_1 = require("axios");
|
|
|
-const crypto = require("crypto");
|
|
|
const mirai_ts_1 = require("mirai-ts");
|
|
|
const message_1 = require("mirai-ts/dist/message");
|
|
|
const helper_1 = require("./helper");
|
|
@@ -24,44 +23,33 @@ const ChatTypeMap = {
|
|
|
exports.MiraiMessage = message_1.default;
|
|
|
class default_1 {
|
|
|
constructor(opt) {
|
|
|
- this.revokeList = new Set();
|
|
|
- this.sendTo = (subscriber, msg, timeout) => {
|
|
|
- const msgId = `${new Date().getTime()}${crypto.randomBytes(8).toString('hex')}`;
|
|
|
- timeout = Math.floor(timeout);
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- if (timeout === 0 || timeout < -1 || timeout > 0xFFFFFFFF) {
|
|
|
- reject(`Error: timeout must be between 1 - ${0xFFFFFFFF} ms`);
|
|
|
+ this.sendTo = (subscriber, msg, timeout = -1) => (() => {
|
|
|
+ if (timeout)
|
|
|
+ timeout = Math.floor(timeout);
|
|
|
+ if (timeout === 0 || timeout < -1) {
|
|
|
+ return Promise.reject('Error: timeout must be greater than 0ms');
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ this.bot.axios.defaults.timeout = timeout === -1 ? 0 : timeout;
|
|
|
+ switch (subscriber.chatType) {
|
|
|
+ case 'group':
|
|
|
+ return this.bot.api.sendGroupMessage(msg, subscriber.chatID);
|
|
|
+ case 'private':
|
|
|
+ return this.bot.api.sendFriendMessage(msg, subscriber.chatID);
|
|
|
}
|
|
|
- (() => {
|
|
|
- switch (subscriber.chatType) {
|
|
|
- case 'group':
|
|
|
- return this.bot.api.sendGroupMessage(msg, subscriber.chatID);
|
|
|
- case 'private':
|
|
|
- return this.bot.api.sendFriendMessage(msg, subscriber.chatID);
|
|
|
- }
|
|
|
- })().then(response => {
|
|
|
- if (this.revokeList.has(msgId)) {
|
|
|
- this.bot.api.recall(response.messageId)
|
|
|
- .then(() => logger.info(`overdue message to ${subscriber.chatID} recalled`))
|
|
|
- .catch(() => logger.info(`error recalling overdue message to ${subscriber.chatID}`))
|
|
|
- .finally(() => this.revokeList.delete(msgId));
|
|
|
- }
|
|
|
- resolve(response);
|
|
|
- }).catch(reject);
|
|
|
- setTimeout(() => {
|
|
|
- this.revokeList.add(msgId);
|
|
|
- reject('Error: timed out, requesting termination');
|
|
|
- }, timeout);
|
|
|
- })
|
|
|
- .then(response => {
|
|
|
- logger.info(`pushing data to ${subscriber.chatID} was successful, response:`);
|
|
|
- logger.info(response);
|
|
|
- })
|
|
|
- .catch(reason => {
|
|
|
- logger.error(`error pushing data to ${subscriber.chatID}, reason: ${reason}`);
|
|
|
- throw Error(reason);
|
|
|
- });
|
|
|
- };
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ this.bot.axios.defaults.timeout = 0;
|
|
|
+ }
|
|
|
+ })()
|
|
|
+ .then(response => {
|
|
|
+ logger.info(`pushing data to ${subscriber.chatID} was successful, response:`);
|
|
|
+ logger.info(response);
|
|
|
+ })
|
|
|
+ .catch(reason => {
|
|
|
+ logger.error(`error pushing data to ${subscriber.chatID}, reason: ${reason}`);
|
|
|
+ throw Error(reason);
|
|
|
+ });
|
|
|
this.initBot = () => {
|
|
|
this.bot = new mirai_ts_1.default({
|
|
|
authKey: this.botInfo.access_token,
|