Exemplo n.º 1
0
        public async Task LoadDiscord(MikiApp app)
        {
            var cache   = app.GetService <IExtendedCacheClient>();
            var gateway = app.GetService <IGateway>();

            new BasicCacheStage().Initialize(gateway, cache);

            var         config      = app.GetService <ConfigurationManager>();
            EventSystem eventSystem = app.GetService <EventSystem>();
            {
                app.Discord.MessageCreate += eventSystem.OnMessageReceivedAsync;

                eventSystem.OnError += async(ex, context) =>
                {
                    if (ex is LocalizedException botEx)
                    {
                        if (context is ICommandContext m)
                        {
                            await Utils.ErrorEmbedResource(m, botEx.LocaleResource)
                            .ToEmbed().QueueToChannelAsync(m.Channel);
                        }
                    }
                    else
                    {
                        Log.Error(ex);
                        await app.GetService <RavenClient>().CaptureAsync(new SentryEvent(ex));
                    }
                };

                eventSystem.MessageFilter.AddFilter(new BotFilter());
                eventSystem.MessageFilter.AddFilter(new UserFilter());

                var commandMap = new Framework.Events.CommandMap();
                commandMap.OnModuleLoaded += (module) =>
                {
                    config.RegisterType(module.GetReflectedInstance().GetType(), module.GetReflectedInstance());
                };

                eventSystem.AddMessageTrigger(new PrefixTrigger(">", true, true));
                eventSystem.AddMessageTrigger(new PrefixTrigger("miki.", false));
                eventSystem.AddMessageTrigger(new MentionTrigger());

                var handler = new SimpleCommandHandler(commandMap);
                handler.OnMessageProcessed += async(cmd, msg, time) =>
                {
                    await Task.Yield();

                    Log.Message($"{cmd.Name} processed in {time}ms");
                };

                eventSystem.AddCommandHandler(handler);

                commandMap.RegisterAttributeCommands();
                commandMap.Install(eventSystem);
            }

            string configFile = Environment.CurrentDirectory + Config.MikiConfigurationFile;

            if (File.Exists(configFile))
            {
                await config.ImportAsync(
                    new JsonSerializationProvider(),
                    configFile
                    );
            }

            await config.ExportAsync(
                new JsonSerializationProvider(),
                configFile
                );

            app.Discord.MessageCreate += Bot_MessageReceived;

            app.Discord.GuildJoin  += Client_JoinedGuild;
            app.Discord.GuildLeave += Client_LeftGuild;
            app.Discord.UserUpdate += Client_UserUpdated;

            await gateway.StartAsync();
        }