Message.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Threading.Tasks;
  3. using CardCollector.Controllers;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using CardCollector.DataBase.Entity;
  7. using CardCollector.DataBase.EntityDao;
  8. using Telegram.Bot.Types;
  9. namespace CardCollector.Commands.Message
  10. {
  11. using static Logs;
  12. public abstract class Message : UpdateModel
  13. {
  14. private static readonly List<Message> List = new()
  15. {
  16. new ProfileMessage(),
  17. new StartMessage()
  18. };
  19. public static async Task<UpdateModel> Factory(Update update)
  20. {
  21. try
  22. {
  23. // Текст команды
  24. var command = update.Message!.Text;
  25. // Объект пользователя
  26. var user = await UserDao.GetUser(update.Message.From);
  27. // Добавляем сообщения пользователя в пул для удаления
  28. MessageController.AddNewMessageToPool(user, update.Message.MessageId);
  29. // Возвращаем объект, если команда совпала
  30. foreach (var item in List.Where(item => item.IsMatches(command)))
  31. if(Activator.CreateInstance(item.GetType(), user, update) is Message executor)
  32. if (executor.IsMatches(command)) return executor;
  33. // Возвращаем команда не найдена, если код дошел до сюда
  34. return new CommandNotFound(user, update, command);
  35. }
  36. catch (Exception e)
  37. {
  38. LogOutError(e);
  39. throw;
  40. }
  41. }
  42. protected Message(UserEntity user, Update update) : base(user, update) { }
  43. protected Message() { }
  44. }
  45. }