Ejemplo n.º 1
0
        public void LoadDiscord()
        {
            WebhookManager.Listen("webhooks");
            WebhookManager.OnEvent += (eventArgs) => new Task(() => Console.WriteLine("[webhook] " + eventArgs.auth_code));

            bot = new Bot(Global.Config.AmountShards, new DiscordSocketConfig()
            {
                ShardId           = Global.Config.ShardId,
                TotalShards       = Global.Config.ShardCount,
                ConnectionTimeout = 100000,
                LargeThreshold    = 250,
            }, new ClientInformation()
            {
                Name       = "Miki",
                Version    = "0.6.2",
                ShardCount = Global.Config.ShardCount,
                DatabaseConnectionString = Global.Config.ConnString,
            });

            EventSystem eventSystem = new EventSystem(new EventSystemConfig()
            {
                Developers        = Global.Config.DeveloperIds,
                ErrorEmbedBuilder = new EmbedBuilder().WithTitle($"🚫 Something went wrong!").WithColor(new Color(1.0f, 0.0f, 0.0f))
            });

            bot.Attach(eventSystem);

            var commandMap = Framework.Events.CommandMap.CreateFromAssembly();

            commandMap.Install(eventSystem, bot);

            var handler = new SimpleCommandHandler(commandMap);

            handler.AddPrefix(">", true);
            handler.AddPrefix("miki.");

            var sessionHandler = new SessionBasedCommandHandler();
            var messageHandler = new MessageListener();

            eventSystem.AddCommandHandler(sessionHandler);
            eventSystem.AddCommandHandler(messageHandler);
            eventSystem.AddCommandHandler(handler);

            if (!string.IsNullOrWhiteSpace(Global.Config.SharpRavenKey))
            {
                Global.ravenClient = new SharpRaven.RavenClient(Global.Config.SharpRavenKey);
            }

            bot.Client.MessageReceived += Bot_MessageReceived;

            bot.Client.JoinedGuild += Client_JoinedGuild;
            bot.Client.LeftGuild   += Client_LeftGuild;
            bot.Client.UserUpdated += Client_UserUpdated;

            bot.Client.ShardConnected    += Bot_OnShardConnect;
            bot.Client.ShardDisconnected += Bot_OnShardDisconnect;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The program runs all discord services and loads all the data here.
        /// </summary>
        public void LoadDiscord()
        {
            Global.redisClient = new StackExchangeRedisCacheClient(new ProtobufSerializer(), Global.Config.RedisConnectionString);

            if (!Global.Config.IsPatreonBot)
            {
                WebhookManager.Listen("webhook");

                WebhookManager.OnEvent += async(eventArgs) =>
                {
                    Console.WriteLine("[webhook] " + eventArgs.auth_code);
                };
            }

            bot = new Bot(new ClientInformation()
            {
                Name       = "Miki",
                Version    = "0.6",
                Token      = Global.Config.Token,
                ShardCount = Global.Config.ShardCount,
                DatabaseConnectionString = Global.Config.ConnString
            });

            var eventSystem = EventSystem.Start(bot);

            if (!string.IsNullOrWhiteSpace(Global.Config.SharpRavenKey))
            {
                Global.ravenClient = new SharpRaven.RavenClient(Global.Config.SharpRavenKey);
            }

            if (!string.IsNullOrWhiteSpace(Global.Config.DatadogKey))
            {
                var dogstatsdConfig = new StatsdConfig
                {
                    StatsdServerName = Global.Config.DatadogHost,
                    StatsdPort       = 8125,
                    Prefix           = "miki"
                };
                DogStatsd.Configure(dogstatsdConfig);
            }

            eventSystem.AddCommandDoneEvent(x =>
            {
                x.Name         = "datadog-command-done";
                x.processEvent = async(msg, cmd, success, t) =>
                {
                    if (!success)
                    {
                        DogStatsd.Counter("commands.error.rate", 1);
                    }

                    DogStatsd.Counter("commands.count", 1, 1, new[]
                                      { $"commandtype:{cmd.Module.Name.ToLowerInvariant()}", $"commandname:{cmd.Name.ToLowerInvariant()}" });
                    DogStatsd.Histogram("commands.time", t, 0.1, new[]
                                        { $"commandtype:{cmd.Module.Name.ToLowerInvariant()}", $"commandname:{cmd.Name.ToLowerInvariant()}" });
                };
            });

            EventSystem.Instance.RegisterPrefixInstance(">")
            .RegisterAsDefault();

            eventSystem.RegisterPrefixInstance("miki.", false);

            bot.MessageReceived += Bot_MessageReceived;

            bot.OnError = async(ex) => Log.Message(ex.ToString());
            eventSystem.AddDeveloper(121919449996460033);

            foreach (ulong l in Global.Config.DeveloperIds)
            {
                eventSystem.AddDeveloper(l);
            }

            bot.Client.JoinedGuild += Client_JoinedGuild;
            bot.Client.LeftGuild   += Client_LeftGuild;

            bot.ShardConnect    += Bot_OnShardConnect;
            bot.ShardDisconnect += Bot_OnShardDisconnect;
        }