示例#1
0
        private async void SendSchedule(
            ISocketMessageChannel channel,
            Domain.Schedule.Models.Schedule?schedule
            )
        {
            if (schedule == null)
            {
                await channel.SendMessageAsync("Расписание не найдено");

                return;
            }

            var date      = DateTime.Today;
            var dayOfWeek = (int)date.DayOfWeek;

            if (dayOfWeek == (int)DayOfWeek.Sunday)
            {
                dayOfWeek = 6;
            }
            else
            {
                dayOfWeek--;
            }

            date = date.AddDays(-dayOfWeek);
            for (var i = 0; i < schedule.DailySchedules.Count; i++, date = date.AddDays(1))
            {
                var day = (i + 1) % schedule.DailySchedules.Count;
                await SendDate(channel, date);

                var dailySchedule = schedule.DailySchedules[day];
                if (dailySchedule.Count == 0)
                {
                    await SendEmptyDay(channel);
                }
                else
                {
                    foreach (var lesson in dailySchedule)
                    {
                        await ScheduleMessageUtils.SendLesson(channel, lesson, date);

                        await Task.Delay(600);
                    }
                }
            }
        }
示例#2
0
        private async void SendTodaySchedule(
            ISocketMessageChannel channel,
            Domain.Schedule.Models.Schedule?schedule
            )
        {
            if (schedule == null)
            {
                await channel.SendMessageAsync("Расписание не найдено");

                return;
            }

            var date = DateTime.Today;

            await SendDate(channel, date);

            foreach (var lesson in schedule.GetSchedule(date))
            {
                await ScheduleMessageUtils.SendLesson(channel, lesson, DateTime.Now);

                await Task.Delay(650);
            }
        }
        public async void Launch(IMessageChannel channel, SocketGuild guild)
        {
            if (cts != null && !cts.IsCancellationRequested)
            {
                return;
            }

            await channel.SendMessageAsync("Служба оповещения включена");

            cts = new CancellationTokenSource();
            var regex = new Regex("\\d\\d.-...");

            groups = guild.Roles
                     .Where(it => regex.IsMatch(it.Name));
            var sch = (await repository.GetAllSchedules())?.Filter(groups: this.groups.Select(it => it.Name));

            lock (key)
            {
                this.schedule = sch;
            }
            if (schedule == null)
            {
                return;
            }
            ScheduleUpdater();

            var msgFromId = 0UL;
            var msgToId   = 0UL;
            var limit     = 0;

            while (true)
            {
                var currentTime = GetMoscowDateTime(DateTime.Now);
                Console.WriteLine("Сервис оповещения о расписании: " + currentTime.ToString());
                var shiftedTime         = currentTime.AddMinutes(10); // Get lesson 10 min before start
                var currentOrder        = Lesson.GetOrder(shiftedTime, false);
                var currentOrderEvening = Lesson.GetOrder(shiftedTime, true);

                var realIsStarted = Lesson.GetTime(currentOrder.Order, currentOrder.Evening).Item1 >=
                                    currentTime.TimeOfDay;
                var realIsStartedEvening = Lesson.GetTime(
                    currentOrderEvening.Order, currentOrderEvening.Evening
                    ).Item1 >= currentTime.TimeOfDay;



                var filteredLessons = schedule.GetSchedule(shiftedTime);
                filteredLessons = filteredLessons.Where(it =>
                                                        !it.GroupIsEvening && currentOrder.Started && realIsStarted && it.Order == currentOrder.Order ||
                                                        it.GroupIsEvening && currentOrderEvening.Started && realIsStartedEvening && it.Order == currentOrderEvening.Order
                                                        ).ToList();

                if (msgFromId != 0UL && msgToId != 0UL)
                {
                    var flag        = false;
                    var collections = channel.GetMessagesAsync(
                        msgToId, Direction.Before, limit: limit
                        );
                    await foreach (var col in collections)
                    {
                        foreach (var message in col)
                        {
                            if (message.Author.IsBot)
                            {
                                await message.DeleteAsync();

                                await Task.Delay(700);
                            }
                            if (message.Id == msgFromId)
                            {
                                flag = true;
                                break;
                            }
                        }

                        if (flag)
                        {
                            break;
                        }
                    }
                    await(await channel.GetMessageAsync(msgToId)).DeleteAsync();
                }
                else if (msgFromId != 0UL)
                {
                    await(await channel.GetMessageAsync(msgFromId)).DeleteAsync();
                }
                limit = 0;
                if (filteredLessons.Count != 0)
                {
                    msgFromId = (await channel.SendMessageAsync("Занятия через 10 минут")).Id;
                    limit     = filteredLessons.Count * 2 + 1;
                    foreach (var lesson in filteredLessons)
                    {
                        var mentions = string.Join(", ",
                                                   lesson.Groups
                                                   .Select(it =>
                        {
                            var q = groups.FirstOrDefault(role =>
                                                          role.Name.Equals(it.Title)
                                                          );
                            return(q?.Mention ?? string.Empty);
                        }
                                                           )
                                                   );
                        await channel.SendMessageAsync(mentions);

                        msgToId = (await ScheduleMessageUtils.SendLesson(channel, lesson, DateTime.Now)).Id;
                        await Task.Delay(700);
                    }
                }
                else
                {
                    msgFromId = (await channel.SendMessageAsync("Через 10 минут занятий нет")).Id;
                    msgToId   = 0UL;
                    Console.WriteLine($"Сейчас {currentOrder.Order + 1}-й занятие или " +
                                      $"{currentOrderEvening.Order + 1}-й занятие");
                }


                var timeToSleep = GetTimeToSleep(currentOrder, currentOrderEvening);

                Console.WriteLine("Следующее оповещение через " + timeToSleep.ToString());

                try
                {
                    await Task.Delay(timeToSleep, cts.Token);
                }
                catch (TaskCanceledException e)
                {
                    Console.WriteLine("Сервис оповещения о расписании остановлен: " + DateTime.Now.ToString());
                    await channel.SendMessageAsync("Служба оповещения остановлена");

                    return;
                }
            }
        }