private static void BanUserForPositiveFilter(Message message, Filters.FilterResult result) { int limitTime = 3; Models.Group.ConfigurationParameter configValue = CacheData.GroupConfigs[message.Chat.Id] .Where(x => x.ConfigurationParameterId == "SpamActionLimitTime") .SingleOrDefault(); if (configValue != null) { int.TryParse(configValue.Value, out limitTime); } RemoveMessageForPositiveFilter(message, result); Bot.Manager.BotClient.KickChatMemberAsync(message.Chat.Id, message.From.Id, DateTime.UtcNow.AddMinutes(-5)); UserTools.AddPenalty(message.Chat.Id, message.From.Id, Models.TrustFactorLog.TrustFactorAction.ban, Bot.Manager.MyId); Bot.Manager.BotClient.KickChatMemberAsync(message.Chat.Id, message.From.Id, DateTime.UtcNow.AddMinutes(-5)); UserTools.AddPenalty(message.Chat.Id, message.From.Id, Models.TrustFactorLog.TrustFactorAction.ban, Bot.Manager.MyId); string author = message.From.Username == null ? message.From.FirstName + " " + message.From.LastName : "@" + message.From.Username; string logMessage = String.Format( "*[Report]*\n" + "User banned as per group _Spam Action_ preference.\n" + "⚠ do not open links you don't know ⚠\n" + "\nControl: `{0}`" + "\nChat: `{1}`" + "\nAuthor: `{2}`" + "\nUserId: `{3}`" + "\n\n*hash_code:* #UB{4}-{5}", result.CheckName, message.Chat.Title, author, message.From.Id, message.Chat.Id.ToString().Replace("-", ""), Guid.NewGuid()); MessageQueueManager.EnqueueLog(new ChatMessage() { ParseMode = ParseMode.Markdown, Text = logMessage }); LogTools.AddActionLog(new ActionLog() { GroupId = CacheData.Groups[message.Chat.Id].GroupId, UtcDate = DateTime.UtcNow, ActionTypeId = "autoBan", Parameters = logMessage, }); }
public void Execute(Message message) { if (!BotTools.IsUserOperator(message.From.Id, Models.Operator.Levels.Basic) && !ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id)) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "ban_command_error_notadmin") }); return; } if (Manager.BotClient.GetChatAdministratorsAsync(message.Chat.Id).Result .Single(x => x.User.Id == message.From.Id) .CanRestrictMembers == false) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "ban_command_error_adminPrivilege") }); return; } int userToKick; if (message.ReplyToMessage == null) { if (message.Text.Split(" ")[1].StartsWith("@")) { if (!CacheData.Usernames.Keys.Contains(message.Text.Split(" ")[1].Remove(0, 1))) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "ban_command_error_invalidUsername") }); return; } userToKick = CacheData.Usernames[message.Text.Split(" ")[1].Remove(0, 1)]; } else { bool isValid = int.TryParse(message.Text.Split(" ")[1], out userToKick); if (!isValid) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "ban_command_error_invalidUserId") }); return; } } } else { userToKick = message.ReplyToMessage.From.Id; } if (userToKick == 777000) // Telegram's official updateServiceNotification { Manager.BotClient.SendTextMessageAsync( chatId: message.Chat.Id, parseMode: ParseMode.Markdown, text: String.Format( "*[Error]*\n" + "This is an official Telegram's user/id.") ); return; } if (BotTools.IsUserOperator(userToKick)) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "command_to_operator_not_allowed") }); return; } try { Manager.BotClient.KickChatMemberAsync(message.Chat.Id, userToKick, DateTime.UtcNow.AddMinutes(-5)); MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "ban_command_success") }); } catch { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "ban_command_error") }); return; } UserTools.AddPenalty(message.Chat.Id, userToKick, Models.TrustFactorLog.TrustFactorAction.ban, Manager.MyId); }
public void Execute(Message message) { Manager.BotClient.DeleteMessageAsync(message.Chat.Id, message.MessageId); if (!BotTools.IsUserOperator(message.From.Id) && !ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id)) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, ReplyToMessageId = message.MessageId, Text = CacheData.GetTranslation("en", "error_not_auth_command") }); return; } int userToKick; if (message.ReplyToMessage == null) { if (message.Text.Split(" ")[1].StartsWith("@")) { if (!CacheData.Usernames.Keys.Contains(message.Text.Split(" ")[1].Remove(0, 1))) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "kick_command_error_invalidUsername") }); return; } userToKick = CacheData.Usernames[message.Text.Split(" ")[1].Remove(0, 1)]; } else { bool isValid = int.TryParse(message.Text.Split(" ")[1], out userToKick); if (!isValid) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "kick_command_error_invalidUserId") }); return; } } } else { userToKick = message.ReplyToMessage.From.Id; } if (userToKick == 777000) // Telegram's official updateServiceNotification { Manager.BotClient.SendTextMessageAsync( chatId: message.Chat.Id, parseMode: ParseMode.Markdown, text: String.Format( "*[Error]*\n" + "This is an official Telegram's user/id.") ); return; } if (BotTools.IsUserOperator(userToKick)) { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, Text = CacheData.GetTranslation("en", "command_to_operator_not_allowed") }); return; } try { Manager.BotClient.KickChatMemberAsync(message.Chat.Id, userToKick); if (message.Chat.Type == ChatType.Supergroup) { Manager.BotClient.UnbanChatMemberAsync(message.Chat.Id, userToKick); } } catch { MessageQueueManager.EnqueueMessage( new Models.ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, ParseMode = ParseMode.Markdown, Text = CacheData.GetTranslation("en", "command_kick_error") }); return; } UserTools.AddPenalty(message.Chat.Id, userToKick, Models.TrustFactorLog.TrustFactorAction.kick, Manager.MyId); }
public ControlResult DoCheck(Message message) { if (ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id)) { return(new ControlResult() { CheckName = "Anti Flood", Result = IControl.ControlResultType.skipped }); } if (message.Date < DateTime.UtcNow.AddMinutes(-1)) { return new ControlResult() { CheckName = "Anti Flood", Result = IControl.ControlResultType.skipped } } ; Models.Group.ConfigurationParameter configValue = CacheData.GroupConfigs[message.Chat.Id] .Where(x => x.ConfigurationParameterId == "FloodControl") .SingleOrDefault(); if (configValue != null) { if (configValue.Value == "false") { return new ControlResult() { CheckName = "Anti Flood", Result = IControl.ControlResultType.skipped } } } ; Dictionary <int, Flood> floodCounter = FloodCounter.GetValueOrDefault(message.Chat.Id); if (floodCounter == null) { FloodCounter.TryAdd(message.Chat.Id, new Dictionary <int, Flood>()); } if (!FloodCounter[message.Chat.Id].TryGetValue(message.From.Id, out Flood currentValue)) { FloodCounter[message.Chat.Id].Add(message.From.Id, new Flood() { UserId = message.From.Id, Messages = 1, LastMessage = DateTime.UtcNow }); } else { if (currentValue.LastMessage < DateTime.UtcNow.AddSeconds(-3)) { FloodCounter[message.Chat.Id][message.From.Id] = new Flood() { UserId = message.From.Id, Messages = 1, LastMessage = DateTime.UtcNow }; return(new ControlResult() { CheckName = "Anti Flood", Result = IControl.ControlResultType.negative }); } currentValue.Messages += 1; currentValue.LastMessage = DateTime.UtcNow; FloodCounter[message.Chat.Id][message.From.Id] = currentValue; } if (FloodCounter[message.Chat.Id][message.From.Id].Messages >= 5) { int minutes = 5; SysConfig floodBanMinutes = CacheData.SysConfigs.Where(x => x.SysConfigId == "FloodBanInMinutes") .SingleOrDefault(); if (floodBanMinutes != null) { int.TryParse(floodBanMinutes.Value, out minutes); } if (limitedUsers.ContainsKey(message.From.Id)) { if (limitedUsers[message.From.Id] > DateTime.UtcNow.AddMinutes(-minutes)) { goto skipLimitAndPenality; } } limitedUsers.AddOrUpdate(message.From.Id, DateTime.UtcNow, (key, value) => value = DateTime.UtcNow); Bot.Manager.BotClient.RestrictChatMemberAsync( message.Chat.Id, message.From.Id, new ChatPermissions() { CanSendMessages = false, CanAddWebPagePreviews = false, CanChangeInfo = false, CanInviteUsers = false, CanPinMessages = false, CanSendMediaMessages = false, CanSendOtherMessages = false, CanSendPolls = false }, DateTime.UtcNow.AddMinutes(minutes) ).Wait(); string author = message.From.Username == null ? message.From.FirstName + " " + message.From.LastName : "@" + message.From.Username; MessageQueueManager.EnqueueLog(new ChatMessage() { ParseMode = ParseMode.Markdown, Text = String.Format( "*[Report]*\n" + "User muted for {0} minutes due to flood.\n" + "\nChat: `{1}`" + "\nAuthor: `{3}`" + "\nUserId: `{2}`" + "\n\n*hash_code:* #UB{4}-{5}", minutes, message.Chat.Title, message.From.Id, author, message.Chat.Id.ToString().Replace("-", ""), Guid.NewGuid()) }); UserTools.AddPenalty(message.Chat.Id, message.From.Id, TrustFactorLog.TrustFactorAction.limit, Bot.Manager.MyId); MessageQueueManager.EnqueueMessage( new ChatMessage() { Timestamp = DateTime.UtcNow, Chat = message.Chat, ParseMode = ParseMode.Markdown, Text = $"User {message.From.Username} has been limited for {minutes} minutes due to flood.\n" + "An admin can immediately remove this limitation by clicking the button below.", ReplyMarkup = new InlineKeyboardMarkup( InlineKeyboardButton.WithCallbackData( CacheData.GetTranslation("en", "button_removeFlood", true), $"/RemoveFlood " + message.From.Id ) ) }); skipLimitAndPenality: return(new ControlResult() { CheckName = "Anti Flood", Result = IControl.ControlResultType.positive }); } return(new ControlResult() { CheckName = "Anti Flood", Result = IControl.ControlResultType.negative }); } }