Ejemplo n.º 1
0
        public async Task CheckDueScheduledMessages()
        {
            using var scope = _serviceProvider.CreateScope();
            var repo     = ScheduledMessageRepository.CreateWithBotIdentity(scope.ServiceProvider);
            var messages = await repo.GetDueMessages();

            foreach (ScheduledMessage message in messages)
            {
                _logger.LogInformation($"Handling scheduled message {message.Id} for {message.GuildId}/{message.ChannelId} by {message.CreatorId}/{message.LastEditedById}.");
                IGuild       guild;
                ITextChannel channel;
                try
                {
                    guild = _discordAPI.FetchGuildInfo(message.GuildId, CacheBehavior.OnlyCache);
                    if (guild == null)
                    {
                        throw new Exception();
                    }
                } catch (Exception)
                {
                    _logger.LogInformation($"Failed scheduled message {message.Id}. Reason unknown.");
                    await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.Unknown);

                    continue;
                }

                try
                {
                    channel = await guild.GetTextChannelAsync(message.ChannelId);

                    if (channel == null)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    _logger.LogInformation($"Failed scheduled message {message.Id}. Reason channel not found.");
                    await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.ChannelNotFound);

                    continue;
                }

                try
                {
                    await channel.SendMessageAsync(message.Content);

                    await repo.SetMessageAsSent(message.Id);

                    _logger.LogInformation($"Sent scheduled message {message.Id} for {message.GuildId}/{message.ChannelId} by {message.CreatorId}/{message.LastEditedById}.");
                }
                catch (HttpException e)
                {
                    if (e.HttpCode == HttpStatusCode.Unauthorized || e.HttpCode == HttpStatusCode.Forbidden)
                    {
                        _logger.LogInformation($"Failed scheduled message {message.Id}. Reason insufficient permission.");
                        await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.InsufficientPermission);
                    }
                    else
                    {
                        _logger.LogInformation($"Failed scheduled message {message.Id}. Reason unknown");
                        await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.Unknown);
                    }
                }
                catch (Exception)
                {
                    _logger.LogInformation($"Failed scheduled message {message.Id}. Reason unknown");
                    await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.Unknown);
                }
            }
        }