using Discord.Exception; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Discord.Converter { #region Public Area public static class InteractionFormatter { public static string TransformEnumsToId(params Enum[] enums) { string output = ""; foreach (var num in enums) { output += num.ToString("D") + ","; } output = output.Remove(output.Length - 1); return output; } /// /// Transforms the given values to the given enums. CAUTION: The length of the given strings and enums must be the same. /// /// The list of values separated by comma (e.g. "0,5,3") /// The list of enums that should be given out (e.g. typeof(Enum), typeof(Enum)"/> /// A list of converted enums that were passed in. /// Thrown when the length of enums and values are different. public static IEnumerable TransformIdToEnums(string value, params Type[] enums) { string[] values = value.Split(','); if (values.Length != enums.Length) { throw new MismatchedEnumLengthsException(); } for (int i = 0; i < enums.Length; i++) { yield return (Enum)Enum.Parse(enums[i], value); } } public static T GetFirstEnumFromIdAndRemove(ref string[] values) { string firstValue = values[0]; values = values.Skip(1).ToArray(); return (T)Enum.Parse(typeof(T), firstValue); } public static T GetFirstEnumFromId(ref string[] values) { return (T)Enum.Parse(typeof(T), values[0]); } public static T GetEnumFromId(string value) { return (T)Enum.Parse(typeof(T), value); } } #endregion Public Area }