diff --git a/src/TelegramPanel.Core/Services/BotManagementService.cs b/src/TelegramPanel.Core/Services/BotManagementService.cs index 7f1f313..28a3873 100644 --- a/src/TelegramPanel.Core/Services/BotManagementService.cs +++ b/src/TelegramPanel.Core/Services/BotManagementService.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using TelegramPanel.Data.Entities; using TelegramPanel.Data.Repositories; @@ -75,7 +76,14 @@ public class BotManagementService if (bot == null) return; - await _botRepository.DeleteAsync(bot); + try + { + await _botRepository.DeleteAsync(bot); + } + catch (DbUpdateConcurrencyException) + { + // 视为已被删除(幂等) + } } public async Task> GetCategoriesAsync(int botId) @@ -119,7 +127,9 @@ public class BotManagementService public async Task> GetChannelsAsync(int botId, int? categoryId = null) { - return await _botChannelRepository.GetForBotAsync(botId, categoryId); + // Bot 频道列表仅展示“频道”(不展示群组/超级群组) + var list = await _botChannelRepository.GetForBotAsync(botId, categoryId); + return list.Where(x => x.IsBroadcast); } public async Task UpsertChannelAsync(BotChannel channel) diff --git a/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs b/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs index 4a1f377..03e95c4 100644 --- a/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs +++ b/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs @@ -52,7 +52,7 @@ public class BotTelegramService if (result.ValueKind != JsonValueKind.Array) return 0; - // Telegram 会针对同一个频道连续产生多条 my_chat_member(例如 member -> administrator), + // Telegram 会针对同一个 chat 连续产生多条 my_chat_member(例如 member -> administrator), // 这里按 chat_id 去重并只应用“最后一次状态”,避免“新增两个”的错觉/重复写库。 var changesByChatId = new Dictionary(); long? maxUpdateId = null; @@ -76,10 +76,6 @@ public class BotTelegramService if (!TryParseChatMemberUpdate(myChatMember, out var chat, out var status)) continue; - // 仅同步“频道”(不包含群组/超级群组),避免把讨论群等误当成频道条目 - if (chat.Type is not "channel") - continue; - changesByChatId[chat.Id] = new BotChatMemberChange(chat, status); } @@ -91,6 +87,14 @@ public class BotTelegramService var chatId = kv.Key; var change = kv.Value; + // 本系统 Bot 只管理频道(channel);如果收到群组/超级群组更新,则仅用于“清理旧数据”,避免列表统计被污染。 + if (change.Chat.Type is not "channel") + { + await _botManagement.DeleteChannelByTelegramIdAsync(bot.Id, chatId); + affected++; + continue; + } + // 只把“具备管理员/创建者权限”的频道纳入 Bot 列表; // left/kicked 或降权为 member 等,直接从列表移除(否则导出邀请会失败且列表不准)。 if (change.Status is "administrator" or "creator") diff --git a/src/TelegramPanel.Web/Components/Pages/Bots.razor b/src/TelegramPanel.Web/Components/Pages/Bots.razor index beaf7ed..cab0ef9 100644 --- a/src/TelegramPanel.Web/Components/Pages/Bots.razor +++ b/src/TelegramPanel.Web/Components/Pages/Bots.razor @@ -49,7 +49,7 @@ @context.Name @(!string.IsNullOrWhiteSpace(context.Username) ? $"@{context.Username}" : "-") - @context.Channels.Count + @context.Channels.Count(x => x.IsBroadcast) @context.Categories.Count @((context.LastSyncAt.HasValue ? context.LastSyncAt.Value.ToString("yyyy-MM-dd HH:mm") : "-"))