Skip to content

Commit

Permalink
"Translate To..." Context Menu Message Command (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
austins authored Oct 6, 2024
1 parent 6963050 commit 2634a9c
Show file tree
Hide file tree
Showing 46 changed files with 2,057 additions and 937 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ dotnet_diagnostic.SA1602.severity = none
dotnet_diagnostic.IDE0005.severity = error
dotnet_diagnostic.CA1812.severity = none
dotnet_diagnostic.SA1201.severity = none
dotnet_diagnostic.S125.severity = none
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>S1135</WarningsNotAsErrors>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace DiscordTranslationBot.Commands.TempReplies;
/// <summary>
/// Sends a temp reply.
/// </summary>
/// <remarks>
/// This is needed in cases where it's not possible to send an ephemeral message, which are only possible to send with
/// message and slash commands, but not when sending a new message to a channel such as when we are handling a reaction.
/// </remarks>
public sealed class SendTempReply : ICommand
{
/// <summary>
Expand Down
34 changes: 30 additions & 4 deletions src/DiscordTranslationBot/Constants/MessageCommandConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,39 @@ namespace DiscordTranslationBot.Constants;
public static class MessageCommandConstants
{
/// <summary>
/// "Translate" message command constants.
/// "Translate (Auto)" message command constants.
/// </summary>
public static class Translate
public static class TranslateAuto
{
/// <summary>
/// The name of the "Translate" message command.
/// The name of the "Translate (Auto)" message command.
/// </summary>
public const string CommandName = "Translate";
public const string CommandName = "Translate (Auto)";
}

/// <summary>
/// "Translate To..." message command constants.
/// </summary>
public static class TranslateTo
{
/// <summary>
/// The name of the "Translate To..." message command.
/// </summary>
public const string CommandName = "Translate To...";

/// <summary>
/// The unique custom ID of the select menu.
/// </summary>
public const string SelectMenuId = $"{nameof(TranslateTo)}_SelectMenu";

/// <summary>
/// The unique custom ID of the translate button.
/// </summary>
public const string TranslateButtonId = $"{nameof(TranslateTo)}_TranslateButton";

/// <summary>
/// The unique custom ID of the translate and share button.
/// </summary>
public const string TranslateAndShareButtonId = $"{nameof(TranslateTo)}_TranslateAndShareButton";
}
}
20 changes: 14 additions & 6 deletions src/DiscordTranslationBot/Discord/DiscordEventListener.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Discord;
using Discord.WebSocket;
using DiscordTranslationBot.Commands.Logging;
using DiscordTranslationBot.Discord.Events;
using DiscordTranslationBot.Discord.Models;
using DiscordTranslationBot.Notifications.Events;

namespace DiscordTranslationBot.Discord;

Expand Down Expand Up @@ -38,7 +38,7 @@ public DiscordEventListener(
public Task InitializeEventsAsync(CancellationToken cancellationToken)
{
// Discord client initiated events can run on its gateway thread.
_client.Ready += async () => await _mediator.Publish(new ReadyEvent(), cancellationToken);
_client.Ready += async () => await _mediator.Publish(new ReadyNotification(), cancellationToken);

_client.Log += async logMessage => await _mediator.Send(
new RedirectLogMessageToLogger { LogMessage = logMessage },
Expand All @@ -47,15 +47,23 @@ public Task InitializeEventsAsync(CancellationToken cancellationToken)
// User initiated events should run on a new thread to not block the gateway thread.
// Each event published can act within a per-request scope.
_client.JoinedGuild += guild => PublishInBackgroundAsync(
new JoinedGuildEvent { Guild = guild },
new JoinedGuildNotification { Guild = guild },
cancellationToken);

_client.MessageCommandExecuted += messageCommand => PublishInBackgroundAsync(
new MessageCommandExecutedEvent { MessageCommand = messageCommand },
new MessageCommandExecutedNotification { Interaction = messageCommand },
cancellationToken);

_client.SelectMenuExecuted += interaction => PublishInBackgroundAsync(
new SelectMenuExecutedNotification { Interaction = interaction },
cancellationToken);

_client.ButtonExecuted += interaction => PublishInBackgroundAsync(
new ButtonExecutedNotification { Interaction = interaction },
cancellationToken);

_client.SlashCommandExecuted += slashCommand => PublishInBackgroundAsync(
new SlashCommandExecutedEvent { SlashCommand = slashCommand },
new SlashCommandExecutedNotification { Interaction = slashCommand },
cancellationToken);

_client.ReactionAdded += async (messageCache, channel, reaction) =>
Expand All @@ -66,7 +74,7 @@ public Task InitializeEventsAsync(CancellationToken cancellationToken)
if (message is not null)
{
await PublishInBackgroundAsync(
new ReactionAddedEvent
new ReactionAddedNotification
{
Message = message,
Channel = await channel.GetOrDownloadAsync(),
Expand Down

This file was deleted.

6 changes: 0 additions & 6 deletions src/DiscordTranslationBot/Discord/Events/ReadyEvent.cs

This file was deleted.

This file was deleted.

24 changes: 24 additions & 0 deletions src/DiscordTranslationBot/Discord/Models/JumpUrl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace DiscordTranslationBot.Discord.Models;

public sealed class JumpUrl
{
/// <summary>
/// Indicates if this jump URL is for a direct-message channel.
/// </summary>
public required bool IsDmChannel { get; init; }

/// <summary>
/// Guild ID. Null if <see cref="IsDmChannel" /> is true.
/// </summary>
public required ulong? GuildId { get; init; }

/// <summary>
/// Channel ID.
/// </summary>
public required ulong ChannelId { get; init; }

/// <summary>
/// Message ID.
/// </summary>
public required ulong MessageId { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using DiscordTranslationBot.Extensions;

namespace DiscordTranslationBot.Mediator;

public sealed class ValidatedTaskWhenAllPublisher : INotificationPublisher
{
private readonly TaskWhenAllPublisher _taskWhenAllPublisher = new();

public ValueTask Publish<TNotification>(
NotificationHandlers<TNotification> handlers,
TNotification notification,
CancellationToken cancellationToken)
where TNotification : INotification
{
if (!notification.TryValidate(out var validationResults))
{
throw new MessageValidationException(notification.GetType().Name, validationResults);
}

return _taskWhenAllPublisher.Publish(handlers, notification, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
using Discord;

namespace DiscordTranslationBot.Notifications.Events;

/// <summary>
/// Notification for the Discord ButtonExecuted event.
/// </summary>
public sealed class ButtonExecutedNotification : INotification
{
/// <summary>
/// The button component interaction.
/// </summary>
[Required]
public required IComponentInteraction Interaction { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System.ComponentModel.DataAnnotations;
using Discord;

namespace DiscordTranslationBot.Discord.Events;
namespace DiscordTranslationBot.Notifications.Events;

/// <summary>
/// Notification for the Discord joined guild event.
/// </summary>
public sealed class JoinedGuildEvent : INotification
public sealed class JoinedGuildNotification : INotification
{
/// <summary>
/// The guild that the bot joined.
/// </summary>
[Required]
public required IGuild Guild { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
using Discord;

namespace DiscordTranslationBot.Notifications.Events;

/// <summary>
/// Notification for the Discord MessageCommandExecuted event.
/// </summary>
public sealed class MessageCommandExecutedNotification : INotification
{
/// <summary>
/// The message command interaction.
/// </summary>
[Required]
public required IMessageCommandInteraction Interaction { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
using System.ComponentModel.DataAnnotations;
using Discord;
using DiscordTranslationBot.Discord.Models;

namespace DiscordTranslationBot.Discord.Events;
namespace DiscordTranslationBot.Notifications.Events;

/// <summary>
/// Notification for the Discord ReactionAdded event.
/// </summary>
public sealed class ReactionAddedEvent : INotification
public sealed class ReactionAddedNotification : INotification
{
/// <summary>
/// The user message.
/// </summary>
[Required]
public required IUserMessage Message { get; init; }

/// <summary>
/// The channel.
/// </summary>
[Required]
public required IMessageChannel Channel { get; init; }

/// <summary>
/// The reaction.
/// </summary>
[Required]
public required ReactionInfo ReactionInfo { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DiscordTranslationBot.Notifications.Events;

/// <summary>
/// Notification for the Discord ready event.
/// </summary>
public sealed class ReadyNotification : INotification;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
using Discord;

namespace DiscordTranslationBot.Notifications.Events;

/// <summary>
/// Notification for the Discord SelectMenuExecuted event.
/// </summary>
public sealed class SelectMenuExecutedNotification : INotification
{
/// <summary>
/// The select menu component interaction.
/// </summary>
[Required]
public required IComponentInteraction Interaction { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
using Discord;

namespace DiscordTranslationBot.Notifications.Events;

/// <summary>
/// Notification for the Discord SlashCommandExecuted event.
/// </summary>
public sealed class SlashCommandExecutedNotification : INotification
{
/// <summary>
/// The slash command interaction.
/// </summary>
[Required]
public required ISlashCommandInteraction Interaction { get; init; }
}
Loading

0 comments on commit 2634a9c

Please sign in to comment.