clansty / Q2TG

QQ 与 Telegram 群相互转发的 bot
https://t.me/Q2TGbot
GNU Affero General Public License v3.0
748 stars 77 forks source link

[Code Asking] Meaning of RecoverMessageHelper.ts #87

Closed Nofated095 closed 1 year ago

Nofated095 commented 1 year ago

如题所示,我本人正在研究类似于 #83 中,在 rainbowcat 中去 Userbot 模式,但是我在尝试去 Userbot 时有些无法理解 RecoverMessageHelper.ts 中 line 244 - 333 中的功能

private async importMessagesAndMedia() {
    const tgChatForUser = await this.tgUser.getChat(this.pair.tgId);
    const txtBuffer = Buffer.from(this.importTxt, 'utf-8');
    const importSession = await tgChatForUser.startImportSession(
      new CustomFile('record.txt', txtBuffer.length, '', txtBuffer),
      Object.keys(this.filesMap).length,
    );
    this.currentStatus = 'uploadMedia';
    await this.updateStatusMessage();
    const { fileTypeFromFile } = await (Function('return import("file-type")')() as Promise<typeof import('file-type')>);
    for (const [fileKey, filePath] of Object.entries(this.filesMap)) {
      let type = fileKey.endsWith('.tgs') ? {
        ext: 'tgs',
        mime: 'application/x-tgsticker',
      } : await fileTypeFromFile(filePath);
      if(!type){
        type = {
          ext: 'bin',
          mime: 'application/octet-stream',
        }
      }
      let media: Api.TypeInputMedia;
      if (['.webp', '.tgs'].includes(path.extname(filePath))) {
        // 贴纸
        media = new Api.InputMediaUploadedDocument({
          file: await importSession.uploadFile(new CustomFile(
            fileKey,
            (await fsP.stat(filePath)).size,
            filePath,
          )),
          mimeType: type.mime,
          attributes: [],
        });
      }
      else if (type.mime.startsWith('audio/')) {
        // 语音
        media = new Api.InputMediaUploadedDocument({
          file: await importSession.uploadFile(new CustomFile(
            fileKey,
            (await fsP.stat(filePath)).size,
            filePath,
          )),
          mimeType: type.mime,
          attributes: [
            new Api.DocumentAttributeAudio({
              duration: 0,
              voice: true,
            }),
          ],
        });
      }
      else if (type.ext === 'gif') {
        media = new Api.InputMediaUploadedDocument({
          file: await importSession.uploadFile(new CustomFile(
            fileKey,
            (await fsP.stat(filePath)).size,
            filePath,
          )),
          mimeType: type.mime,
          attributes: [new Api.DocumentAttributeAnimated()],
        });
      }
      else if (type.mime.startsWith('image/')) {
        media = new Api.InputMediaUploadedPhoto({
          file: await importSession.uploadFile(new CustomFile(
            fileKey,
            (await fsP.stat(filePath)).size,
            filePath,
          )),
        });
      }
      else {
        media = new Api.InputMediaUploadedDocument({
          file: await importSession.uploadFile(new CustomFile(
            fileKey,
            (await fsP.stat(filePath)).size,
            filePath,
          )),
          mimeType: type.mime,
          attributes: [],
        });
      }
      await importSession.uploadMedia(fileKey, media);
      this.mediaUploadedCount++;
      await this.updateStatusMessage();
    }
    this.currentStatus = 'finishing';
    await this.updateStatusMessage();
    await importSession.finish();
  }

请问是有关于文件格式转换吗?以及可以请描述一下 Userbot 在此的作用吗?非常感谢