From fdbfda72d40310e94abfb81f865eabd6f4491694 Mon Sep 17 00:00:00 2001 From: Clansty Date: Mon, 12 Feb 2024 22:38:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20/q=20=E5=90=8E=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=9C=A8=20tg=20pin=20=E5=8E=9F=E6=B6=88=E6=81=AF=EF=BC=8C?= =?UTF-8?q?=E5=9C=A8=20QQ=20=E5=B0=86=E5=8E=9F=E6=B6=88=E6=81=AF=E8=AE=BE?= =?UTF-8?q?=E4=B8=BA=E7=B2=BE=E5=8D=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- .idea/codeStyles/codeStyleConfig.xml | 5 +++ src/constants/flags.ts | 1 + src/controllers/QuotLyController.ts | 60 ++++++++++++++++++---------- 4 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.gitignore b/.gitignore index 3b9e8df..2228367 100644 --- a/.gitignore +++ b/.gitignore @@ -728,7 +728,7 @@ Network Trash Folder Temporary Items .apdisk -data/ +data config.yaml build .yarn/* diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/src/constants/flags.ts b/src/constants/flags.ts index 4d52af4..62ba5b1 100644 --- a/src/constants/flags.ts +++ b/src/constants/flags.ts @@ -7,6 +7,7 @@ enum flags { NO_AUTO_CREATE_PM = 1 << 5, COLOR_EMOJI_PREFIX = 1 << 6, RICH_HEADER = 1 << 7, + NO_QUOTE_PIN = 1 << 8, } export default flags; diff --git a/src/controllers/QuotLyController.ts b/src/controllers/QuotLyController.ts index 5a5a836..99efbe6 100644 --- a/src/controllers/QuotLyController.ts +++ b/src/controllers/QuotLyController.ts @@ -2,7 +2,7 @@ import Instance from '../models/Instance'; import Telegram from '../client/Telegram'; import OicqClient from '../client/OicqClient'; import { getLogger, Logger } from 'log4js'; -import { GroupMessageEvent, PrivateMessageEvent } from 'icqq'; +import { Group, GroupMessageEvent, PrivateMessageEvent } from 'icqq'; import { Api } from 'telegram'; import quotly from 'quote-api/methods/generate.js'; import { CustomFile } from 'telegram/client/uploads'; @@ -13,6 +13,7 @@ import { getAvatarUrl } from '../utils/urls'; import convert from '../helpers/convert'; import { Pair } from '../models/Pair'; import env from '../models/env'; +import flags from '../constants/flags'; export default class { private readonly log: Logger; @@ -55,16 +56,14 @@ export default class { this.log.error('找不到 sourceMessage'); return true; } - setTimeout(async () => { - // 异步发送,为了让 /q 先到达 - try { - await this.sendQuote(pair, sourceMessage); - } - catch (e) { - this.log.error(e); - await event.reply(e.toString(), true); - } - }, 50); + if (!((pair.flags | this.instance.flags) & flags.NO_QUOTE_PIN)) { + this.pinMessageOnBothSide(pair, sourceMessage).then(); + } + // 异步发送,为了让 /q 先到达 + this.sendQuote(pair, sourceMessage).catch(async e => { + this.log.error(e); + await event.reply(e.toString(), true); + }); }; private onTelegramMessage = async (message: Api.Message) => { @@ -91,22 +90,39 @@ export default class { this.log.error('找不到 sourceMessage'); return true; } - setTimeout(async () => { - try { - await this.sendQuote(pair, sourceMessage); - } - catch (e) { - this.log.error(e); - await message.reply({ - message: e.toString(), - }); - } - }, 50); + if (!((pair.flags | this.instance.flags) & flags.NO_QUOTE_PIN)) { + this.pinMessageOnBothSide(pair, sourceMessage).then(); + } + // 异步发送,为了让 /q 先到达 + this.sendQuote(pair, sourceMessage).catch(async e => { + this.log.error(e); + await message.reply({ + message: e.toString(), + }); + }); // 个人模式下,/q 这条消息不转发到 QQ,怪话图只有自己可见 if (this.instance.workMode === 'personal') return true; }; + private async pinMessageOnBothSide(pair: Pair, sourceMessage: Awaited>) { + if (pair.qq instanceof Group) { + try { + await pair.qq.addEssence(sourceMessage.seq, Number(sourceMessage.rand)); + } + catch (e) { + this.log.warn('无法添加精华消息,群:', pair.qqRoomId, e); + } + } + try { + const tgMessage = await pair.tg.getMessage({ ids: sourceMessage.tgMsgId }); + await tgMessage.pin({ notify: false, pmOneSide: false }); + } + catch (e) { + this.log.warn('无法置顶消息,群:', pair.tgId, '消息 ID:', sourceMessage.tgMsgId, e); + } + } + private async genQuote(message: Message) { const GROUP_ANONYMOUS_BOT = 1087968824n;