示例#1
0
        public async Task InstallAsync(object bot)
        {
            Bot b = (Bot)bot;

            Name = Name.ToLower();

            if (MessageRecieved != null)
            {
                b.MessageReceived += Module_MessageReceived;
            }

            if (UserUpdated != null)
            {
                b.UserUpdated += Module_UserUpdated;
            }

            if (UserJoinGuild != null)
            {
                b.UserJoin += Module_UserJoined;
            }

            if (UserLeaveGuild != null)
            {
                b.UserLeft += Module_UserLeft;
            }

            if (JoinedGuild != null)
            {
                b.GuildJoin += Module_JoinedGuild;
            }

            if (LeftGuild != null)
            {
                b.GuildLeave += Module_LeftGuild;
            }

            EventSystem = b.Events;

            b.Events.CommandHandler.Modules.Add(Name, this);

            foreach (ICommandEvent e in Events)
            {
                RuntimeCommandEvent ev = new RuntimeCommandEvent(e)
                {
                    eventSystem = b.Events,
                    Module      = this
                };
                EventSystem.CommandHandler.AddCommand(ev);
            }

            isInstalled = true;

            await Task.CompletedTask;
        }
示例#2
0
 public async Task OnCommandDone(IDiscordMessage e, RuntimeCommandEvent commandEvent)
 {
     foreach (CommandDoneEvent ev in events.CommandDoneEvents.Values)
     {
         try
         {
             await ev.processEvent(e, commandEvent);
         }
         catch (Exception ex)
         {
             Log.ErrorAt($"commanddone@{ev.Name}", ex.Message);
         }
     }
 }
示例#3
0
文件: Module.cs 项目: amishshah/IA
        public async Task UninstallAsync(object bot)
        {
            Bot b = (Bot)bot;

            if (!isInstalled)
            {
                return;
            }

            b.Events.Modules.Remove(Name);

            foreach (ICommandEvent e in Events)
            {
                ICommandEvent ev = new RuntimeCommandEvent(e);
                EventSystem.events.CommandEvents.Remove(ev.Name);
            }

            if (MessageRecieved != null)
            {
                b.Client.MessageReceived -= Module_MessageRecieved;
            }

            if (UserUpdated != null)
            {
                b.Client.UserUpdated -= Module_UserUpdated;
            }

            if (UserJoinGuild != null)
            {
                b.Client.UserJoined -= Module_UserJoined;
            }

            if (UserLeaveGuild != null)
            {
                b.Client.UserLeft -= Module_UserLeft;
            }

            if (JoinedGuild != null)
            {
                b.Client.JoinedGuild -= Module_JoinedGuild;
            }

            if (LeftGuild != null)
            {
                b.Client.LeftGuild -= Module_LeftGuild;
            }

            isInstalled = false;
            await Task.CompletedTask;
        }
示例#4
0
        public void AddMentionEvent(Action <RuntimeCommandEvent> info)
        {
            RuntimeCommandEvent newEvent = new RuntimeCommandEvent();

            info.Invoke(newEvent);
            newEvent.eventSystem = this;
            if (newEvent.Aliases.Length > 0)
            {
                foreach (string s in newEvent.Aliases)
                {
                    aliases.Add(s, newEvent.Name.ToLower());
                }
            }
            events.MentionEvents.Add(newEvent.Name.ToLower(), newEvent);

            Sql.TryCreateTable("event(name VARCHAR(255), id BIGINT, enabled BOOLEAN)");
        }
示例#5
0
        public void RegisterAttributeCommands()
        {
            Assembly assembly = Assembly.GetEntryAssembly();

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

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

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

                newModule.EventSystem = this;

                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)
                {
                    RuntimeCommandEvent newEvent         = new RuntimeCommandEvent();
                    CommandAttribute    commandAttribute = x.GetCustomAttribute <CommandAttribute>();

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

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

                    if (foundCommand != null)
                    {
                        if (commandAttribute.on != "")
                        {
                            foundCommand.On(commandAttribute.On, newEvent.ProcessCommand);
                        }
                        else
                        {
                            foundCommand.Default(newEvent.ProcessCommand);
                        }
                    }
                    else
                    {
                        newModule.AddCommand(newEvent);
                    }
                }

                newModule.InstallAsync(bot).GetAwaiter().GetResult();
            }
        }