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 Jul 26, 2023
1 parent f479445 commit b02016c
Showing 1 changed file with 87 additions and 93 deletions.
180 changes: 87 additions & 93 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, long> 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<int, long> 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<ChatData> col = dataBase.GetCollection<ChatData>("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<int, long> 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<ChatData> col = dataBase.GetCollection<ChatData>("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<int, long> 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),

Check failure on line 109 in src/Program.cs

View workflow job for this annotation

GitHub Actions / build

A conditional expression cannot be used directly in a string interpolation because the ':' ends the interpolation. Parenthesize the conditional expression.
messageThreadId: (update.ChatJoinRequest.Chat.IsForum ?? false) ? dataBase.GetCollection<ChatData>("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<int, long> 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<ChatData>("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<int, long> 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)
Expand Down

0 comments on commit b02016c

Please sign in to comment.