using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace NadekoBot.Common.Replacements { public class Replacer { private readonly IEnumerable<(string Key, Func Text)> _replacements; private readonly IEnumerable<(Regex Regex, Func Replacement)> _regex; public Replacer(IEnumerable<(string, Func)> replacements, IEnumerable<(Regex, Func)> regex) { _replacements = replacements; _regex = regex; } public string Replace(string input) { if (string.IsNullOrWhiteSpace(input)) return input; foreach (var item in _replacements) { if (input.Contains(item.Key)) input = input.Replace(item.Key, item.Text()); } foreach (var item in _regex) { input = item.Regex.Replace(input, (m) => item.Replacement(m)); } return input; } public void Replace(CREmbed embedData) { embedData.PlainText = Replace(embedData.PlainText); embedData.Description = Replace(embedData.Description); embedData.Title = Replace(embedData.Title); if (embedData.Fields != null) foreach (var f in embedData.Fields) { f.Name = Replace(f.Name); f.Value = Replace(f.Value); } if (embedData.Footer != null) embedData.Footer.Text = Replace(embedData.Footer.Text); } } }