diff --git a/Discord/Attribute/EventControllerAttribute.cs b/Discord/Attribute/EventControllerAttribute.cs new file mode 100644 index 0000000..5adfa51 --- /dev/null +++ b/Discord/Attribute/EventControllerAttribute.cs @@ -0,0 +1,17 @@ +namespace Discord.Attribute +{ + using Controller; + /// + /// To be placed over dynamic methods that will be executed by the + /// + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public class EventControllerAttribute : System.Attribute + { + public T Event { get; set; } + public EventControllerAttribute(T Event) + { + this.Event = Event; + } + } +} \ No newline at end of file diff --git a/Discord/Class1.cs b/Discord/Class1.cs deleted file mode 100644 index b9e60b3..0000000 --- a/Discord/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Discord -{ - public class Class1 - { - - } -} \ No newline at end of file diff --git a/Discord/Controller/EventController.cs b/Discord/Controller/EventController.cs new file mode 100644 index 0000000..980fa47 --- /dev/null +++ b/Discord/Controller/EventController.cs @@ -0,0 +1,71 @@ +using Discord.Attribute; +using System.Collections.Generic; +using System.Reflection; +using System.Resources; + +namespace Discord.Controller +{ + public static class EventController + { + /// + /// To be executed by the Discord Events that use Ids. + /// + /// The class that should be looked in by the controller, defined by using + /// The base enum that will get separated and passed to the actual controller + /// Passed Ids by the event e.g e.Interaction.Data.CustomId + /// The passed Event args + public static void InvokeEventControllerClass(string ids, object e) where ENUM : class + { + string[] splittedIds = ids.Split(','); + var category = Converter.InteractionFormatter.GetFirstEnumFromIdAndRemove(ref splittedIds); + InvokeEventController(category, splittedIds, e); + } + /// + /// Invokes the event controller by passing over the given class and enum. This method takes a look at all the created Controllers and picks one that gets executed and invokes the correct method. + /// + /// Passed method from the controller invoker + /// Passed enum from the controller invoker + /// + /// + /// + private static void InvokeEventController(ENUM PassedEnum, string[] ids, object e) + { + var type = typeof(CLASS); + var methods = type.GetMethods(); + EventControllerAttribute? attribute; + foreach (var method in methods) + { + attribute = method.GetCustomAttribute>(); + if (attribute is not null && EqualityComparer.Default.Equals(attribute.Event, PassedEnum)) + method.Invoke(null, new object[] { ids, e }); + } + } + /// + /// Method To be placed in a separate Class that will be passed into the + /// For the EventControllerAttribute you have to pass the enum type e.g. of all categories and pass the enum value for a specific category. Finally you have to execute the controller and use the category's enum for the generic. + /// + /// The enum of the category + /// The given Ids without category enum. + /// The namespace with all the required events. E.g. Project_Sipster.DiscordBarista.Function.Partnership.Events + /// The passed event from the - just needs to be put it as a param and passed over to the controller. + public static void ExecuteController(string[] ids, string nspace, object e) + { + var category = Converter.InteractionFormatter.GetFirstEnumFromId(ref ids); + var q = (from t in Assembly.GetExecutingAssembly().GetTypes() + where t.IsClass && t.Namespace == nspace + select t).ToList(); + List methods = new(); + foreach (var type in q) + { + methods.AddRange(type.GetMethods()); + } + EventControllerAttribute? attribute; + foreach (var method in methods) + { + attribute = method.GetCustomAttribute>(); + if (attribute is not null && EqualityComparer.Default.Equals(attribute.Event, category)) + method.Invoke(null, new object[] { ids, e }); + } + } + } +} diff --git a/Discord/Converter/InteractionFormatter.cs b/Discord/Converter/InteractionFormatter.cs new file mode 100644 index 0000000..dbdb647 --- /dev/null +++ b/Discord/Converter/InteractionFormatter.cs @@ -0,0 +1,64 @@ +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 +} diff --git a/Discord/Exception/MismatchedEnumLengthsException.cs b/Discord/Exception/MismatchedEnumLengthsException.cs new file mode 100644 index 0000000..b726372 --- /dev/null +++ b/Discord/Exception/MismatchedEnumLengthsException.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Discord.Exception +{ + public class MismatchedEnumLengthsException : System.Exception + { + public MismatchedEnumLengthsException() : base("Length of values and enums are different. You are required to pass as many values as there are enums.") + { + } + } +}