Skip to content

Commit

Permalink
feat: 支持更多 qlottie,支持反向转换表情
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Jul 8, 2024
1 parent 308136d commit 161166b
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 25 deletions.
Binary file removed main/assets/tgs/tgs0.tgs
Binary file not shown.
Binary file removed main/assets/tgs/tgs1.tgs
Binary file not shown.
Binary file removed main/assets/tgs/tgs2.tgs
Binary file not shown.
Binary file removed main/assets/tgs/tgs3.tgs
Binary file not shown.
Binary file removed main/assets/tgs/tgs4.tgs
Binary file not shown.
Binary file removed main/assets/tgs/tgs5.tgs
Binary file not shown.
6 changes: 6 additions & 0 deletions main/src/client/Telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,10 @@ export default class Telegram {
dcId: document.dcId,
});
}

public async getStickerSet(handle: string) {
return await this.client.invoke(new Api.messages.GetStickerSet({
stickerset: new Api.InputStickerSetShortName({ shortName: handle }),
})) as Api.messages.StickerSet;
}
}
14 changes: 7 additions & 7 deletions main/src/constants/lottie.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const TGS = ['打call', '流泪', '变形', '比心', '庆祝', '鞭炮'];
export default {
getTgsIndex(message: string) {
const index1 = TGS.map(text => `[${text}]请使用最新版手机QQ体验新功能`).indexOf(message);
if (index1 > -1) {
return index1;
}
return TGS.map(text => `/${text}`).indexOf(message);
packInfo: {
qlottie_2: [364, 362, 397, 396, 360, 361, 363, 365, 367],
qlottie_3: [413, 405, 404, 406, 411, 407, 408, 412, 409],
qlottie_4: [403, 402, 390, 391, 388, 389, 386, 385, 384, 387],
qlottie_5: [382, 383, 401, 400, 380, 381, 379, 376, 378, 377],
qlottie_6: [399, 398, 373, 370, 375, 368, 369, 371, 372, 374],
QQAniSticker: [5, 311, 312, 319, 320, 339, 137, 346, 344, 345, 181, 74, 75, 349, 350],
},
};
3 changes: 2 additions & 1 deletion main/src/controllers/ForwardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Telegram from '../client/Telegram';
import OicqClient from '../client/OicqClient';
import ForwardService from '../services/ForwardService';
import {
FaceElem,
Friend,
FriendPokeEvent,
GroupMessageEvent,
GroupPokeEvent,
MemberIncreaseEvent,
MemberIncreaseEvent, MessageElem,
PrivateMessageEvent,
} from '@icqqjs/icqq';
import db from '../models/db';
Expand Down
8 changes: 1 addition & 7 deletions main/src/helpers/RecoverMessageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,7 @@ export default class {
let url: string;
switch (elem.type) {
case 'text': {
let tgs = lottie.getTgsIndex(elem.text);
if (tgs === -1) {
text += elem.text;
}
else {
useFile(`${tgs}.tgs`, `assets/tgs/tgs${tgs}.tgs`);
}
text += elem.text;
break;
}
case 'at':
Expand Down
58 changes: 48 additions & 10 deletions main/src/services/ForwardService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Telegram from '../client/Telegram';
import {
FaceElem,
Forwardable,
Group,
GroupMessageEvent,
Expand Down Expand Up @@ -96,6 +97,38 @@ export default class ForwardService {
});
});
}
this.initStickerPack().then(() => this.log.info('Sticker Pack 初始化完成'));
}

private readonly stickerPackMap: Record<keyof typeof lottie.packInfo, Api.Document[]> = {} as any;

private async initStickerPack() {
for (const handle of Object.keys(lottie.packInfo)) {
const pack = await this.tgBot.getStickerSet(handle);
this.stickerPackMap[handle] = pack.documents;
}
}

private getStickerByQQFaceId(id: number) {
for (const [pack, ids] of Object.entries(lottie.packInfo)) {
if (ids.includes(id as any)) {
if (this.stickerPackMap[pack])
return this.stickerPackMap[pack][ids.indexOf(id)] as Api.Document;
}
}
}

private getFaceByTgFileId(fileId: BigInteger.BigNumber): FaceElem | undefined {
for (const [pack, documents] of Object.entries(this.stickerPackMap)) {
for (const document of documents) {
if (document.id.eq(fileId))
return {
type: 'face',
id: lottie.packInfo[pack][documents.indexOf(document)],
stickerType: 1,
};
}
}
}

public async forwardFromQq(event: PrivateMessageEvent | GroupMessageEvent, pair: Pair) {
Expand Down Expand Up @@ -169,14 +202,7 @@ export default class ForwardService {
instantViewUrl.searchParams.set('rhash', '45756f9b0bb3c6');
message += `<a href="${instantViewUrl}">\u200e</a>`;
}
// 判断 tgs 表情
let tgs = lottie.getTgsIndex(elem.text);
if (tgs === -1) {
message += helper.htmlEscape(elem.text);
}
else {
useSticker(`assets/tgs/tgs${tgs}.tgs`);
}
message += helper.htmlEscape(elem.text);
break;
}
case 'at': {
Expand All @@ -188,6 +214,12 @@ export default class ForwardService {
}
}
case 'face':
// 判断 tgs 表情
const tgs = this.getStickerByQQFaceId(elem.id as number);
if (tgs) {
useSticker(tgs);
break;
}
case 'sface': {
message += `[<i>${helper.htmlEscape(elem.text)}</i>]`;
break;
Expand Down Expand Up @@ -597,8 +629,14 @@ export default class ForwardService {
}
else if (message.sticker) {
// 一定是 tgs
const gifPath = await convert.tgs2gif(message.sticker.id.toString(16), () => message.downloadMedia({}));
useImage(await fsP.readFile(gifPath), true);
const face = this.getFaceByTgFileId(message.sticker.id);
if (face) {
chain.push(face);
}
else {
const gifPath = await convert.tgs2gif(message.sticker.id.toString(16), () => message.downloadMedia({}));
useImage(await fsP.readFile(gifPath), true);
}
markdownCompatible = false;
brief += '[贴纸]';
}
Expand Down

0 comments on commit 161166b

Please sign in to comment.