Message.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 CardCollector.Resources;
  9. using Telegram.Bot.Types;
  10. namespace CardCollector.Commands.Message
  11. {
  12. using static Logs;
  13. public abstract class Message : UpdateModel
  14. {
  15. private static readonly List<Message> List = new()
  16. {
  17. new ProfileMessage()
  18. };
  19. public static async Task<UpdateModel> Factory(Update update)
  20. {
  21. try
  22. {
  23. // Текст команды
  24. var command = update.Message!.Text;
  25. // Id пользователя, отправившего команду
  26. var userId = update.Message!.From!.Id;
  27. // Объект пользователя
  28. var user = await UserDao.GetById(userId) ?? await UserDao.AddNew(update.Message.From);
  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 && executor.IsMatches(command))
  34. 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. }
  46. }