Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronous main entry point
        /// </summary>
        /// <param name="args">Command line arguments</param>
        /// <returns></returns>
        static async Task MainAsync(string[] args)
        {
            // Parse command line arguments if given
            var arguments      = CommandLine.ParseArgs(new string[] { "--", "-" }, args);
            var configFilePath = string.Empty;
            var managerName    = string.Empty;
            // Loop through the parsed command line arguments and set the key values associated with each argument provided
            var keys = arguments.Keys.ToList();

            for (var i = 0; i < keys.Count; i++)
            {
                var key = keys[i];
                switch (key.ToLower())
                {
                case "config":
                case "c":
                    configFilePath = arguments.ContainsKey(key) ? arguments[key]?.ToString() : Strings.ConfigFileName;
                    break;

                case "name":
                case "n":
                    managerName = arguments.ContainsKey(key) ? arguments[key]?.ToString() : "Default";
                    break;
                }
            }

            configFilePath = Path.Combine(Environment.CurrentDirectory, string.IsNullOrEmpty(configFilePath) ? Strings.ConfigFileName : configFilePath);
            ManagerName    = managerName;
            var logger = EventLogger.GetLogger(managerName);

            logger.Info(Strings.BannerAsciiText);
            logger.Info($"Version: {Strings.Version}");
            logger.Info($".NET Runtime Version: {System.Reflection.Assembly.GetExecutingAssembly().ImageRuntimeVersion}\n");
            var whConfig = Configuration.WhConfig.Load(configFilePath);

            if (whConfig == null)
            {
                logger.Error($"Failed to load config {configFilePath}.");
                return;
            }
            whConfig.FileName = configFilePath;

            LogLevel = whConfig.LogLevel;

            // Start bot
            var bot = new Bot(whConfig);
            await bot.Start();

            // Keep the process alive
            Process.GetCurrentProcess().WaitForExit();
        }
Ejemplo n.º 2
0
        public Bot(Config config)
        {
            var name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName;

            _logger = EventLogger.GetLogger(name);
            _logger.Trace($"Bot::Bot [GuildId={config.GuildId}, OwnerId={config.OwnerId}]");

            _config = config;

#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
            AppDomain.CurrentDomain.UnhandledException += async(sender, e) =>
#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
            {
                _logger.Debug("Unhandled exception caught.");
                _logger.Error((Exception)e.ExceptionObject);

                if (e.IsTerminating)
                {
                    if (_client != null)
                    {
                        var owner = await _client.GetUserAsync(_config.OwnerId);

                        if (owner == null)
                        {
                            _logger.Warn($"Failed to get owner from id {_config.OwnerId}.");
                            return;
                        }

                        await _client.SendDirectMessage(owner, Strings.CrashMessage, null);
                    }
                }
            };

            _client = new DiscordClient(new DiscordConfiguration
            {
                AutomaticGuildSync = true,
                AutoReconnect      = true,
                EnableCompression  = true,
                Token                 = _config.Token,
                TokenType             = TokenType.Bot,
                UseInternalLogHandler = true
            });
            _client.Ready += Client_Ready;
            _client.MessageReactionAdded           += Client_MessageReactionAdded;
            _client.ClientErrored                  += Client_ClientErrored;
            _client.DebugLogger.LogMessageReceived += DebugLogger_LogMessageReceived;

            _lobbyManager = new RaidLobbyManager(_client, _config);
        }