UpdateModel.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Telegram.Bot.Types;
  7. namespace MafiaTelegramBot.Models
  8. {
  9. public abstract class UpdateModel<T> : ICloneable
  10. {
  11. protected abstract T Name { get; }
  12. protected long ChatId { get; set; }
  13. protected long UserId { get; set; }
  14. protected abstract Task<Message> Execute(Update update);
  15. protected abstract bool IsMatches(String command);
  16. protected static UpdateModel<T>? FirstOrDefault(IReadOnlyList<UpdateModel<T>> list, string data)
  17. {
  18. return (from item in list
  19. where item.IsMatches(data)
  20. select item).FirstOrDefault();
  21. }
  22. protected static async Task DeletePreviousMessage(long chatId, int messageId)
  23. {
  24. await Bot.Get().DeleteMessageAsync(chatId, messageId);
  25. }
  26. public object? Clone(long chatId, long userId)
  27. {
  28. var clone = (UpdateModel<T>?) MemberwiseClone();
  29. if(clone == null) return clone;
  30. clone.ChatId = chatId;
  31. clone.UserId = userId;
  32. return clone;
  33. }
  34. public object Clone()
  35. {
  36. return MemberwiseClone();
  37. }
  38. }
  39. }