Quellcode durchsuchen

优化任务执行器的群组邀请和退群逻辑,区分处理普通群和超级群的邀请方式,增强错误处理,提升代码可读性和可维护性。

wuyi vor 1 Woche
Ursprung
Commit
0c741b38b9
1 geänderte Dateien mit 17 neuen und 7 gelöschten Zeilen
  1. 17 7
      src/executor/task.executor.ts

+ 17 - 7
src/executor/task.executor.ts

@@ -412,12 +412,18 @@ export class TaskExecutor {
       }
       const chatId = groupEntity.chatId ?? groupEntity.id
       const accessHash = groupEntity.accessHash
-      if (chatId === undefined || chatId === null || accessHash === undefined || accessHash === null) {
-        throw new Error('群拉人任务:群组实体缺少 id/chatId/accessHash(请检查 resolveGroupEntityByInviteLink 返回值)')
+      if (chatId === undefined || chatId === null) {
+        throw new Error('群拉人任务:群组实体缺少 id/chatId(请检查 resolveGroupEntityByInviteLink 返回值)')
       }
 
-      const inputChannel = await workerTgClient.getInputChannel(chatId, accessHash)
-      await workerTgClient.inviteMembersToChannelGroup(inputChannel, [targetUser])
+      // 超级群/频道:有 accessHash,走 channels.InviteToChannel
+      if (accessHash !== undefined && accessHash !== null) {
+        const inputChannel = await workerTgClient.getInputChannel(chatId, accessHash)
+        await workerTgClient.inviteMembersToChannelGroup(inputChannel, [targetUser])
+      } else {
+        // 普通群(Chat):没有 accessHash,走 messages.AddChatUser
+        await workerTgClient.inviteUserToBasicChat(chatId, targetUser)
+      }
 
       await this.taskItemRepo.update(item.id, {
         status: TaskItemStatus.SUCCESS,
@@ -474,10 +480,14 @@ export class TaskExecutor {
     if (!inviteGroupEntity) return
     const chatId = inviteGroupEntity.chatId ?? inviteGroupEntity.id
     const accessHash = inviteGroupEntity.accessHash
-    if (chatId === undefined || chatId === null || accessHash === undefined || accessHash === null) return
     try {
-      const inputChannel = await workerTgClient.getInputChannel(chatId, accessHash)
-      await workerTgClient.leaveGroup(inputChannel)
+      if (chatId === undefined || chatId === null) return
+      if (accessHash !== undefined && accessHash !== null) {
+        const inputChannel = await workerTgClient.getInputChannel(chatId, accessHash)
+        await workerTgClient.leaveGroup(inputChannel)
+      } else {
+        await workerTgClient.leaveBasicChat(chatId)
+      }
     } catch {
       // 忽略退群异常(可能已不在群、权限问题等)
     }