示例#1
0
        private void Log(params Message[] messages)
        {
            var now = DateTime.UtcNow;

            if (!CakeJson.GetConfig().LogEnabled)
            {
                return;
            }
            try
            {
                foreach (var message in messages)
                {
                    if (message.Type == Type.Debug)
                    {
                        return;
                    }
                    var typeName = TypeHelper.GetName(message.Type);
                    Console.WriteLineFormatted(FinalMessageFormat,
                                               message.DefaultColor,
                                               message.GetDefaultFormatter(now));
                }
            }
            catch (Exception exception) when(
                exception is IOException ||
                exception is ObjectDisposedException)
            {
                var exceptionMessage = new ExceptionMessage(exception);

                Console.WriteLineFormatted(FinalMessageFormat,
                                           DefaultColor,
                                           exceptionMessage.GetDefaultFormatter(now));
            }
        }
示例#2
0
        public async Task StartAsync()
        {
            while (true)
            {
                _logger.Log(Type.Info, "Starting");
                SetupBot();
                _cakeConfiguration.BotKey = CakeJson.GetConfig().BotKey;

                try
                {
                    _logger.Log(Type.Info, "Connecting");
                    await _client.LoginAsync(TokenType.Bot, _cakeConfiguration.BotKey);

                    await _client.StartAsync().ConfigureAwait(false);

                    await _client.SetStatusAsync(UserStatus.Online);

                    await _commandHandler.InitializeAsync();

                    Running = true;
                    _logger.Log(Type.Info, "Connected");
                    break;
                }
                catch (Exception e)
                {
                    _logger.LogException(e);
                    _logger.Log(Type.Error, "Failed to connect");
                }
            }

            while (Running)
            {
                await Task.Delay(RunningInterval).ConfigureAwait(false);
            }
        }