Message.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 user = await UserDao.GetUser(update.Message!.From);
  25. //Если сообщение не содержит текст
  26. if (update.Message!.Text == null) return new IgnoreUpdate(user, update);
  27. // Текст команды
  28. var command = update.Message!.Text;
  29. // Добавляем сообщения пользователя в пул для удаления
  30. MessageController.AddNewMessageToPool(user, update.Message.MessageId);
  31. // Возвращаем объект, если команда совпала
  32. foreach (var item in List.Where(item => item.IsMatches(command)))
  33. if(Activator.CreateInstance(item.GetType(), user, update) is Message executor)
  34. if (executor.IsMatches(command)) return executor;
  35. // Возвращаем команда не найдена, если код дошел до сюда
  36. return new CommandNotFound(user, update, command);
  37. }
  38. catch (Exception e)
  39. {
  40. LogOutError(e);
  41. throw;
  42. }
  43. }
  44. protected Message(UserEntity user, Update update) : base(user, update) { }
  45. protected Message() { }
  46. }
  47. }