Skip to content

Commit

Permalink
Safer fire-and-forget for flag emoji SendTempMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
austins committed Sep 9, 2023
1 parent e0b7c8d commit e121116
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
1 change: 1 addition & 0 deletions DiscordTranslationBot/DiscordTranslationBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="6.0.6"/>
<PackageReference Include="Discord.Net" Version="3.12.0"/>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.7.1"/>
<PackageReference Include="Humanizer.Core" Version="2.14.1"/>
Expand Down
68 changes: 36 additions & 32 deletions DiscordTranslationBot/Handlers/FlagEmojiReactionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Discord;
using AsyncAwaitBestPractices;
using Discord;
using DiscordTranslationBot.Exceptions;
using DiscordTranslationBot.Models.Discord;
using DiscordTranslationBot.Models.Providers.Translation;
Expand Down Expand Up @@ -188,42 +189,39 @@ public virtual void SendTempMessage(
uint seconds = 10
)
{
using var typingState = channel.EnterTypingState();
HandleSendTempMessage().SafeFireAndForget(ex => _log.FailedToSendTempMessage(ex, referencedMessageId, text));
return;

// Wrapped in Task.Run to not block the handler as the cleanup has a delay of over 3 seconds.
_ = Task.Run(
async () =>
{
// Send reply message.
var replyMessage = await channel.SendMessageAsync(
text,
messageReference: new MessageReference(referencedMessageId),
options: new RequestOptions { CancelToken = cancellationToken }
);
async Task HandleSendTempMessage()
{
// Send reply message.
var replyMessage = await channel.SendMessageAsync(
text,
messageReference: new MessageReference(referencedMessageId),
options: new RequestOptions { CancelToken = cancellationToken }
);

// Cleanup.
await Task.Delay(TimeSpan.FromSeconds(seconds), cancellationToken);
// Cleanup.
await Task.Delay(TimeSpan.FromSeconds(seconds), cancellationToken);

// If the source message still exists, remove the reaction from it.
var sourceMessage = await replyMessage.Channel.GetMessageAsync(
referencedMessageId,
options: new RequestOptions { CancelToken = cancellationToken }
);
// If the source message still exists, remove the reaction from it.
var sourceMessage = await replyMessage.Channel.GetMessageAsync(
referencedMessageId,
options: new RequestOptions { CancelToken = cancellationToken }
);

if (sourceMessage != null)
{
await sourceMessage.RemoveReactionAsync(
reaction.Emote,
reaction.UserId,
new RequestOptions { CancelToken = cancellationToken }
);
}
if (sourceMessage != null)
{
await sourceMessage.RemoveReactionAsync(
reaction.Emote,
reaction.UserId,
new RequestOptions { CancelToken = cancellationToken }
);
}

// Delete the reply message.
await replyMessage.DeleteAsync(new RequestOptions { CancelToken = cancellationToken });
},
cancellationToken
);
// Delete the reply message.
await replyMessage.DeleteAsync(new RequestOptions { CancelToken = cancellationToken });
}
}

private sealed partial class Log
Expand Down Expand Up @@ -255,5 +253,11 @@ public Log(ILogger<FlagEmojiReactionHandler> logger)
Message = "Couldn't detect the source language to translate from. This could happen when the provider's detected language confidence is 0 or the source language is the same as the target language."
)]
public partial void FailureToDetectSourceLanguage();

[LoggerMessage(
Level = LogLevel.Error,
Message = "Failed to send temp message for reaction to message ID {referencedMessageId} with text: {text}"
)]
public partial void FailedToSendTempMessage(Exception ex, ulong referencedMessageId, string text);
}
}
8 changes: 0 additions & 8 deletions DiscordTranslationBot/appsettings.Development.json

This file was deleted.

3 changes: 1 addition & 2 deletions DiscordTranslationBot/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
"Default": "Information"
}
}
}

0 comments on commit e121116

Please sign in to comment.