|
@@ -42,6 +42,25 @@ export default class {
|
|
|
private app: App;
|
|
|
public bot: Bot;
|
|
|
|
|
|
+ private messageQueues: {[key: string]: (() => Promise<void>)[]} = {};
|
|
|
+
|
|
|
+ private next = (type: 'private' | 'group', id: string) => {
|
|
|
+ const queue = this.messageQueues[`${type}:${id}`];
|
|
|
+ if (queue && queue.length) {
|
|
|
+ queue[0]().then(() => queue.shift()).then(() => {
|
|
|
+ if (!queue.length) delete this.messageQueues[`${type}:${id}`];
|
|
|
+ else this.next(type, id);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ private enqueue = (type: 'private' | 'group', id: string, resolver: () => Promise<void>) => {
|
|
|
+ const queue = this.messageQueues[`${type}:${id}`] ||= [];
|
|
|
+ queue.push(() => sleep(200).then(resolver));
|
|
|
+ logger.debug(`no. of message currently queued for ${type}:${id}: ${queue.length}`);
|
|
|
+ if (queue.length === 1) this.next(type, id);
|
|
|
+ };
|
|
|
+
|
|
|
private getChat = async (session: Session): Promise<IChat> => {
|
|
|
switch (session.subtype) {
|
|
|
case 'private':
|
|
@@ -69,22 +88,30 @@ export default class {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ private sendToGroup = (groupID: string, message: string) => new Promise<string>(resolve => {
|
|
|
+ this.enqueue('group', groupID, () => this.bot.sendMessage(groupID, message).then(resolve));
|
|
|
+ });
|
|
|
+
|
|
|
+ private sendToUser = (userID: string, message: string) => new Promise<string>(resolve => {
|
|
|
+ this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve));
|
|
|
+ });
|
|
|
+
|
|
|
public sendTo = (subscriber: IChat, messageChain: string) => chainPromises(
|
|
|
(splitted => [splitted.message, ...splitted.attachments])(
|
|
|
Message.separateAttachment(messageChain)
|
|
|
).map(msg => {
|
|
|
switch (subscriber.chatType) {
|
|
|
case 'group':
|
|
|
- return this.bot.sendMessage(subscriber.chatID.toString(), msg);
|
|
|
+ return this.sendToGroup(subscriber.chatID.toString(), msg);
|
|
|
case 'private':
|
|
|
- return this.bot.sendPrivateMessage(subscriber.chatID.toString(), msg);
|
|
|
+ return this.sendToUser(subscriber.chatID.toString(), msg);
|
|
|
case 'temp': // currently unable to open session, awaiting OneBot v12
|
|
|
- return this.bot.sendPrivateMessage(subscriber.chatID.qq.toString(), msg);
|
|
|
+ return this.sendToUser(subscriber.chatID.qq.toString(), msg);
|
|
|
}
|
|
|
}))
|
|
|
.then(response => {
|
|
|
- logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response:`);
|
|
|
- logger.info(response);
|
|
|
+ if (response === undefined) return;
|
|
|
+ logger.info(`pushing data to ${JSON.stringify(subscriber.chatID)} was successful, response: ${response}`);
|
|
|
})
|
|
|
.catch(reason => {
|
|
|
reason = Message.ellipseBase64(reason);
|
|
@@ -135,7 +162,7 @@ export default class {
|
|
|
this.app.middleware(async session => {
|
|
|
const chat = await this.getChat(session);
|
|
|
const cmdObj = parseCmd(session.content);
|
|
|
- const reply = async msg => session.send(msg);
|
|
|
+ const reply = async msg => session.sendQueued(msg);
|
|
|
switch (cmdObj.cmd) {
|
|
|
case 'twitter_view':
|
|
|
case 'twitter_get':
|