Esempio n. 1
0
        async Task RunRollAsync(DiscordClient client, IMessageChannel channel, CancellationToken cancellationToken = default)
        {
            var logPlace = $"channel '{channel.Name}' ({channel.Id})";

            var batches = 0;
            var rolls   = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                var options = _options.CurrentValue;

                if (!options.Enabled)
                {
                    await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);

                    continue;
                }

                IUserMessage response;

                try
                {
                    using (channel.Typing())
                    {
                        await Task.Delay(TimeSpan.FromSeconds(options.TypingDelaySeconds), cancellationToken);

                        response = await _commandHandler.SendAsync(channel, options.Command, cancellationToken);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogWarning(e, $"Could not roll '{options.Command}' in {logPlace}.");

                    await Task.Delay(TimeSpan.FromMinutes(30), cancellationToken);

                    continue;
                }

                ++rolls;

                if (response.Embeds.Count != 0)
                {
                    _logger.LogInformation($"Sent roll {rolls} of batch {batches} in {logPlace}.");

                    await Task.Delay(TimeSpan.FromSeconds(options.IntervalSeconds), cancellationToken);

                    continue;
                }

                if (response.Content.StartsWith($"**{client.CurrentUser.Name}**", StringComparison.OrdinalIgnoreCase))
                {
                    if (_outputParser.TryParseRollRemaining(response.Content, out var remaining))
                    {
                        _logger.LogInformation($"Sent roll {rolls} of batch {batches} in {logPlace}. {remaining} rolls are remaining.");

                        await Task.Delay(TimeSpan.FromSeconds(options.IntervalSeconds), cancellationToken);

                        continue;
                    }

                    if (_outputParser.TryParseRollLimited(response.Content, out var resetTime))
                    {
                        resetTime += TimeSpan.FromMinutes(1);

                        _logger.LogInformation($"Finished roll {rolls} of batch {batches} in {logPlace}. Next batch in {resetTime}.");
                        rolls = 0;
                        ++batches;

                        await Task.Delay(resetTime, cancellationToken);

                        continue;
                    }
                }

                _logger.LogWarning($"Could not handle Mudae response for command '{options.Command}'. Assuming a sane default of 5 rolls per hour ({rolls} right now). Response: {response.Content}");

                if (rolls >= 5)
                {
                    _logger.LogInformation($"Preemptively finished roll {rolls} of batch {batches} in {logPlace}. Next batch in an hour.");
                    rolls = 0;
                    ++batches;

                    await Task.Delay(TimeSpan.FromHours(1), cancellationToken);

                    continue;
                }
            }
        }