Переглянути джерело

重构任务执行器,优化群组邀请逻辑,移除不必要的参数,更新延迟时间范围,增强错误处理信息的准确性。

wuyi 1 тиждень тому
батько
коміт
30fb17c705
2 змінених файлів з 20 додано та 56 видалено
  1. 19 55
      src/executor/task.executor.ts
  2. 1 1
      src/services/clients/tg-client.manager.ts

+ 19 - 55
src/executor/task.executor.ts

@@ -173,24 +173,15 @@ export class TaskExecutor {
 
           accountUsageInRound = 0
           inviteGroupEntity = null
-
-          // 延迟
-          const delaySeconds = this.getRandomDelaySeconds()
-          this.app.log.info(`延迟 ${delaySeconds}s 后开始处理任务`)
-          await this.sleep(delaySeconds * 1000)
-
-          // 群拉人任务:每次换号后,确保已加入目标群并拿到群实体
-          if (task.type === TaskType.INVITE_TO_GROUP) {
-            const inviteLink = String(task.payload?.inviteLink ?? '').trim()
-            if (!inviteLink) {
-              throw new Error('邀请链接为空,请检查 task.payload.inviteLink')
-            }
-            inviteGroupEntity = await workerTgClient.resolveGroupEntityByInviteLink(inviteLink)
-          }
         }
 
+        // 延迟
+        const delaySeconds = this.getRandomDelaySeconds()
+        this.app.log.info(`延迟 ${delaySeconds}s 后开始处理任务`)
+        await this.sleep(delaySeconds * 1000)
+
         try {
-          await this.processTaskItem(task, taskItem, tgUser, workerTgClient, inviteGroupEntity)
+          await this.processTaskItem(task, taskItem, tgUser, workerTgClient)
         } catch (error) {
           const msg = error instanceof Error ? error.message : '未知错误'
           if (tgUser && this.isSessionRevokedMessage(msg)) {
@@ -263,11 +254,10 @@ export class TaskExecutor {
     task: Task,
     item: TaskItem,
     sender: TgUser,
-    workerTgClient: TgClientManager,
-    inviteGroupEntity: any | null
+    workerTgClient: TgClientManager
   ): Promise<void> {
     if (task.type === TaskType.INVITE_TO_GROUP) {
-      return await this.processInviteToGroup(task, item, sender, workerTgClient, inviteGroupEntity)
+      return await this.processInviteToGroup(task, item, sender, workerTgClient)
     }
 
     return await this.processSendMessage(task, item, sender, workerTgClient)
@@ -344,12 +334,12 @@ export class TaskExecutor {
     task: Task,
     item: TaskItem,
     sender: TgUser,
-    workerTgClient: TgClientManager,
-    inviteGroupEntity: any | null
+    workerTgClient: TgClientManager
   ): Promise<void> {
     try {
-      if (!inviteGroupEntity) {
-        throw new Error('未获取到群组实体(inviteGroupEntity 为空)')
+      const inviteLink = String(task.payload?.inviteLink ?? '').trim()
+      if (!inviteLink) {
+        throw new Error('群拉人任务:邀请链接为空,请检查任务配置信息')
       }
 
       const parsedTarget = this.parseTarget(item.target)
@@ -367,40 +357,14 @@ export class TaskExecutor {
         throw new Error('TelegramClient 未连接')
       }
 
-      const isChannel = inviteGroupEntity?.className === 'Channel'
-      const isChat = inviteGroupEntity?.className === 'Chat'
-      if (!isChannel && !isChat) {
-        throw new Error('目标并非群组或频道,无法邀请成员')
+      // tgUser 加入群组,获取群组实体
+      const inviteGroupEntity = await workerTgClient.resolveGroupEntityByInviteLink(inviteLink)
+      if (!inviteGroupEntity) {
+        throw new Error('群拉人任务:未获取到群组实体(inviteGroupEntity 为空)')
       }
 
-      const inputUser = new Api.InputUser({
-        userId: targetUser.id,
-        accessHash: targetUser.accessHash || BigInt(0)
-      })
-
-      if (isChannel) {
-        if (!inviteGroupEntity?.accessHash) {
-          throw new Error('缺少 accessHash,无法邀请到频道/超级群组')
-        }
-        const inputChannel = new Api.InputChannel({
-          channelId: inviteGroupEntity.id,
-          accessHash: inviteGroupEntity.accessHash
-        })
-        await client.invoke(
-          new Api.channels.InviteToChannel({
-            channel: inputChannel,
-            users: [inputUser]
-          })
-        )
-      } else {
-        await client.invoke(
-          new Api.messages.AddChatUser({
-            chatId: inviteGroupEntity.id,
-            userId: inputUser,
-            fwdLimit: 0
-          })
-        )
-      }
+      const inputChannel = await workerTgClient.getInputChannel(inviteGroupEntity.chatId, inviteGroupEntity.accessHash)
+      await workerTgClient.inviteMembersToChannelGroup(inputChannel, [targetUser])
 
       await this.taskItemRepo.update(item.id, {
         status: TaskItemStatus.SUCCESS,
@@ -647,7 +611,7 @@ export class TaskExecutor {
   /**
    * 获取随机延迟秒数
    */
-  private getRandomDelaySeconds(min: number = 10, max: number = 20): number {
+  private getRandomDelaySeconds(min: number = 5, max: number = 10): number {
     return Math.floor(Math.random() * (max - min + 1)) + min
   }
 

+ 1 - 1
src/services/clients/tg-client.manager.ts

@@ -354,7 +354,7 @@ export class TgClientManager {
   }
 
   /**
-   * 通过邀请链接获取群组实体
+   * 通过邀请链接进群后获取群组实体
    * - 用于“拉人进群”任务:需要拿到 chat/channel 实体,进而拿到 accessHash
    */
   async resolveGroupEntityByInviteLink(inviteLink: string): Promise<any> {