public static CoreBotUser AddOrUpdateUser(CoreBotUser user) { var usr = Users.Set().FirstOrDefault(u => u.Id == user.Id); //Store Users if (usr == null) { //If new User then add Users.Add(user); Serilog.Log.Information($"New User {user.Name} {user.UserName}"); //bot.SendTextMessageAsync(Config.AdminId, $"New User {user.Name} {user.UserName}"); } else { //Update User usr.ActiveAt = DateTime.UtcNow; } //Save to file ApplicationData.Users.Save(); return(usr); }
private bool CheckMessage(MessageEventArgs messageEventArgs = null, CallbackQueryEventArgs callbackQueryEventArgs = null) { if (messageEventArgs == null && callbackQueryEventArgs == null) { return(false); } var chatId = messageEventArgs != null ? messageEventArgs.Message.Chat.Id : callbackQueryEventArgs.CallbackQuery.Message.Chat.Id; var messageId = messageEventArgs != null ? messageEventArgs.Message.MessageId : callbackQueryEventArgs.CallbackQuery.Message.MessageId; var sender = messageEventArgs != null ? messageEventArgs.Message.From : callbackQueryEventArgs.CallbackQuery.From; var user = ApplicationData.GetUser(sender.Id); // Store Users if (user == null) { user = new CoreBotUser { Id = sender.Id, Name = sender.FirstName + " " + sender.LastName, UserName = sender.Username, ActiveAt = DateTime.UtcNow, UserAccess = UserAccess.User }; } // First user is admin if (!ApplicationData.Users.Set().Any()) { user.UserAccess = UserAccess.Admin; SendMessageAsync(chatId, "You are admin now"); } // If new User then add ApplicationData.AddOrUpdateUser(user); // Filters for messages if (messageEventArgs != null) { var message = messageEventArgs.Message; if (message == null || (message.Type != MessageType.Text && message.Type != MessageType.Document)) { return(false); } // Ignore old messages if (message.Date.AddMinutes(1) < DateTime.UtcNow) { return(false); } } // Authorize User (if user banned - ignore) if (user.IsBanned()) { // SendMessage(chatId, "Banned"); DeleteMessageAsync(chatId, messageId); return(false); } // Store Stats { ApplicationData.UpdateStats(sender); } return(true); }