From 425fe6b879abe61e3e2cdc3ee33100d5474a78e8 Mon Sep 17 00:00:00 2001 From: austins Date: Mon, 7 Oct 2024 03:08:58 -0700 Subject: [PATCH] Update translation text if author is same as interaction user --- .../Services/MessageHelper.cs | 9 +++-- .../Services/MessageHelperTests.cs | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/DiscordTranslationBot/Services/MessageHelper.cs b/src/DiscordTranslationBot/Services/MessageHelper.cs index bc4df3b..92635ba 100644 --- a/src/DiscordTranslationBot/Services/MessageHelper.cs +++ b/src/DiscordTranslationBot/Services/MessageHelper.cs @@ -42,7 +42,12 @@ public string BuildTranslationReplyWithReference( ulong? interactionUserId = null) { var replyText = - $"{(interactionUserId is null ? "You" : MentionUtils.MentionUser(interactionUserId.Value))} translated {GetJumpUrl(referencedMessage)} by {MentionUtils.MentionUser(referencedMessage.Author.Id)}"; + $"{(interactionUserId is null ? "You" : MentionUtils.MentionUser(interactionUserId.Value))} translated {GetJumpUrl(referencedMessage)}"; + + if (interactionUserId != referencedMessage.Author.Id) + { + replyText += $" by {MentionUtils.MentionUser(referencedMessage.Author.Id)}"; + } if (!string.IsNullOrWhiteSpace(translationResult.DetectedLanguageCode)) { @@ -83,7 +88,7 @@ public interface IMessageHelper /// /// The message being translated. /// The translation result. - /// Optionally, the user who invoked the interaction. + /// Optionally, the user who invoked the interaction, which will be mentioned. /// Content text with jump URL and info of message being translated. public string BuildTranslationReplyWithReference( IMessage referencedMessage, diff --git a/tests/DiscordTranslationBot.Tests.Unit/Services/MessageHelperTests.cs b/tests/DiscordTranslationBot.Tests.Unit/Services/MessageHelperTests.cs index 0a967a3..f8dddc9 100644 --- a/tests/DiscordTranslationBot.Tests.Unit/Services/MessageHelperTests.cs +++ b/tests/DiscordTranslationBot.Tests.Unit/Services/MessageHelperTests.cs @@ -147,4 +147,40 @@ public void BuildTranslationReplyWithReference_ReturnsExpected( // Assert result.Should().Be(expected); } + + [Fact] + public void BuildTranslationReplyWithReference_SameUser_ReturnsExpected() + { + // Arrange + var message = Substitute.For(); + message.Id.Returns(1UL); + + const ulong interactionUserId = 2UL; + message.Author.Id.Returns(interactionUserId); + + var channel = Substitute.For(); + channel.Id.Returns(3UL); + channel.GuildId.Returns(4UL); + message.Channel.Returns(channel); + + var messageJumpUrl = _sut.GetJumpUrl(message); + + var translationResult = new TranslationResult + { + DetectedLanguageCode = "en-US", + DetectedLanguageName = "English", + TargetLanguageCode = "de", + TargetLanguageName = "German", + TranslatedText = "TRANSLATED_TEXT" + }; + + var expected = + $"<@{interactionUserId}> translated {messageJumpUrl} from *{translationResult.DetectedLanguageName}* to *{translationResult.TargetLanguageName}*:\n>>> {translationResult.TranslatedText}"; + + // Act + var result = _sut.BuildTranslationReplyWithReference(message, translationResult, interactionUserId); + + // Assert + result.Should().Be(expected); + } }