Ejemplo n.º 1
0
 private static IHostBuilder CreateHostBuilder(string[] args) =>
 Host.CreateDefaultBuilder(args)
 // TODO: move to service rather than being across all services by default.
 .ConfigureDiscordBot(() => new DiscordSocketConfig
 {
     LogLevel            = EnvironmentsHelper.IsDevelopment() ? LogSeverity.Verbose : LogSeverity.Info,
     MessageCacheSize    = 40,
     HandlerTimeout      = 2000,
     AlwaysDownloadUsers = true
 })
 .ConfigureLogging(builder =>
 {
     builder.ClearProviders();
     builder.AddConsole();
 })
 .ConfigureServices(builder =>
 {
     builder.AddOptions();
 })
 .UseServiceProviderFactory(new AutofacServiceProviderFactory(builder =>
 {
     var ownedAssemblies = AppDomain.CurrentDomain.GetAssemblies()
                           .Where(a => a.GetName().FullName.Contains(nameof(YosherBot)))
                           .ToArray();
     builder.RegisterAssemblyModules(ownedAssemblies);
 }));
Ejemplo n.º 2
0
        // TODO: move to handler class
        private async Task HandleOnMessageReceivedAsync(SocketMessage socketMessage)
        {
            // ReSharper disable once UseNegatedPatternMatching
            var message = socketMessage as SocketUserMessage;

            if (message == null)
            {
                return;
            }

            var argPos = 0;
            var prefix = _options.Prefix;

            if (!message.HasStringPrefix(prefix, ref argPos) || message.Author.IsBot)
            {
                return;
            }

            if (EnvironmentsHelper.IsProduction() && socketMessage.Channel.Name != "commands-use")
            {
                return;
            }

            var context = new SocketCommandContext(_client, message);

            await _commandService.ExecuteAsync(context, argPos, _serviceProvider);
        }
Ejemplo n.º 3
0
        public void ShouldProductionBeFalse_WhenEnvvarIsUnknown()
        {
            TestHelper.SetEnvironmentName("abc");

            var result = EnvironmentsHelper.IsProduction();

            Assert.IsFalse(result);
        }
Ejemplo n.º 4
0
        public void ShouldDevelopmentBeFalse_WhenEnvvarIsUnknown()
        {
            TestHelper.SetEnvironmentName("abc");

            var result = EnvironmentsHelper.IsDevelopment();

            Assert.IsFalse(result);
        }
Ejemplo n.º 5
0
        public void ShouldProductionBeTrue_WhenEnvvarIsProduction()
        {
            TestHelper.SetEnvironmentName("production");

            var result = EnvironmentsHelper.IsProduction();

            Assert.IsTrue(result);
        }
Ejemplo n.º 6
0
        public void ShouldDevelopmentBeFalse_WhenEnvvarIsProduction()
        {
            TestHelper.SetEnvironmentName("production");

            var result = EnvironmentsHelper.IsDevelopment();

            Assert.IsFalse(result);
        }
Ejemplo n.º 7
0
        public void ShouldDevelopmentBeTrue_WhenEnvvarIsUnset()
        {
            TestHelper.SetEnvironmentName(null);

            var result = EnvironmentsHelper.IsDevelopment();

            Assert.IsTrue(result);
        }
Ejemplo n.º 8
0
        public Task HandleLogEventAsync(LogMessage logMessage)
        {
            var isDevelopment = EnvironmentsHelper.IsDevelopment();

            switch (logMessage.Severity)
            {
            case LogSeverity.Critical:
                if (isDevelopment)
                {
                    _logger.LogCritical("Discord.NET ({Source}) - {Message}", logMessage.Source, logMessage.Message);
                }
                else
                {
                    _logger.LogCritical("Discord.NET - {Message}", logMessage.Message);
                }
                if (logMessage.Exception != null)
                {
                    _logger.LogError("Exception: {Exception}", logMessage.Exception);
                }
                break;

            case LogSeverity.Error:
                if (isDevelopment)
                {
                    _logger.LogError("Discord.NET ({Source}) - {Message}", logMessage.Source, logMessage.Message);
                }
                else
                {
                    _logger.LogError("Discord.NET - {Message}", logMessage.Message);
                }
                if (logMessage.Exception != null)
                {
                    _logger.LogError("Exception: {Exception}", logMessage.Exception);
                }
                break;

            case LogSeverity.Warning:
                if (isDevelopment)
                {
                    _logger.LogWarning("Discord.NET ({Source}) - {Message}", logMessage.Source, logMessage.Message);
                }
                else
                {
                    _logger.LogWarning("Discord.NET - {Message}", logMessage.Message);
                }
                if (logMessage.Exception != null)
                {
                    _logger.LogError("Exception: {Exception}", logMessage.Exception);
                }
                break;

            case LogSeverity.Info:
                if (isDevelopment)
                {
                    _logger.LogInformation("Discord.NET ({Source}) - {Message}", logMessage.Source, logMessage.Message);
                }
                else
                {
                    _logger.LogInformation("Discord.NET - {Message}", logMessage.Message);
                }
                break;

            case LogSeverity.Verbose:
            case LogSeverity.Debug:
                if (isDevelopment)
                {
                    _logger.LogDebug("Discord.NET ({Source}) - {Message}", logMessage.Source, logMessage.Message);
                }
                break;

            default:
                _logger.LogInformation("Discord.NET ({Source}) - {Message}", logMessage.Source, logMessage.Message);
                break;
            }

            return(Task.CompletedTask);
        }