discord-bot/CompatBot/EventHandlers/PostLogHelpHandler.cs
Ilya 72dbc4074a
Some performance and code optimizations (#955)
* upgrade deps, remove wrong ppu hashes

* upgrade compiler packages

will require container pull after build

* replace Regex with compiler-generated versions

* use new collection initialization syntax

* configure global defaults for regex

* bump min amd driver version

fixes #950

* add macos version check

fixes #948

* fix #954 (!sudo log date)
2024-05-18 14:26:34 +01:00

73 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.Commands.Attributes;
using CompatBot.Database;
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.EventArgs;
using Microsoft.EntityFrameworkCore;
namespace CompatBot.EventHandlers;
internal static partial class PostLogHelpHandler
{
[GeneratedRegex(
@"\b((?<vulkan>(vul[ck][ae]n(-?1)?))|(?<help>(post|upload|send|give)(ing)?\s+((a|the|rpcs3('s)?|your|you're|ur|my|full|game)\s+)*\blogs?))\b",
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Singleline
)]
private static partial Regex UploadLogMention();
private static readonly SemaphoreSlim TheDoor = new(1, 1);
private static readonly TimeSpan ThrottlingThreshold = TimeSpan.FromSeconds(5);
private static readonly Dictionary<string, Explanation> DefaultExplanation = new()
{
["log"] = new() { Text = "To upload log, run the game, then completely close RPCS3, then drag and drop rpcs3.log.gz from the RPCS3 folder into Discord. The file may have a zip or rar icon." },
["vulkan-1"] = new() { Text = "Please remove all the traces of video drivers using DDU, and then reinstall the latest driver version for your GPU." },
};
private static DateTime lastMention = DateTime.UtcNow.AddHours(-1);
public static async Task OnMessageCreated(DiscordClient _, MessageCreateEventArgs args)
{
if (DefaultHandlerFilter.IsFluff(args.Message))
return;
if (!LimitedToHelpChannel.IsHelpChannel(args.Channel))
return;
if (DateTime.UtcNow - lastMention < ThrottlingThreshold)
return;
var match = UploadLogMention().Match(args.Message.Content);
if (!match.Success || string.IsNullOrEmpty(match.Groups["help"].Value))
return;
if (!await TheDoor.WaitAsync(0).ConfigureAwait(false))
return;
try
{
var explanation = await GetExplanationAsync(string.IsNullOrEmpty(match.Groups["vulkan"].Value) ? "log" : "vulkan-1").ConfigureAwait(false);
var lastBotMessages = await args.Channel.GetMessagesBeforeCachedAsync(args.Message.Id, 10).ConfigureAwait(false);
foreach (var msg in lastBotMessages)
if (BotReactionsHandler.NeedToSilence(msg).needToChill
|| msg.Author.IsCurrent && msg.Content == explanation.Text)
return;
await args.Channel.SendMessageAsync(explanation.Text, explanation.Attachment, explanation.AttachmentFilename).ConfigureAwait(false);
lastMention = DateTime.UtcNow;
}
finally
{
TheDoor.Release();
}
}
public static async Task<Explanation> GetExplanationAsync(string term)
{
await using var db = new BotDb();
var result = await db.Explanation.FirstOrDefaultAsync(e => e.Keyword == term).ConfigureAwait(false);
return result ?? DefaultExplanation[term];
}
}