Bot.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Timers;
  6. using CardCollector.DailyTasks;
  7. using CardCollector.DataBase;
  8. using CardCollector.DataBase.EntityDao;
  9. using CardCollector.Resources;
  10. using CardCollector.StickerEffects;
  11. using Telegram.Bot;
  12. using Telegram.Bot.Types;
  13. using CancellationTokenSource = System.Threading.CancellationTokenSource;
  14. using Timer = System.Timers.Timer;
  15. namespace CardCollector
  16. {
  17. using static Controllers.MessageController;
  18. public static class Bot
  19. {
  20. private static TelegramBotClient _client;
  21. public static TelegramBotClient Client => _client ??= new TelegramBotClient(AppSettings.TOKEN);
  22. private static readonly ManualResetEvent _end = new(false);
  23. private static readonly Timer _timer = new () {
  24. AutoReset = true,
  25. Enabled = true,
  26. Interval = Constants.SAVING_CHANGES_INTERVAL
  27. };
  28. private static readonly IEnumerable<BotCommand> _commands = new[]
  29. {
  30. new BotCommand {Command = "/menu", Description = "Показать меню"},/*
  31. new BotCommand {Command = "/help", Description = "Показать информацию"},
  32. new BotCommand {Command = "/error", Description = "Сообщить об ошибке"},*/
  33. };
  34. public static void Main(string[] args)
  35. {
  36. var cts = new CancellationTokenSource();
  37. Client.StartReceiving(HandleUpdateAsync, HandleErrorAsync, cancellationToken: cts.Token);
  38. Client.SetMyCommandsAsync(_commands, BotCommandScope.AllPrivateChats(), cancellationToken: cts.Token);
  39. _timer.Elapsed += SavingChanges;
  40. _timer.Elapsed += UserDao.ClearMemory;
  41. /* Запускаем механизм уведомления */
  42. Utilities.SetUpTimer(Constants.DailyTaskAlert, DailyTaskAlert);
  43. /* Запускаем сброс ежедневных заданий */
  44. Utilities.SetUpTimer(Constants.DailyTaskReset, DailyTask.ResetTasks);
  45. /* Запускаем таймер с эффектами стикеров */
  46. Utilities.SetUpTimer(Constants.DailyStickerRewardCheck, EffectFunctions.RunAll);
  47. _end.WaitOne();
  48. Logs.LogOut("Stopping program");
  49. cts.Cancel();
  50. }
  51. public static void StopProgram()
  52. {
  53. _timer.Elapsed += OnTimerOnElapsed;
  54. static async void OnTimerOnElapsed(object o, ElapsedEventArgs elapsedEventArgs)
  55. {
  56. _timer.Stop();
  57. await UserDao.ClearMemory();
  58. _end.Set();
  59. }
  60. }
  61. private static async void SavingChanges(object o, ElapsedEventArgs e)
  62. {
  63. try {
  64. await CardCollectorDatabase.SaveAllChangesAsync();
  65. } catch (Exception) { /*ignored*/ }
  66. }
  67. private static async void DailyTaskAlert(object o, ElapsedEventArgs e)
  68. {
  69. var users = await UserDao.GetAllWhere(user => Task.FromResult(!user.IsBlocked));
  70. foreach (var user in users)
  71. await SendMessage(user, Messages.daily_task_alertation, Keyboard.Menu);
  72. Utilities.SetUpTimer(Constants.DailyTaskAlert, DailyTaskAlert);
  73. }
  74. }
  75. }