public UserInfoCommandHandler(ModixContext modixContext, IDiscordClient discordClient, IAutoRemoveMessageService autoRemoveMessageService, IImageService imageService, ILogger <UserInfoCommandHandler> logger, IMessageRepository messageRepository, IOptions <ModixConfig> modixOptions) { _modixContext = modixContext; _discordClient = discordClient; _autoRemoveMessageService = autoRemoveMessageService; _imageService = imageService; _logger = logger; _messageRepository = messageRepository; _modixConfig = modixOptions.Value; }
private static ModixConfig LoadConfig() { var config = new ModixConfig { DiscordToken = Environment.GetEnvironmentVariable("DiscordToken"), ReplToken = Environment.GetEnvironmentVariable("ReplToken"), StackoverflowToken = Environment.GetEnvironmentVariable("StackoverflowToken"), PostgreConnectionString = Environment.GetEnvironmentVariable("MODIX_DB_CONNECTION"), DiscordClientId = Environment.GetEnvironmentVariable("DiscordClientId"), DiscordClientSecret = Environment.GetEnvironmentVariable("DiscordClientSecret") }; if (string.IsNullOrWhiteSpace(config.DiscordToken)) { Log.Fatal("The discord token was not set - this is fatal! Check your envvars."); } if (string.IsNullOrWhiteSpace(config.DiscordClientId) || string.IsNullOrWhiteSpace(config.DiscordClientSecret)) { Log.Warning("The discord client id and/or client secret were not set. These are required for Web API functionality - " + "if you need that, set your envvars, and make sure to configure redirect URIs"); } if (int.TryParse(Environment.GetEnvironmentVariable("DiscordMessageCacheSize"), out var cacheSize)) { config.MessageCacheSize = cacheSize; } else { config.MessageCacheSize = 0; Log.Information("The message cache size was not set. Defaulting to 0."); } var id = Environment.GetEnvironmentVariable("log_webhook_id"); if (!string.IsNullOrWhiteSpace(id)) { config.WebhookId = ulong.Parse(id); config.WebhookToken = Environment.GetEnvironmentVariable("log_webhook_token"); } var sentryToken = Environment.GetEnvironmentVariable("SentryToken"); if (!string.IsNullOrWhiteSpace(sentryToken)) { config.SentryToken = sentryToken; } return(config); }
public ModixBot( DiscordSocketClient discordClient, ModixConfig modixConfig, CommandService commandService, IServiceProvider serviceProvider, ILogger <ModixBot> logger) { _client = discordClient ?? throw new ArgumentNullException(nameof(discordClient)); _config = modixConfig ?? throw new ArgumentNullException(nameof(modixConfig)); _commands = commandService ?? throw new ArgumentNullException(nameof(commandService)); _provider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); Log = logger ?? throw new ArgumentNullException(nameof(logger)); }
public void LoadConfig() { _config = new ModixConfig { DiscordToken = Environment.GetEnvironmentVariable("Token"), ReplToken = Environment.GetEnvironmentVariable("ReplToken"), StackoverflowToken = Environment.GetEnvironmentVariable("StackoverflowToken"), PostgreConnectionString = Environment.GetEnvironmentVariable("MODIX_DB_CONNECTION"), }; var id = Environment.GetEnvironmentVariable("log_webhook_id"); if (!string.IsNullOrWhiteSpace(id)) { _config.WebhookId = ulong.Parse(id); _config.WebhookToken = Environment.GetEnvironmentVariable("log_webhook_token"); } }
public ModixBot( DiscordSocketClient discordClient, DiscordRestClient restClient, IOptions <ModixConfig> modixConfig, CommandService commandService, DiscordSerilogAdapter serilogAdapter, IApplicationLifetime applicationLifetime, IServiceProvider serviceProvider, ILogger <ModixBot> logger, CommandErrorHandler commandErrorHandler) { _client = discordClient ?? throw new ArgumentNullException(nameof(discordClient)); _restClient = restClient ?? throw new ArgumentNullException(nameof(restClient)); _config = modixConfig?.Value ?? throw new ArgumentNullException(nameof(modixConfig)); _commands = commandService ?? throw new ArgumentNullException(nameof(commandService)); _provider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); _serilogAdapter = serilogAdapter ?? throw new ArgumentNullException(nameof(serilogAdapter)); _applicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime)); Log = logger ?? throw new ArgumentNullException(nameof(logger)); _commandErrorHandler = commandErrorHandler; }
public UserInfoModule( ILogger <UserInfoModule> logger, IUserService userService, IModerationService moderationService, IAuthorizationService authorizationService, IMessageRepository messageRepository, IEmojiRepository emojiRepository, IPromotionsService promotionsService, IImageService imageService, IOptions <ModixConfig> config, IAutoRemoveMessageService autoRemoveMessageService) { _log = logger ?? new NullLogger <UserInfoModule>(); _userService = userService; _moderationService = moderationService; _authorizationService = authorizationService; _messageRepository = messageRepository; _emojiRepository = emojiRepository; _promotionsService = promotionsService; _imageService = imageService; _config = config.Value; _autoRemoveMessageService = autoRemoveMessageService; }
public ModixBot( DiscordSocketClient discordClient, DiscordRestClient restClient, IOptions <ModixConfig> modixConfig, CommandService commandService, DiscordSerilogAdapter serilogAdapter, IApplicationLifetime applicationLifetime, IServiceProvider serviceProvider, ILogger <ModixBot> logger, IHostingEnvironment env, IDogStatsd stats) { _client = discordClient ?? throw new ArgumentNullException(nameof(discordClient)); _restClient = restClient ?? throw new ArgumentNullException(nameof(restClient)); _config = modixConfig?.Value ?? throw new ArgumentNullException(nameof(modixConfig)); _commands = commandService ?? throw new ArgumentNullException(nameof(commandService)); _provider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); _serilogAdapter = serilogAdapter ?? throw new ArgumentNullException(nameof(serilogAdapter)); _applicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime)); Log = logger ?? throw new ArgumentNullException(nameof(logger)); _env = env; _stats = stats; }
public static int Main(string[] args) { const string DEVELOPMENT_ENVIRONMENT_VARIABLE = "ASPNETCORE_ENVIRONMENT"; const string DEVELOPMENT_ENVIRONMENT_KEY = "Development"; var environment = Environment.GetEnvironmentVariable(DEVELOPMENT_ENVIRONMENT_VARIABLE); var configBuilder = new ConfigurationBuilder() .AddEnvironmentVariables("MODIX_") .AddJsonFile("developmentSettings.json", optional: true, reloadOnChange: false) .AddKeyPerFile("/run/secrets", true); if (environment is DEVELOPMENT_ENVIRONMENT_KEY) { configBuilder.AddUserSecrets <Program>(); } var builtConfig = configBuilder.Build(); var config = new ModixConfig(); builtConfig.Bind(config); var loggerConfig = new LoggerConfiguration() .MinimumLevel.Verbose() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .MinimumLevel.Override("Modix.DiscordSerilogAdapter", LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.Logger(subLoggerConfig => subLoggerConfig .MinimumLevel.Information() // .MinimumLevel.Override() is not supported for sub-loggers, even though the docs don't specify this. See https://github.com/serilog/serilog/pull/1033 .Filter.ByExcluding("SourceContext like 'Microsoft.%' and @Level in ['Information', 'Debug', 'Verbose']") .WriteTo.Console() .WriteTo.RollingFile(Path.Combine("logs", "{Date}.log"))) .WriteTo.RollingFile( new RenderedCompactJsonFormatter(), Path.Combine("logs", "{Date}.clef"), retainedFileCountLimit: 2); var seqEndpoint = config.SeqEndpoint; var seqKey = config.SeqKey; if (seqEndpoint != null && seqKey == null) // seq is enabled without a key { loggerConfig = loggerConfig.WriteTo.Seq(seqEndpoint); } else if (seqEndpoint != null && seqKey != null) //seq is enabled with a key { loggerConfig = loggerConfig.WriteTo.Seq(seqEndpoint, apiKey: seqKey); } var webhookId = config.LogWebhookId; var webhookToken = config.LogWebhookToken; var webHost = CreateWebHostBuilder(args, builtConfig).Build(); if (webhookId.HasValue && webhookToken != null) { loggerConfig = loggerConfig .WriteTo.DiscordWebhookSink(webhookId.Value, webhookToken, LogEventLevel.Error, webHost.Services.GetRequiredService <CodePasteService>()); } Log.Logger = loggerConfig.CreateLogger(); try { webHost.Run(); return(0); } catch (Exception ex) { Log.ForContext <Program>() .Fatal(ex, "Host terminated unexpectedly."); if (Debugger.IsAttached && Environment.UserInteractive) { Console.WriteLine(Environment.NewLine + "Press any key to exit..."); Console.ReadKey(true); } return(ex.HResult); } finally { Log.CloseAndFlush(); } }
public static AuthenticationBuilder AddModix(this AuthenticationBuilder builder, ModixConfig config) { return(builder.AddOAuth <DiscordAuthenticationOptions, ModixAuthenticationHandler>(DiscordAuthenticationDefaults.AuthenticationScheme, options => { options.ClaimActions.MapJsonKey(claimType: "avatarHash", jsonKey: "avatar"); options.ClientId = config.DiscordClientId; options.ClientSecret = config.DiscordClientSecret; options.Scope.Add("identify"); //options.Scope.Add("guilds"); options.Events.OnRemoteFailure = context => { context.Response.Redirect("/error"); var errorMessage = context.Failure.Message; //Generic oauth error if (errorMessage == "access_denied") { errorMessage = "There was a problem authenticating via OAuth. Try again later."; } context.Response.Cookies.Append("Error", errorMessage, new CookieOptions { Expires = DateTimeOffset.UtcNow.AddHours(1) }); context.HandleResponse(); return Task.CompletedTask; }; })); }
public StackExchangeModule(ModixConfig config) { _config = config; }
public ReplModule(ModixConfig config) { _config = config; _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", config.ReplToken); }
public ModixConfigValidator(IOptions <ModixConfig> config) { _config = config.Value; }
public IlModule(ModixConfig config, CodePasteService pasteService) { _pasteService = pasteService; _config = config; _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", config.ReplToken); }
public DesignatedChannelModule(IAuthorizationService authorizationService, IDesignatedChannelService designatedChannelService, IOptions <ModixConfig> config) { AuthorizationService = authorizationService; DesignatedChannelService = designatedChannelService; Config = config.Value; }
public PromotionService(DiscordSocketClient client, IPromotionRepository repository, ModixConfig config) { _client = client; _repository = repository; promotionChannelID = config.ChannelIdForPromotionCampaignAnnouncement; allowedToCommentRoleID = config.RoleIdToAllowCommentingOnPromotionCampaign; allowedToCreateRoleID = config.RoleIdToAllowCreatingPromotionCampaign; }
public HelpModule(ICommandHelpService commandHelpService, IOptions <ModixConfig> config) { _commandHelpService = commandHelpService; _config = config.Value; }
public ModixBot(ModixConfig config, ILogger logger) { _config = config ?? throw new ArgumentNullException(nameof(config)); _map.AddLogging(bldr => bldr.AddSerilog(logger ?? Log.Logger)); }
public Startup(ModixConfig configuration) { Configuration = configuration; }