Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
修复
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Jan 24, 2024
1 parent 9ff491f commit c334a18
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 192 deletions.
7 changes: 0 additions & 7 deletions res/LanguagePack/en.json

This file was deleted.

7 changes: 0 additions & 7 deletions res/LanguagePack/zh-CN.json

This file was deleted.

27 changes: 7 additions & 20 deletions src/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static async Task OnCallback(this CallbackQuery callbackQuery)
return;
}

Internationalization lang = Program.I18n.GetI18n(callbackQuery.From.LanguageCode);
Localizer lang = Program.Localizer.GetLocalizer(callbackQuery.From.LanguageCode);
if (!Program.GroupData.TryGetValue(callbackQuery.Message.Chat.Id, out Dictionary<long, int>? data) ||
!data.TryGetValue(callbackQuery.From.Id, out int historyMessageId) ||
historyMessageId != callbackQuery.Message.MessageId)
Expand Down Expand Up @@ -51,22 +51,17 @@ public static async Task OnRequest(this ChatJoinRequest chatJoinRequest)
return;
}

Internationalization lang = Program.I18n.GetI18n(chatJoinRequest.From.LanguageCode);
Localizer lang = Program.Localizer.GetLocalizer(chatJoinRequest.From.LanguageCode);
const int min = 3; // TODO:群组管理员自定义时长
Message msg = await Program.BotClient.SendTextMessageAsync(
chatJoinRequest.Chat.Id,
Message msg = await Program.BotClient.SendTextMessageAsync(chatJoinRequest.Chat.Id,
lang.Translate("Message",
$"[{(string.IsNullOrWhiteSpace(chatJoinRequest.From.Username) ? $"{chatJoinRequest.From.FirstName} {chatJoinRequest.From.LastName}".Escape() : chatJoinRequest.From.Username)}](tg://user?id={chatJoinRequest.From.Id})",
min),
chatJoinRequest.Chat.IsForum ?? false
? Program.Database.GetCollection<ChatData>("chats").FindById(chatJoinRequest.Chat.Id).MessageThreadId
: default,
ParseMode.MarkdownV2,
: default, ParseMode.MarkdownV2,
replyMarkup: new InlineKeyboardMarkup(new[]
{
InlineKeyboardButton.WithCallbackData(lang["VerifyButton"])
})
);
{ InlineKeyboardButton.WithCallbackData(lang["VerifyButton"]) }));
Program.GroupData[chatJoinRequest.Chat.Id][chatJoinRequest.From.Id] = msg.MessageId;
Timer timer = new(min * 60000)
{
Expand Down Expand Up @@ -94,22 +89,14 @@ public static async Task OnRequest(this ChatJoinRequest chatJoinRequest)

public static async Task OnSet(this Message message)
{
if (message.From is null)
{
return;
}

Internationalization lang = Program.I18n.GetI18n(message.From.LanguageCode);
if ((!message.Chat.IsForum ?? true) ||
if (message.From is null || (!message.Chat.IsForum ?? true) ||
(await Program.BotClient.GetChatAdministratorsAsync(message.Chat.Id)).All(chatMember =>
chatMember.User.Id != message.From.Id))
{
await Program.BotClient.SendTextMessageAsync(message.Chat.Id, lang["UpdateFailed"],
message.Chat.IsForum ?? false ? message.MessageThreadId : default, ParseMode.MarkdownV2,
replyToMessageId: message.MessageId);
return;
}

Localizer lang = Program.Localizer.GetLocalizer(message.From.LanguageCode);
ILiteCollection<ChatData> col = Program.Database.GetCollection<ChatData>("chats");
col.Upsert(new ChatData(message.Chat.Id, message.MessageThreadId ?? default));
await Program.BotClient.SendTextMessageAsync(message.Chat.Id, lang["UpdateSuccess"], message.MessageThreadId,
Expand Down
179 changes: 63 additions & 116 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Net;
using LiteDB;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Net;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
Expand All @@ -10,28 +12,27 @@ namespace Yan;

internal static class Program
{
public static readonly ConfigHelper Config;
public static readonly I18nHelper I18n;
public static readonly Config Config;
public static readonly L10nProvider Localizer;
public static readonly LiteDatabase Database;
public static readonly Dictionary<long, Dictionary<long, int>> GroupData;
public static readonly TelegramBotClient BotClient;

static Program()
{
Config = new("Config.json");
I18n = new("LanguagePack");
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.Configuration.Sources.Clear();
IHostEnvironment env = builder.Environment;
builder.Configuration.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true);
Config = builder.Configuration.Get<Config>()!;
Localizer = new("Resources");
Database = new("Groups.db");
GroupData = new();
BotClient = new(Config.Token,
string.IsNullOrWhiteSpace(Config.ProxyUrl)
? default
: new(
new HttpClientHandler
{
Proxy = new WebProxy(Config.ProxyUrl, true)
}
)
);
: new(new HttpClientHandler { Proxy = new WebProxy(Config.ProxyUrl, true) }));
}

private static void Main()
Expand All @@ -43,134 +44,80 @@ private static void Main()
switch (update.Type)
{
case UpdateType.CallbackQuery:
{
if (update.CallbackQuery is null)
{
break;
}
if (update.CallbackQuery is null)
{
break;
}
await update.CallbackQuery.OnCallback();
break;
}
case UpdateType.Message:
{
if (update.Message is null)
{
await update.CallbackQuery.OnCallback();
break;
}
switch (update.Message.Type)
case UpdateType.Message:
{
case MessageType.Text:
if (update.Message is null)
{
if (update.Message.Text != $"/set@{(await BotClient.GetMeAsync()).Username}")
{
break;
}
await update.Message.OnSet();
}
break;
case MessageType.ChatMembersAdded:
}
switch (update.Message.Type)
{
if (update.Message.NewChatMembers is null ||
!GroupData.TryGetValue(update.Message.Chat.Id, out Dictionary<long, int>? data))
{
case MessageType.Text:
{
if (update.Message.Text != $"/set@{(await BotClient.GetMeAsync()).Username}")
{
break;
}
await update.Message.OnSet();
}
break;
}
case MessageType.ChatMembersAdded:
{
if (update.Message.NewChatMembers is null ||
!GroupData.TryGetValue(update.Message.Chat.Id,
out Dictionary<long, int>? data))
{
break;
}
foreach (User member in update.Message.NewChatMembers)
{
await member.OnJoin(update.Message.Chat.Id, data);
}
foreach (User member in update.Message.NewChatMembers)
{
await member.OnJoin(update.Message.Chat.Id, data);
}
break;
break;
}
default:
{
throw new ArgumentOutOfRangeException();
}
}
case MessageType.Unknown:
case MessageType.Photo:
case MessageType.Audio:
case MessageType.Video:
case MessageType.Voice:
case MessageType.Document:
case MessageType.Sticker:
case MessageType.Location:
case MessageType.Contact:
case MessageType.Venue:
case MessageType.Game:
case MessageType.VideoNote:
case MessageType.Invoice:
case MessageType.SuccessfulPayment:
case MessageType.WebsiteConnected:
case MessageType.ChatMemberLeft:
case MessageType.ChatTitleChanged:
case MessageType.ChatPhotoChanged:
case MessageType.MessagePinned:
case MessageType.ChatPhotoDeleted:
case MessageType.GroupCreated:
case MessageType.SupergroupCreated:
case MessageType.ChannelCreated:
case MessageType.MigratedToSupergroup:
case MessageType.MigratedFromGroup:
case MessageType.Poll:
case MessageType.Dice:
case MessageType.MessageAutoDeleteTimerChanged:
case MessageType.ProximityAlertTriggered:
case MessageType.WebAppData:
case MessageType.VideoChatScheduled:
case MessageType.VideoChatStarted:
case MessageType.VideoChatEnded:
case MessageType.VideoChatParticipantsInvited:
case MessageType.Animation:
case MessageType.ForumTopicCreated:
case MessageType.ForumTopicClosed:
case MessageType.ForumTopicReopened:
case MessageType.ForumTopicEdited:
case MessageType.GeneralForumTopicHidden:
case MessageType.GeneralForumTopicUnhidden:
case MessageType.WriteAccessAllowed:
case MessageType.UserShared:
case MessageType.ChatShared:
case MessageType.Story:
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
}
break;
}
case UpdateType.ChatJoinRequest:
{
if (update.ChatJoinRequest is null || update.ChatJoinRequest.From.IsBot)
{
if (update.ChatJoinRequest is null || update.ChatJoinRequest.From.IsBot)
{
break;
}
await update.ChatJoinRequest.OnRequest();
break;
}
await update.ChatJoinRequest.OnRequest();
break;
}
case UpdateType.Unknown:
case UpdateType.InlineQuery:
case UpdateType.ChosenInlineResult:
case UpdateType.EditedMessage:
case UpdateType.ChannelPost:
case UpdateType.EditedChannelPost:
case UpdateType.ShippingQuery:
case UpdateType.PreCheckoutQuery:
case UpdateType.Poll:
case UpdateType.PollAnswer:
case UpdateType.MyChatMember:
case UpdateType.ChatMember:
break;
default:
throw new ArgumentOutOfRangeException();
{
throw new ArgumentOutOfRangeException();
}
}
}
catch (Exception ex)
{
await File.AppendAllTextAsync("Exception.log", $"[{DateTime.Now}] {ex}\n");
Directory.CreateDirectory("logs");
await File.AppendAllTextAsync($"logs/{DateTime.Now:yy-MM-ddTHH-mm-ss}.log", ex.ToString());
}
}, (_, e, _) => { });

while (true)
{
Console.ReadLine();
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Message": "Hi, {0}! You have applied to join this group.\nPlease click the button below for man-machine verification. This verification will expire in {1} minutes.",
"VerifyButton": "Captcha",
"Pass": "Verify passed",
"Failed": "Verify failed",
"UpdateSuccess": "Update successful"
}
7 changes: 7 additions & 0 deletions src/Resources/zh-CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Message": "你好,{0}!您已申请加入本群组\n请点击下方按钮进行人机验证,本次验证将于{1}分钟后失效。",
"VerifyButton": "验证",
"Pass": "验证成功",
"Failed": "验证失败",
"UpdateSuccess": "更新成功"
}
3 changes: 3 additions & 0 deletions src/Utils/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Yan.Utils;

public record Config(string Token, string ProxyUrl, bool EnableAutoL10n);
20 changes: 0 additions & 20 deletions src/Utils/ConfigHelper.cs

This file was deleted.

Loading

0 comments on commit c334a18

Please sign in to comment.