mirror of
https://github.com/RPCS3/discord-bot.git
synced 2026-07-21 01:05:27 -04:00
38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using CompatBot.Database;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace CompatBot.Utils;
|
|
|
|
internal static partial class ExplanationFormatter
|
|
{
|
|
[GeneratedRegex(@"</(?<name>(\w|\s)+)(:\d+)?>", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture)]
|
|
private static partial Regex CmdMention();
|
|
|
|
public static async ValueTask<string> FormatTextAsync(this Explanation explanation, DiscordClient client)
|
|
{
|
|
if (explanation is not { Text: {Length: >0} result })
|
|
return "";
|
|
|
|
var matches = CmdMention().Matches(result).Select((Match m) => (m.Value, m.Groups["name"].Value)).Distinct().ToList();
|
|
foreach (var (substr, name) in matches)
|
|
{
|
|
try
|
|
{
|
|
var nameParts = name.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
if (await client.GetGlobalApplicationCommandAsync(nameParts[0]).ConfigureAwait(false) is { } cmd)
|
|
{
|
|
var mention = nameParts is [_]
|
|
? cmd.Mention
|
|
: cmd.GetSubcommandMention(nameParts[1..]);
|
|
result = result.Replace(substr, mention, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|