コード例 #1
0
ファイル: CommandMap.cs プロジェクト: Xetera/Miki.Framework
 public void AddCommand(CommandEvent command)
 {
     foreach (string a in command.Aliases)
     {
         commandCache.Add(a.ToLower(), command);
     }
     commandCache.Add(command.Name.ToLower(), command);
 }
コード例 #2
0
ファイル: CommandMap.cs プロジェクト: Xetera/Miki.Framework
 public void RemoveCommand(CommandEvent command)
 {
     commandCache.Remove(command.Name.ToLower());
     foreach (var c in command.Aliases)
     {
         commandCache.Remove(c.ToLower());
     }
 }
コード例 #3
0
        public override async Task CheckAsync(MessageContext context)
        {
            EventContext e = new EventContext();

            e.commandHandler = this;
            e.message        = context.message;
            e.EventSystem    = context.eventSystem;

            Stopwatch sw = Stopwatch.StartNew();

            e.Channel = await context.message.GetChannelAsync();

            if (e.Channel is IDiscordGuildChannel guildChannel)
            {
                e.Guild = await guildChannel.GetGuildAsync();
            }

            foreach (PrefixInstance prefix in Prefixes.Values)
            {
                string identifier = prefix.DefaultValue;

                if (e.Guild != null)
                {
                    identifier = await prefix.GetForGuildAsync(Bot.Instance.CachePool.Get, e.Guild.Id);
                }

                if (!context.message.Content.StartsWith(identifier))
                {
                    continue;
                }

                e.Prefix = prefix;

                string command = Regex.Replace(context.message.Content, @"\r\n?|\n", "")
                                 .Substring(identifier.Length)
                                 .Split(' ')
                                 .First()
                                 .ToLower();

                CommandEvent eventInstance = map.GetCommandEvent(command);

                if (eventInstance == null)
                {
                    return;
                }

                if ((await GetUserAccessibility(context.message, e.Channel)) >= eventInstance.Accessibility)
                {
                    if (await eventInstance.IsEnabled(Bot.Instance.CachePool.Get, (await context.message.GetChannelAsync()).Id))
                    {
                        await eventInstance.Check(e, identifier);
                        await OnMessageProcessed(eventInstance, context.message, sw.ElapsedMilliseconds);
                    }
                }
            }
        }
コード例 #4
0
ファイル: CommandMap.cs プロジェクト: Xetera/Miki.Framework
        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);
            }
        }
コード例 #5
0
ファイル: Module.cs プロジェクト: hlinkad/Miki.Framework
 public Module AddCommand(CommandEvent command)
 {
     Events.Add(command);
     return(this);
 }
コード例 #6
0
        public override async Task CheckAsync(MessageContext context)
        {
            try
            {
                Stopwatch sw = Stopwatch.StartNew();

                await base.CheckAsync(context);

                foreach (PrefixInstance prefix in Prefixes.Values)
                {
                    string identifier = prefix.DefaultValue;

                    Log.Message($"checking channel with id {context.message.ChannelId}...");

                    context.channel = await context.message.GetChannelAsync();

                    Log.Message("channel ok!");

                    if (context.channel is IDiscordGuildChannel guildChannel)
                    {
                        identifier = await prefix.GetForGuildAsync(guildChannel.GuildId);
                    }

                    if (!context.message.Content.StartsWith(identifier))
                    {
                        continue;
                    }

                    Log.Message("prefix ok!");

                    string command = Regex.Replace(context.message.Content, @"\r\n?|\n", "")
                                     .Substring(identifier.Length)
                                     .Split(' ')
                                     .First();

                    CommandEvent eventInstance = map.GetCommandEvent(command);

                    if (eventInstance == null)
                    {
                        return;
                    }

                    Log.Message($"command '{eventInstance.Name}' found!");

                    if ((await GetUserAccessibility(context.message, context.channel)) >= eventInstance.Accessibility)
                    {
                        Log.Message("permissions ok!");

                        if (await eventInstance.IsEnabled((await context.message.GetChannelAsync()).Id))
                        {
                            Log.Message("command enabled!");

                            await eventInstance.Check(context, identifier);

                            Log.Message("command success!");

                            await OnMessageProcessed(eventInstance, context.message, sw.ElapsedMilliseconds);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }