Ejemplo n.º 1
0
        public void Initialize()
        {
            connectedServers = new List <Server>();
            eventQueue       = new LinkedList <string>();
            dc                  = new DiscordClient();
            dc.Log.Message     += onRequestLog;
            dc.MessageReceived += onMessageReceived;
            dc.ServerAvailable += (s, e) => { dc.Log.Info("Manager", "Connected to server " + e.Server.Id, null); connectedServers.Add(e.Server); };

            try
            {
                dc.Log.Info("Manager", "Attempting to load configuration file", null);
                cfg = Configuration.loadConfig("IW4MAdmin.Discord.cfg");
            }

            catch (ConfigException e)
            {
                dc.Log.Info("Manager", "No usable configuration found", e);
                cfg = Configuration.createConfig();
                try
                {
                    Configuration.saveConfig(cfg);
                    dc.Log.Info("Manager", "Saved new bot configuration", null);
                }

                catch (ConfigException ce)
                {
                    dc.Log.Error("Manager", "Could not save new configuration file", ce);
                    dc.Log.Error(ce.Message, ce);
                }
            }

            dc.UsingCommands(c => { c.PrefixChar = cfg.CommandPrefix; c.HelpMode = HelpMode.Public; })
            .UsingPermissionLevels((u, c) => { return((int)PermissionHelper.getPermissions(u, c)); })
            .UsingModules();

            dc.Log.Info("Manager", "Adding command modules", null);
            dc.AddModule <IW4MAdminModule>("IW4MAdmin", ModuleFilter.None);

            dc.Log.Info("Manager", "Initializing connection to IW4MAdmin API", null);
            rc = RestConnection.Initialize(new Uri(cfg.IW4MAdminURI + ':' + cfg.IW4MAdminPort + "/api/events"));

            try
            {
                Task.Run(queryAPI);
                dc.ExecuteAndWait(Connect);
            }

            catch (Exception e)
            {
                dc.Log.Error("Manager", "Could not connect!", e);
            }
        }