|
| 1 | +using System.Text; |
| 2 | +using DSharpPlus; |
| 3 | +using DSharpPlus.Entities; |
| 4 | +using DSharpPlus.SlashCommands; |
| 5 | +using DSharpPlus.SlashCommands.Attributes; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Stopwatch.Data; |
| 8 | +using Stopwatch.Services; |
| 9 | + |
| 10 | +namespace Stopwatch.Commands; |
| 11 | + |
| 12 | +internal sealed class BucketsCommand : ApplicationCommandModule |
| 13 | +{ |
| 14 | + private readonly LimiterService _limiterService; |
| 15 | + private readonly MessageCountingService _countingService; |
| 16 | + |
| 17 | + public BucketsCommand(LimiterService limiterService, MessageCountingService countingService) |
| 18 | + { |
| 19 | + _limiterService = limiterService; |
| 20 | + _countingService = countingService; |
| 21 | + } |
| 22 | + |
| 23 | + [SlashCommand("buckets", "View the current bucket counts for a channel.", false)] |
| 24 | + [SlashRequireGuild] |
| 25 | + public async Task BucketsAsync(InteractionContext context, |
| 26 | + [Option("channel", "The channel whose buckets to retrieve.")] |
| 27 | + DiscordChannel? channel = null) |
| 28 | + { |
| 29 | + channel ??= context.Channel; |
| 30 | + |
| 31 | + if (!_limiterService.TryGetRate(channel, out Rate limit)) |
| 32 | + { |
| 33 | + await context.CreateResponseAsync("Channel is not rate limited").ConfigureAwait(false); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + var lines = new StringBuilder(); |
| 38 | + lines.AppendLine($"Limit: {limit} ({limit.CountPerSecond * 10} per 10s)"); |
| 39 | + lines.AppendLine("Buckets: 0s 10s 20s 30s 40s 50s"); |
| 40 | + |
| 41 | + Rate[][] buckets = _countingService.GetBuckets(channel).Chunk(6).ToArray(); |
| 42 | + |
| 43 | + for (var time = 0; time < buckets.Length; time++) |
| 44 | + { |
| 45 | + string formatted = string.Join(' ', buckets[time].Select(r => r.Count.ToString("D3"))); |
| 46 | + lines.AppendLine($"{time:D7}m {formatted}"); |
| 47 | + } |
| 48 | + |
| 49 | + await context.CreateResponseAsync(Formatter.BlockCode(lines.ToString())).ConfigureAwait(false); |
| 50 | + } |
| 51 | +} |
0 commit comments