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 }); } } } }