示例#1
0
        public void RegisterAttributeCommands(Assembly assembly = null)
        {
            Bot b = Bot.Instance;

            if (assembly == null)
            {
                assembly = Assembly.GetEntryAssembly();
            }

            var modules = assembly.GetTypes()
                          .Where(m => m.GetCustomAttributes <ModuleAttribute>().Count() > 0)
                          .ToArray();

            foreach (var m in modules)
            {
                object instance = null;

                Module newModule = new Module(instance);

                try
                {
                    instance = Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName), newModule, b);
                }
                catch
                {
                    try
                    {
                        instance = Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName), newModule);
                    }
                    catch
                    {
                        instance = Activator.CreateInstance(Type.GetType(m.AssemblyQualifiedName));
                    }
                }

                newModule.SetInstance(instance);

                ModuleAttribute mAttrib = m.GetCustomAttribute <ModuleAttribute>();
                newModule.Name          = mAttrib.module.Name.ToLower();
                newModule.Nsfw          = mAttrib.module.Nsfw;
                newModule.CanBeDisabled = mAttrib.module.CanBeDisabled;

                var methods = m.GetMethods()
                              .Where(t => t.GetCustomAttributes <CommandAttribute>().Count() > 0)
                              .ToArray();

                foreach (var x in methods)
                {
                    CommandEvent     newEvent         = new CommandEvent();
                    CommandAttribute commandAttribute = x.GetCustomAttribute <CommandAttribute>();

                    newEvent = commandAttribute.command;
                    newEvent.ProcessCommand = async(context) => await(Task) x.Invoke(instance, new object[] { context });
                    newEvent.Module         = newModule;

                    CommandEvent foundCommand = newModule.Events.Find(c => c.Name == newEvent.Name);

                    if (foundCommand != null)
                    {
                        foundCommand.Default(newEvent.ProcessCommand);
                    }
                    else
                    {
                        newModule.AddCommand(newEvent);
                    }

                    foreach (var a in newEvent.Aliases)
                    {
                        commandCache.Add(a.ToLower(), newEvent);
                    }
                    commandCache.Add(newEvent.Name.ToLower(), newEvent);
                }

                var services = m.GetProperties().Where(x => x.GetCustomAttributes <ServiceAttribute>().Count() > 0).ToArray();

                foreach (var s in services)
                {
                    BaseService service = Activator.CreateInstance(s.PropertyType, true) as BaseService;
                    var         attrib  = s.GetCustomAttribute <ServiceAttribute>();

                    service.Name = attrib.Name;
                    newModule.Services.Add(service);
                }

                OnModuleLoaded?.Invoke(newModule);

                modulesLoaded.Add(newModule);
            }
        }
示例#2
0
 /// <summary>
 ///     Invokes the OnModuleLoaded event with the given loaded module instance.
 /// </summary>
 /// <param name="moduleInstance"></param>
 public static void OnOnModuleLoaded(object moduleInstance)
 {
     OnModuleLoaded?.Invoke(moduleInstance);
 }