From 02090e5ae2fda345b20eff1b6b9c17e6789c2e9b Mon Sep 17 00:00:00 2001 From: meoacgx Date: Fri, 19 Dec 2025 18:45:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=BB=9F=E8=AE=A1=E4=B8=8E=E5=88=A0=E9=99=A4?= =?UTF-8?q?=EF=BC=9A=E4=BB=85=E7=BB=9F=E8=AE=A1=E9=A2=91=E9=81=93=E5=B9=B6?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E5=B9=B6=E5=8F=91=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/BotManagementService.cs | 14 ++++++++++++-- .../Services/Telegram/BotTelegramService.cs | 14 +++++++++----- src/TelegramPanel.Web/Components/Pages/Bots.razor | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) 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") : "-"))