Skip to content

Commit

Permalink
Defer to acknowledge commands and followup instead of directly respon…
Browse files Browse the repository at this point in the history
…ding to allow more than 3 seconds to respond
  • Loading branch information
austins committed Sep 5, 2023
1 parent 16f8251 commit 70880de
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ public async Task Handle_MessageCommandExecutedNotification_Success()
// Assert
_ = _translationProviders[0].Received(2).SupportedLanguages;

await _command.Received(1).DeferAsync(true, Arg.Any<RequestOptions>());

await _command
.Received(1)
.RespondAsync(
.FollowupAsync(
embed: Arg.Is<Embed>(x => x.Title == "Translated Message"),
ephemeral: true,
options: Arg.Any<RequestOptions>()
Expand All @@ -104,7 +106,7 @@ public async Task Handle_MessageCommandExecutedNotification_NotTranslateCommand_

await notification.Command
.DidNotReceive()
.RespondAsync(Arg.Any<string>(), ephemeral: Arg.Any<bool>(), options: Arg.Any<RequestOptions>());
.FollowupAsync(Arg.Any<string>(), ephemeral: Arg.Any<bool>(), options: Arg.Any<RequestOptions>());
}

[Fact]
Expand Down Expand Up @@ -163,7 +165,7 @@ public async Task Handle_MessageCommandExecutedNotification_UsesNextTranslationP

await _command
.Received(1)
.RespondAsync(
.FollowupAsync(
embed: Arg.Is<Embed>(x => x.Title == "Translated Message"),
ephemeral: true,
options: Arg.Any<RequestOptions>()
Expand All @@ -184,7 +186,7 @@ public async Task Handle_MessageCommandExecutedNotification_Returns_WhenTranslat
// Assert
await _command
.Received(1)
.RespondAsync(
.FollowupAsync(
"Translating this bot's messages isn't allowed.",
ephemeral: true,
options: Arg.Any<RequestOptions>()
Expand Down Expand Up @@ -221,7 +223,7 @@ public async Task Handle_MessageCommandExecutedNotification_Returns_IfNoProvider

await _command
.Received(1)
.RespondAsync(
.FollowupAsync(
$"Your locale {userLocale} isn't supported for translation via this action.",
ephemeral: true,
options: Arg.Any<RequestOptions>()
Expand Down Expand Up @@ -264,7 +266,7 @@ public async Task Handle_MessageCommandExecutedNotification_Returns_WhenTranslat

await _command
.Received(1)
.RespondAsync(
.FollowupAsync(
"The message couldn't be translated. It might already be in your language or the translator failed to detect its source language.",
ephemeral: true,
options: Arg.Any<RequestOptions>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ await _translationProvider
Arg.Is<SupportedLanguage>(x => x.LangCode == sourceLanguage.LangCode)
);

await command.Received(1).DeferAsync(true, Arg.Any<RequestOptions>());

await command
.Received(1)
.RespondAsync(
.FollowupAsync(
Arg.Is<string>(text => text.Contains($"translated text using {ProviderName} from")),
options: Arg.Any<RequestOptions>()
);
Expand All @@ -123,7 +125,7 @@ public async Task Handle_SlashCommandExecutedNotification_NotTranslateCommand_Re

await notification.Command
.DidNotReceive()
.RespondAsync(Arg.Any<string>(), ephemeral: Arg.Any<bool>(), options: Arg.Any<RequestOptions>());
.FollowupAsync(Arg.Any<string>(), ephemeral: Arg.Any<bool>(), options: Arg.Any<RequestOptions>());
}

[Fact]
Expand Down Expand Up @@ -156,7 +158,7 @@ public async Task Handle_SlashCommandExecutedNotification_Returns_SourceTextIsEm
// Assert
await command
.Received(1)
.RespondAsync("Nothing to translate.", ephemeral: true, options: Arg.Any<RequestOptions>());
.FollowupAsync("Nothing to translate.", ephemeral: true, options: Arg.Any<RequestOptions>());

await _translationProvider
.DidNotReceive()
Expand Down Expand Up @@ -240,7 +242,7 @@ await _translationProvider

await command
.Received(1)
.RespondAsync(
.FollowupAsync(
"Couldn't detect the source language to translate from or the result is the same.",
ephemeral: true,
options: Arg.Any<RequestOptions>()
Expand Down
10 changes: 6 additions & 4 deletions DiscordTranslationBot/Handlers/TranslateMessageCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public async Task Handle(MessageCommandExecutedNotification notification, Cancel
return;
}

await notification.Command.DeferAsync(true, new RequestOptions { CancelToken = cancellationToken });

if (notification.Command.Data.Message.Author.Id == _client.CurrentUser?.Id)
{
_log.TranslatingBotMessageDisallowed();

await notification.Command.RespondAsync(
await notification.Command.FollowupAsync(
"Translating this bot's messages isn't allowed.",
ephemeral: true,
options: new RequestOptions { CancelToken = cancellationToken }
Expand Down Expand Up @@ -115,7 +117,7 @@ await notification.Command.RespondAsync(
if (translationResult == null)
{
// Send message if no translation providers support the locale.
await notification.Command.RespondAsync(
await notification.Command.FollowupAsync(
$"Your locale {userLocale} isn't supported for translation via this action.",
ephemeral: true,
options: new RequestOptions { CancelToken = cancellationToken }
Expand All @@ -128,7 +130,7 @@ await notification.Command.RespondAsync(
{
_log.FailureToDetectSourceLanguage();

await notification.Command.RespondAsync(
await notification.Command.FollowupAsync(
"The message couldn't be translated. It might already be in your language or the translator failed to detect its source language.",
ephemeral: true,
options: new RequestOptions { CancelToken = cancellationToken }
Expand Down Expand Up @@ -156,7 +158,7 @@ await notification.Command.RespondAsync(
{translationResult.TranslatedText}
""";

await notification.Command.RespondAsync(
await notification.Command.FollowupAsync(
embed: new EmbedBuilder()
.WithTitle("Translated Message")
.WithUrl(GetJumpUrl(notification.Command.Data.Message).AbsoluteUri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public async Task Handle(SlashCommandExecutedNotification notification, Cancella
return;
}

await notification.Command.DeferAsync(true, new RequestOptions { CancelToken = cancellationToken });

// Get the input values.
var options = notification.Command.Data.Options;

Expand All @@ -55,7 +57,8 @@ public async Task Handle(SlashCommandExecutedNotification notification, Cancella
if (string.IsNullOrWhiteSpace(sanitizedText))
{
_log.EmptySourceText();
await notification.Command.RespondAsync(

await notification.Command.FollowupAsync(
"Nothing to translate.",
ephemeral: true,
options: new RequestOptions { CancelToken = cancellationToken }
Expand Down Expand Up @@ -84,7 +87,7 @@ await notification.Command.RespondAsync(
{
_log.FailureToDetectSourceLanguage();

await notification.Command.RespondAsync(
await notification.Command.FollowupAsync(
"Couldn't detect the source language to translate from or the result is the same.",
ephemeral: true,
options: new RequestOptions { CancelToken = cancellationToken }
Expand All @@ -93,7 +96,7 @@ await notification.Command.RespondAsync(
return;
}

await notification.Command.RespondAsync(
await notification.Command.FollowupAsync(
$"""
{MentionUtils.MentionUser(notification.Command.User.Id)} translated text using {translationProvider.ProviderName} from {Format.Italics(sourceLanguage?.Name ?? translationResult.DetectedLanguageName)}:
{Format.Quote(sanitizedText)}
Expand Down

0 comments on commit 70880de

Please sign in to comment.