diff --git a/src/Program.cs b/src/Program.cs index 3ba77fa..53d5b49 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -23,119 +23,113 @@ })); botClient.StartReceiving(async (_, update, _) => { - try + switch (update.Type) { - switch (update.Type) - { - case UpdateType.CallbackQuery: + case UpdateType.CallbackQuery: + { + Internationalization lang = (config.EnableAutoI18n && !string.IsNullOrEmpty(update.CallbackQuery.From.LanguageCode)) ? i18nHelper.TryGetLanguageData(update.CallbackQuery.From.LanguageCode, out Internationalization value) ? value : i18nHelper[CultureInfo.CurrentCulture.Name] : i18nHelper[CultureInfo.CurrentCulture.Name]; + if (!groupData.TryGetValue(update.CallbackQuery.Message.Chat.Id, out Dictionary data) + || !data.TryGetValue(update.CallbackQuery.Message.MessageId, out long userId) + || userId != update.CallbackQuery.From.Id) { - Internationalization lang = (config.EnableAutoI18n && !string.IsNullOrEmpty(update.CallbackQuery.From.LanguageCode)) ? i18nHelper.TryGetLanguageData(update.CallbackQuery.From.LanguageCode, out Internationalization value) ? value : i18nHelper[CultureInfo.CurrentCulture.Name] : i18nHelper[CultureInfo.CurrentCulture.Name]; - if (!groupData.TryGetValue(update.CallbackQuery.Message.Chat.Id, out Dictionary data) - || !data.TryGetValue(update.CallbackQuery.Message.MessageId, out long userId) - || userId != update.CallbackQuery.From.Id) - { - await botClient.AnswerCallbackQueryAsync(update.CallbackQuery.Id, lang["Failed"]); - break; - } - try - { - await botClient.ApproveChatJoinRequest(update.CallbackQuery.Message.Chat.Id, data[update.CallbackQuery.Message.MessageId]); - } - catch (ApiRequestException) - { - await botClient.AnswerCallbackQueryAsync(update.CallbackQuery.Id, lang["Failed"]); - break; - } - await botClient.AnswerCallbackQueryAsync(update.CallbackQuery.Id, lang["Pass"]); + await botClient.AnswerCallbackQueryAsync(update.CallbackQuery.Id, lang["Failed"]); break; } - case UpdateType.Message: + try { - switch (update.Message.Type) - { - case MessageType.Text: + await botClient.ApproveChatJoinRequest(update.CallbackQuery.Message.Chat.Id, data[update.CallbackQuery.Message.MessageId]); + } + catch (ApiRequestException) + { + await botClient.AnswerCallbackQueryAsync(update.CallbackQuery.Id, lang["Failed"]); + break; + } + await botClient.AnswerCallbackQueryAsync(update.CallbackQuery.Id, lang["Pass"]); + break; + } + case UpdateType.Message: + { + switch (update.Message.Type) + { + case MessageType.Text: + { + Internationalization lang = (config.EnableAutoI18n && !string.IsNullOrEmpty(update.Message.From.LanguageCode)) ? i18nHelper.TryGetLanguageData(update.Message.From.LanguageCode, out Internationalization value) ? value : i18nHelper[CultureInfo.CurrentCulture.Name] : i18nHelper[CultureInfo.CurrentCulture.Name]; + if (update.Message.Text != $"/set@{(await botClient.GetMeAsync()).Username}") { - Internationalization lang = (config.EnableAutoI18n && !string.IsNullOrEmpty(update.Message.From.LanguageCode)) ? i18nHelper.TryGetLanguageData(update.Message.From.LanguageCode, out Internationalization value) ? value : i18nHelper[CultureInfo.CurrentCulture.Name] : i18nHelper[CultureInfo.CurrentCulture.Name]; - if (update.Message.Text != $"/set@{(await botClient.GetMeAsync()).Username}") - { - break; - } - if (!(update.Message.Chat.IsForum ?? false) || !(await botClient.GetChatAdministratorsAsync(update.Message.Chat.Id)).Any((chatMember) => chatMember.User.Id == update.Message.From.Id)) + break; + } + if (!(update.Message.Chat.IsForum ?? false) || !(await botClient.GetChatAdministratorsAsync(update.Message.Chat.Id)).Any((chatMember) => chatMember.User.Id == update.Message.From.Id)) + { + await botClient.SendTextMessageAsync(update.Message.Chat.Id, lang["UpdateFailed"], (update.Message.Chat.IsForum ?? false) ? update.Message.MessageThreadId : default, replyToMessageId: update.Message.MessageId); + break; + } + update.Message.Chat.JoinByRequest = true; + ILiteCollection col = dataBase.GetCollection("chats"); + col.Upsert(new ChatData(update.Message.Chat.Id, update.Message.MessageThreadId ?? default)); + await botClient.SendTextMessageAsync(update.Message.Chat.Id, lang["UpdateSuccess"], update.Message.MessageThreadId, replyToMessageId: update.Message.MessageId); + } + break; + case MessageType.ChatMembersAdded: + { + foreach (User member in update.Message.NewChatMembers) + { + if (!groupData.TryGetValue(update.Message.Chat.Id, out Dictionary data)) { - await botClient.SendTextMessageAsync(update.Message.Chat.Id, lang["UpdateFailed"], (update.Message.Chat.IsForum ?? false) ? update.Message.MessageThreadId : default, replyToMessageId: update.Message.MessageId); break; } - update.Message.Chat.JoinByRequest = true; - ILiteCollection col = dataBase.GetCollection("chats"); - col.Upsert(new ChatData(update.Message.Chat.Id, update.Message.MessageThreadId ?? default)); - await botClient.SendTextMessageAsync(update.Message.Chat.Id, lang["UpdateSuccess"], update.Message.MessageThreadId, replyToMessageId: update.Message.MessageId); - } - break; - case MessageType.ChatMembersAdded: - { - foreach (User member in update.Message.NewChatMembers) + foreach ((int messageId, long userId) in data) { - if (!groupData.TryGetValue(update.Message.Chat.Id, out Dictionary data)) + if (userId != member.Id) { - break; - } - foreach ((int messageId, long userId) in data) - { - if (userId != member.Id) - { - continue; - } - await botClient.DeleteMessageAsync(update.Message.Chat.Id, messageId); - data.Remove(messageId); + continue; } + await botClient.DeleteMessageAsync(update.Message.Chat.Id, messageId); + data.Remove(messageId); } - break; } - } + break; + } + } + break; + } + case UpdateType.ChatJoinRequest: + { + Internationalization lang = (config.EnableAutoI18n && !string.IsNullOrEmpty(update.ChatJoinRequest.From.LanguageCode)) ? i18nHelper.TryGetLanguageData(update.ChatJoinRequest.From.LanguageCode, out Internationalization value) ? value : i18nHelper[CultureInfo.CurrentCulture.Name] : i18nHelper[CultureInfo.CurrentCulture.Name]; + if (update.ChatJoinRequest.From.IsBot) + { break; } - case UpdateType.ChatJoinRequest: + if (!groupData.ContainsKey(update.ChatJoinRequest.Chat.Id)) { - Internationalization lang = (config.EnableAutoI18n && !string.IsNullOrEmpty(update.ChatJoinRequest.From.LanguageCode)) ? i18nHelper.TryGetLanguageData(update.ChatJoinRequest.From.LanguageCode, out Internationalization value) ? value : i18nHelper[CultureInfo.CurrentCulture.Name] : i18nHelper[CultureInfo.CurrentCulture.Name]; - if (update.ChatJoinRequest.From.IsBot) + groupData[update.ChatJoinRequest.Chat.Id] = new(); + } + int min = 3; // TODO:群组管理员自定义时长 + Message msg = await botClient.SendTextMessageAsync( + update.ChatJoinRequest.Chat.Id, + lang.Translate("Message", $"[{string.IsNullOrWhiteSpace(update.ChatJoinRequest.From.Username) ? update.ChatJoinRequest.From.FirstName + update.ChatJoinRequest.From.LastName : update.ChatJoinRequest.From.Username}](tg://user?id={update.ChatJoinRequest.From.Id})", min), + messageThreadId: (update.ChatJoinRequest.Chat.IsForum ?? false) ? dataBase.GetCollection("chats").FindById(update.ChatJoinRequest.Chat.Id).MessageThreadId : default, + replyMarkup: new InlineKeyboardMarkup(new[] { - break; - } - if (!groupData.ContainsKey(update.ChatJoinRequest.Chat.Id)) + InlineKeyboardButton.WithCallbackData(lang["VerifyButton"]), + }) + ); + groupData[update.ChatJoinRequest.Chat.Id][msg.MessageId] = update.ChatJoinRequest.UserChatId; + Timer timer = new(min * 60000) + { + AutoReset = false, + }; + timer.Elapsed += async (_, _) => + { + if (!groupData.TryGetValue(update.ChatJoinRequest.Chat.Id, out Dictionary members) || !members.ContainsKey(msg.MessageId)) { - groupData[update.ChatJoinRequest.Chat.Id] = new(); + return; } - int min = 3; // TODO:群组管理员自定义时长 - Message msg = await botClient.SendTextMessageAsync( - update.ChatJoinRequest.Chat.Id, - lang.Translate("Message", string.IsNullOrWhiteSpace(update.ChatJoinRequest.From.Username) ? update.ChatJoinRequest.From.FirstName : update.ChatJoinRequest.From.Username, min), - messageThreadId: (update.ChatJoinRequest.Chat.IsForum ?? false) ? dataBase.GetCollection("chats").FindById(update.ChatJoinRequest.Chat.Id).MessageThreadId : default, - replyMarkup: new InlineKeyboardMarkup(new[] - { - InlineKeyboardButton.WithCallbackData(lang["VerifyButton"]), - })); - groupData[update.ChatJoinRequest.Chat.Id][msg.MessageId] = update.ChatJoinRequest.UserChatId; - Timer timer = new(min * 60000) - { - AutoReset = false, - }; - timer.Elapsed += async (_, _) => - { - if (!groupData.TryGetValue(update.ChatJoinRequest.Chat.Id, out Dictionary members) || !members.ContainsKey(msg.MessageId)) - { - return; - } - members.Remove(msg.MessageId); - await botClient.DeleteMessageAsync(update.ChatJoinRequest.Chat.Id, msg.MessageId); - }; - timer.Start(); - break; - } - } - } - catch (Exception e) - { - Console.WriteLine(e); + members.Remove(msg.MessageId); + await botClient.DeleteMessageAsync(update.ChatJoinRequest.Chat.Id, msg.MessageId); + }; + timer.Start(); + break; + } } }, (_, e, _) => { ExceptionDispatchInfo.Capture(e).Throw(); }); while (true)