diff --git a/source/POI.DiscordDotNet/Commands/SlashCommands/Modules/UtilSlashCommandsModule.cs b/source/POI.DiscordDotNet/Commands/SlashCommands/Modules/UtilSlashCommandsModule.cs index 265abc2c..25a34b30 100644 --- a/source/POI.DiscordDotNet/Commands/SlashCommands/Modules/UtilSlashCommandsModule.cs +++ b/source/POI.DiscordDotNet/Commands/SlashCommands/Modules/UtilSlashCommandsModule.cs @@ -1,4 +1,5 @@ using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; using POI.DiscordDotNet.Commands.SlashCommands.Utils; @@ -20,5 +21,12 @@ await ctx [SlashCommand("uppy", "Shows how long I've been online already 😅"), UsedImplicitly] public Task HandleUptimeCommand(InteractionContext ctx) => ctx.Services.GetRequiredService().Handle(ctx); + + [SlashCommand("silentmessage", "Send a message anonymously into this channel."), UsedImplicitly] + [SlashCooldown(1, 60 * 5, SlashCooldownBucketType.Channel)] // TODO: extract to db + migrations + public Task HandleSilentMessageCommand(InteractionContext ctx, + [Option("Type", "What type of message do you want to send into this channel?")] + AnonymousMessages messageType = AnonymousMessages.StayOnTopicReminder) + => ctx.Services.GetRequiredService().Handle(ctx, messageType); } } \ No newline at end of file diff --git a/source/POI.DiscordDotNet/Commands/SlashCommands/Utils/ServiceCollectionExtensions.cs b/source/POI.DiscordDotNet/Commands/SlashCommands/Utils/ServiceCollectionExtensions.cs index afa37f87..2aba96fe 100644 --- a/source/POI.DiscordDotNet/Commands/SlashCommands/Utils/ServiceCollectionExtensions.cs +++ b/source/POI.DiscordDotNet/Commands/SlashCommands/Utils/ServiceCollectionExtensions.cs @@ -5,5 +5,5 @@ namespace POI.DiscordDotNet.Commands.SlashCommands.Utils; public static class ServiceCollectionExtensions { public static IServiceCollection AddUtilitySlashCommands(this IServiceCollection services) => services - .AddTransient(); + .AddTransient().AddTransient(); } \ No newline at end of file diff --git a/source/POI.DiscordDotNet/Commands/SlashCommands/Utils/SilentMessageSlashCommands.cs b/source/POI.DiscordDotNet/Commands/SlashCommands/Utils/SilentMessageSlashCommands.cs new file mode 100644 index 00000000..3c858f53 --- /dev/null +++ b/source/POI.DiscordDotNet/Commands/SlashCommands/Utils/SilentMessageSlashCommands.cs @@ -0,0 +1,24 @@ +using DSharpPlus.SlashCommands; +using POI.DiscordDotNet.Commands.SlashCommands.Modules; + +namespace POI.DiscordDotNet.Commands.SlashCommands.Utils; + +public enum AnonymousMessages +{ + [ChoiceName("Stay on Topic reminder")] StayOnTopicReminder +} + +public class SilentMessageSlashCommands: UtilSlashCommandsModule +{ + private readonly Dictionary _anonymousMessages = new() + { + { AnonymousMessages.StayOnTopicReminder, "\u26a0\ufe0f Beep boop, please stay on topic! \ud83d\ude4f" } + }; + + public async Task Handle(InteractionContext ctx, + AnonymousMessages messageType = AnonymousMessages.StayOnTopicReminder) + { + await ctx.CreateResponseAsync("Message has been sent.", true).ConfigureAwait(false); + await ctx.Channel.SendMessageAsync(_anonymousMessages[messageType]).ConfigureAwait(false); + } +} \ No newline at end of file diff --git a/source/POI.DiscordDotNet/Services/Implementations/DiscordSlashCommandsService.cs b/source/POI.DiscordDotNet/Services/Implementations/DiscordSlashCommandsService.cs index d3e6d447..a0d5a756 100644 --- a/source/POI.DiscordDotNet/Services/Implementations/DiscordSlashCommandsService.cs +++ b/source/POI.DiscordDotNet/Services/Implementations/DiscordSlashCommandsService.cs @@ -1,4 +1,5 @@ using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; using DSharpPlus.SlashCommands.EventArgs; using Microsoft.Extensions.Logging; using POI.DiscordDotNet.Commands.SlashCommands.Modules; @@ -51,13 +52,29 @@ public void Cleanup(IDiscordClientProvider discordClientProvider) _slashCommands = null; } - private Task OnSlashCommandErrored(SlashCommandsExtension _, SlashCommandErrorEventArgs eventArgs) + private async Task OnSlashCommandErrored(SlashCommandsExtension _, SlashCommandErrorEventArgs eventArgs) { + if (eventArgs.Exception is SlashExecutionChecksFailedException castedException) + { + var timeLeft = string.Empty; + foreach (var error in castedException.FailedChecks) + { + var cooldown = (SlashCooldownAttribute) error; + var rawTime = cooldown.GetRemainingCooldown(eventArgs.Context); + + if (rawTime.Days != 0) timeLeft += $"{rawTime.Days} days, "; + if (rawTime.Hours != 0) timeLeft += $"{rawTime.Hours} hours, "; + if (rawTime.Minutes != 0) timeLeft += $"{rawTime.Minutes} minutes, "; + if (rawTime.Seconds != 0) timeLeft += $"{rawTime.Seconds} seconds"; + } + + await eventArgs.Context.CreateResponseAsync($"To use that command you need to wait {timeLeft}", true).ConfigureAwait(false); + return; + } + _logger.LogError(eventArgs.Exception, "{Username} tried to execute slashcommand /{CommandName}, but it errored", eventArgs.Context.User.Username, eventArgs.Context.CommandName); - - return Task.CompletedTask; } private Task OnSlashCommandsExecuted(SlashCommandsExtension _, SlashCommandExecutedEventArgs eventArgs)