Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Feature/silent message slash command #689

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using POI.DiscordDotNet.Commands.SlashCommands.Utils;
Expand All @@ -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<UptimeCommand>().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<SilentMessageSlashCommands>().Handle(ctx, messageType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace POI.DiscordDotNet.Commands.SlashCommands.Utils;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddUtilitySlashCommands(this IServiceCollection services) => services
.AddTransient<UptimeCommand>();
.AddTransient<UptimeCommand>().AddTransient<SilentMessageSlashCommands>();
}
Original file line number Diff line number Diff line change
@@ -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, string> _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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down