Files
archived-discord-bot/CompatBot/EventHandlers/TableFlipMonitor.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

97 lines
3.9 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CompatApiClient.Utils;
using CompatBot.Commands;
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using Microsoft.Extensions.Caching.Memory;
namespace CompatBot.EventHandlers;
internal static partial class TableFlipMonitor
{
[GeneratedRegex(@"(🎲|\s)+")]
private static partial Regex DiceRoll();
private static readonly char[] OpenParen = ['(', '', 'ʕ'];
public static async Task OnMessageCreated(DiscordClient _, MessageCreateEventArgs args)
{
if (DefaultHandlerFilter.IsFluff(args.Message))
return;
/*
* (╯°□°)╯︵ ┻━┻
* ( ゜Д゜) ︵ ┻━┻
* (ノಠ益ಠ)ノ彡┻━┻
* (ノಥ益ಥ)ノ ┻━┻
* (ノಥДಥ)ノ︵┻━┻・/
* (^_^)ノ┻━┻
* (/¯◡ ‿ ◡)/¯ ~ ┻━┻
*
* this might look the same, but only because of the font choice
*
* ┻━┻
* ┻━┻
*/
try
{
var content = args.Message.Content;
if (content.Contains("🎲") && DiceRoll().IsMatch(content))
{
var count = 1;
var idx = content.IndexOf("🎲");
while (idx < content.Length && (idx = content.IndexOf("🎲", idx + 1)) > 0)
count++;
EmpathySimulationHandler.Throttling.Set(args.Channel.Id, new List<DiscordMessage> {args.Message}, EmpathySimulationHandler.ThrottleDuration);
await Misc.RollImpl(args.Message, $"{count}d6").ConfigureAwait(false);
return;
}
if (content.Trim() == "🥠")
{
EmpathySimulationHandler.Throttling.Set(args.Channel.Id, new List<DiscordMessage> {args.Message}, EmpathySimulationHandler.ThrottleDuration);
await Fortune.ShowFortune(args.Message, args.Author).ConfigureAwait(false);
return;
}
if (!(content.Contains("┻━┻") ||
content.Contains("┻━┻")))
return;
var tableIdx = content.IndexOf("┻━┻", StringComparison.Ordinal);
if (tableIdx < 0)
tableIdx = content.IndexOf("┻━┻", StringComparison.Ordinal);
var faceIdx = content[..tableIdx].LastIndexOfAny(OpenParen);
var face = content[faceIdx..tableIdx];
if (face.Length > 30)
return;
var reverseFace = face
.Replace("(╯", "╯(").Replace("(ノ", "ノ(").Replace("(", "(").Replace("(/¯", @"\_/(")
.Replace(")╯", "╯)").Replace(")ノ", "ノ)").Replace(")", ")").Replace(")/¯", @"\_/)")
.Replace("(╯", "╯(").Replace("(ノ", "ノ(").Replace("", "").Replace("/¯", @"\_/")
.Replace(")╯", "╯)").Replace(")ノ", "ノ)").Replace("", "").Replace("/¯", @"\_/")
.Replace("ʕ╯", "╯ʕ").Replace("ʕノ", "ノʕ").Replace("ʕノ", "ノʕ").Replace("ʕ/¯", @"\_/ʕ")
.Replace("ʔ╯", "╯ʔ").Replace("ʔノ", "ノʔ").Replace("ʔ", "ʔ").Replace("ʔ/¯", @"\_/ʔ")
.TrimEnd('︵', '彡', ' ', ' ', '~', '');
if (reverseFace == face)
return;
var faceLength = reverseFace.Length;
if (faceLength > 5 + 4)
reverseFace = $"{reverseFace[..2]}ಠ益ಠ{reverseFace[^2..]}";
await args.Channel.SendMessageAsync("┬─┬ " + reverseFace.Sanitize()).ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Warn(e);
}
}
}