Command.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using MafiaTelegramBot.Models.Commands.CustomMessageHandlers;
  4. using Telegram.Bot.Types;
  5. using Telegram.Bot.Types.Enums;
  6. namespace MafiaTelegramBot.Models.Commands
  7. {
  8. public abstract class Command : UpdateModel
  9. {
  10. private static Dictionary<string, Command> _customHandlers = new()
  11. {
  12. {"UsersThatChangesNickname", new ChangeNicknameCommand()},
  13. {"UsersThatCreatesRoom", new CreateRoomCommand()},
  14. {"UserThatEntersPrivateCode", new EnterPrivateRoomCommand()},
  15. };
  16. public static async Task<Message> Update(Update update)
  17. {
  18. var chatId = update.Message.Chat.Id;
  19. await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
  20. var commands = Bot.Commands;
  21. var message = update.Message.Text;
  22. var command = FirstOrDefault(commands, message);
  23. if (command != null) return await command.Execute(update);
  24. var userId = update.Message.From.Id;
  25. if (Bot.UsersThatChangesNickname.Remove(userId))
  26. return await _customHandlers["UsersThatChangesNickname"].Execute(update);
  27. if (Bot.UsersThatCreatesRoom.ContainsKey(userId))
  28. return await _customHandlers["UsersThatCreatesRoom"].Execute(update);
  29. if (Bot.UserThatEntersPrivateCode.Remove(userId))
  30. return await _customHandlers["UserThatEntersPrivateCode"].Execute(update);
  31. return await Bot.SendWithMarkdown2(chatId, $"{strings.command_not_found} _*\\({message}\\)*_");
  32. }
  33. }
  34. }