using System.Text.RegularExpressions; namespace EllieBot.Common; public sealed class ReplacementContext { public DiscordSocketClient? Client { get; } public IGuild? Guild { get; } public IMessageChannel? Channel { get; } public IUser? User { get; } private readonly List _overrides = new(); private readonly HashSet _tokens = new(); public IReadOnlyList Overrides => _overrides.AsReadOnly(); private readonly List _regexOverrides = new(); private readonly HashSet _regexPatterns = new(); public IReadOnlyList RegexOverrides => _regexOverrides.AsReadOnly(); public ReplacementContext(ICommandContext cmdContext) : this(cmdContext.Client as DiscordSocketClient, cmdContext.Guild, cmdContext.Channel, cmdContext.User) { } public ReplacementContext( DiscordSocketClient? client = null, IGuild? guild = null, IMessageChannel? channel = null, IUser? user = null) { Client = client; Guild = guild; Channel = channel; User = user; } public ReplacementContext WithOverride(string key, Func> repFactory) { if (_tokens.Add(key)) { _overrides.Add(new(key, repFactory)); } return this; } public ReplacementContext WithOverride(string key, Func repFactory) => WithOverride(key, () => new ValueTask(repFactory())); public ReplacementContext WithOverride(Regex regex, Func> repFactory) { if (_regexPatterns.Add(regex.ToString())) { _regexOverrides.Add(new(regex, repFactory)); } return this; } public ReplacementContext WithOverride(Regex regex, Func repFactory) => WithOverride(regex, (Match m) => new ValueTask(repFactory(m))); }