Skip to content

Commit

Permalink
Update translation text if author is same as interaction user
Browse files Browse the repository at this point in the history
  • Loading branch information
austins committed Oct 7, 2024
1 parent fc83374 commit 425fe6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/DiscordTranslationBot/Services/MessageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -83,7 +88,7 @@ public interface IMessageHelper
/// </remarks>
/// <param name="referencedMessage">The message being translated.</param>
/// <param name="translationResult">The translation result.</param>
/// <param name="interactionUserId">Optionally, the user who invoked the interaction.</param>
/// <param name="interactionUserId">Optionally, the user who invoked the interaction, which will be mentioned.</param>
/// <returns>Content text with jump URL and info of message being translated.</returns>
public string BuildTranslationReplyWithReference(
IMessage referencedMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,40 @@ public void BuildTranslationReplyWithReference_ReturnsExpected(
// Assert
result.Should().Be(expected);
}

[Fact]
public void BuildTranslationReplyWithReference_SameUser_ReturnsExpected()
{
// Arrange
var message = Substitute.For<IMessage>();
message.Id.Returns(1UL);

const ulong interactionUserId = 2UL;
message.Author.Id.Returns(interactionUserId);

var channel = Substitute.For<ITextChannel>();
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);
}
}

0 comments on commit 425fe6b

Please sign in to comment.