From 3ca022088d9816bcbd99c0440c8df1929fd691f3 Mon Sep 17 00:00:00 2001 From: clansty Date: Sun, 14 Aug 2022 12:09:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=9A=E8=BF=87=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=90=AF=E7=94=A8=E5=92=8C=E5=85=B3=E9=97=AD=E8=BD=AC=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prisma/schema.prisma | 1 + src/constants/commands.ts | 8 ++++ src/controllers/ForwardController.ts | 2 + src/controllers/InChatCommandsController.ts | 8 ++++ src/models/ForwardPairs.ts | 4 +- src/models/Pair.ts | 44 ++++++++++++++++++++- 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cfda770..61f397a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -74,6 +74,7 @@ model ForwardPair { instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) joinNotice Boolean @default(true) poke Boolean @default(true) + enable Boolean @default(true) @@unique([qqRoomId, instanceId]) @@unique([tgChatId, instanceId]) diff --git a/src/constants/commands.ts b/src/constants/commands.ts index 5582654..b23a28a 100644 --- a/src/constants/commands.ts +++ b/src/constants/commands.ts @@ -47,6 +47,14 @@ const groupInChatCommands = [ command: 'info', description: '查看本群或选定消息的详情', }), + new Api.BotCommand({ + command: 'forwardoff', + description: '暂停消息转发', + }), + new Api.BotCommand({ + command: 'forwardon', + description: '恢复消息转发', + }), ]; const personalInChatCommands = [ diff --git a/src/controllers/ForwardController.ts b/src/controllers/ForwardController.ts index 4a2c070..c802fc4 100644 --- a/src/controllers/ForwardController.ts +++ b/src/controllers/ForwardController.ts @@ -41,6 +41,7 @@ export default class ForwardController { const target = event.message_type === 'private' ? event.friend : event.group; const pair = this.instance.forwardPairs.find(target); if (!pair) return; + if (!pair.enable) return; const tgMessages = await this.forwardService.forwardFromQq(event, pair); for (const tgMessage of tgMessages) { // 更新数据库 @@ -70,6 +71,7 @@ export default class ForwardController { if (message.senderId?.eq(this.instance.botMe.id)) return true; const pair = this.instance.forwardPairs.find(message.chat); if (!pair) return false; + if (!pair.enable) return; const qqMessagesSent = await this.forwardService.forwardFromTelegram(message, pair); if (qqMessagesSent) { // 更新数据库 diff --git a/src/controllers/InChatCommandsController.ts b/src/controllers/InChatCommandsController.ts index d1b6be6..13ba101 100644 --- a/src/controllers/InChatCommandsController.ts +++ b/src/controllers/InChatCommandsController.ts @@ -36,6 +36,14 @@ export default class InChatCommandsController { case '/poke': await this.service.poke(message, pair); return true; + case '/forwardoff': + pair.enable = false; + await message.reply({ message: '转发已禁用' }); + return true; + case '/forwardon': + pair.enable = true; + await message.reply({ message: '转发已启用' }); + return true; case '/refresh': if (this.instance.workMode !== 'personal' || !message.senderId?.eq(this.instance.owner)) return false; await pair.updateInfo(); diff --git a/src/models/ForwardPairs.ts b/src/models/ForwardPairs.ts index e248e2a..2efd95a 100644 --- a/src/models/ForwardPairs.ts +++ b/src/models/ForwardPairs.ts @@ -26,7 +26,7 @@ export default class ForwardPairs { const qq = oicq.getChat(Number(i.qqRoomId)); const tg = await tgBot.getChat(Number(i.tgChatId)); if (qq && tg) { - this.pairs.push(new Pair(qq, tg, i.id, i.joinNotice, i.poke)); + this.pairs.push(new Pair(qq, tg, i.id, i.joinNotice, i.poke, i.enable)); } } catch (e) { @@ -49,7 +49,7 @@ export default class ForwardPairs { instanceId: this.instanceId, }, }); - this.pairs.push(new Pair(qq, tg, dbEntry.id, true, true)); + this.pairs.push(new Pair(qq, tg, dbEntry.id, true, true, true)); return dbEntry; } diff --git a/src/models/Pair.ts b/src/models/Pair.ts index 32abf38..aea4889 100644 --- a/src/models/Pair.ts +++ b/src/models/Pair.ts @@ -12,8 +12,9 @@ export class Pair { constructor(public readonly qq: Friend | Group, private _tg: TelegramChat, public dbId: number, - public joinNotice: boolean, - public poke: boolean) { + private _joinNotice: boolean, + private _poke: boolean, + private _enable: boolean) { } // 更新 TG 群组的头像和简介 @@ -56,4 +57,43 @@ export class Pair { }) .then(() => log.info(`出现了到超级群组的转换: ${value.id}`)); } + + get joinNotice() { + return this._joinNotice; + } + + set joinNotice(value) { + this._joinNotice = value; + db.forwardPair.update({ + where: { id: this.dbId }, + data: { joinNotice: value }, + }) + .then(() => 0); + } + + get poke() { + return this._poke; + } + + set poke(value) { + this._poke = value; + db.forwardPair.update({ + where: { id: this.dbId }, + data: { poke: value }, + }) + .then(() => 0); + } + + get enable() { + return this._enable; + } + + set enable(value) { + this._enable = value; + db.forwardPair.update({ + where: { id: this.dbId }, + data: { enable: value }, + }) + .then(() => 0); + } }