-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
149 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using Discord; | ||
using Discord.Interactions; | ||
using Discord.WebSocket; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using OpenShock.DiscordBot; | ||
using OpenShock.DiscordBot.OpenShockDiscordDb; | ||
using OpenShock.DiscordBot.Services; | ||
using OpenShock.DiscordBot.Services.UserRepository; | ||
using OpenShock.DiscordBot.Utils; | ||
using Serilog; | ||
using System.Reflection; | ||
|
||
HostBuilder builder = new(); | ||
|
||
builder.UseContentRoot(Directory.GetCurrentDirectory()) | ||
.ConfigureHostConfiguration(config => | ||
{ | ||
config.AddEnvironmentVariables(prefix: "DOTNET_"); | ||
if (args is { Length: > 0 }) config.AddCommandLine(args); | ||
}) | ||
.ConfigureAppConfiguration((context, config) => | ||
{ | ||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) | ||
.AddJsonFile("appsettings.Custom.json", optional: true, reloadOnChange: false) | ||
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, | ||
reloadOnChange: false); | ||
config.AddEnvironmentVariables(); | ||
if (args is { Length: > 0 }) config.AddCommandLine(args); | ||
}) | ||
.UseDefaultServiceProvider((context, options) => | ||
{ | ||
var isDevelopment = context.HostingEnvironment.IsDevelopment(); | ||
options.ValidateScopes = isDevelopment; | ||
options.ValidateOnBuild = isDevelopment; | ||
}) | ||
.UseSerilog((context, _, config) => { config.ReadFrom.Configuration(context.Configuration); }); | ||
|
||
// <---- Services ----> | ||
|
||
builder.ConfigureServices((context, services) => | ||
{ | ||
var discordBotConfig = context.Configuration.GetSection("bot").Get<DiscordBotConfig>() ?? | ||
throw new Exception("Could not load bot config"); | ||
services.AddSingleton(discordBotConfig); | ||
services.AddDbContextPool<OpenShockDiscordContext>(x => | ||
{ | ||
x.UseNpgsql(discordBotConfig.Db.Conn); | ||
// ReSharper disable once InvertIf | ||
if (discordBotConfig.Db.Debug) | ||
{ | ||
x.EnableDetailedErrors(); | ||
x.EnableSensitiveDataLogging(); | ||
} | ||
}); | ||
services.AddSingleton<IUserRepository, UserRepository>(); | ||
services.AddSingleton<IOpenShockBackendService, OpenShockBackendService>(); | ||
services.AddSingleton<MessageHandler>(); | ||
services.AddSingleton<DiscordSocketClient>(new DiscordSocketClient(new DiscordSocketConfig | ||
{ | ||
GatewayIntents = GatewayIntents.AllUnprivileged | | ||
GatewayIntents.MessageContent | ||
})); | ||
services.AddSingleton<InteractionService>(x => | ||
new InteractionService(x.GetRequiredService<DiscordSocketClient>())); | ||
services.AddSingleton<InteractionHandler>(); | ||
services.AddHostedService<StatusTask>(); | ||
}); | ||
|
||
try | ||
{ | ||
var host = builder.Build(); | ||
var logger = host.Services.GetRequiredService<ILogger<Program>>(); | ||
logger.LogInformation("Starting OpenShock Discord Bot version {Version}", Assembly.GetEntryAssembly()?.GetName().Version?.ToString()); | ||
|
||
var config = host.Services.GetRequiredService<DiscordBotConfig>(); | ||
|
||
// <---- DATABASE MIGRATION ----> | ||
|
||
if (!config.Db.SkipMigration) | ||
{ | ||
logger.LogInformation("Running database migrations..."); | ||
using var scope = host.Services.CreateScope(); | ||
var openShockContext = scope.ServiceProvider.GetRequiredService<OpenShockDiscordContext>(); | ||
var pendingMigrations = (await openShockContext.Database.GetPendingMigrationsAsync()).ToList(); | ||
|
||
if (pendingMigrations.Count > 0) | ||
{ | ||
logger.LogInformation("Found pending migrations, applying [{@Migrations}]", pendingMigrations); | ||
await openShockContext.Database.MigrateAsync(); | ||
logger.LogInformation("Applied database migrations... proceeding with startup"); | ||
} | ||
else logger.LogInformation("No pending migrations found, proceeding with startup"); | ||
} | ||
else logger.LogWarning("Skipping possible database migrations..."); | ||
|
||
// <---- Initialize Service stuff, this also instantiates the singletons!!! ----> | ||
|
||
var client = host.Services.GetRequiredService<DiscordSocketClient>(); | ||
var interactionService = host.Services.GetRequiredService<InteractionService>(); | ||
var interactionHandler = host.Services.GetRequiredService<InteractionHandler>(); | ||
var messageHandler = host.Services.GetRequiredService<MessageHandler>(); | ||
|
||
client.Log += LoggingUtils.LogAsync; | ||
client.Ready += () => ReadyAsync(client, interactionService); | ||
|
||
client.MessageReceived += messageHandler.HandleMessageAsync; | ||
|
||
interactionService.Log += LoggingUtils.LogAsync; | ||
|
||
await interactionHandler.InitializeAsync(); | ||
|
||
// <---- Run discord client ----> | ||
|
||
await client.LoginAsync(TokenType.Bot, config.Token); | ||
await client.StartAsync(); | ||
|
||
await host.RunAsync(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
|
||
static async Task ReadyAsync(BaseSocketClient client, InteractionService interactionService) | ||
{ | ||
Log.Information("Connected as [{CurrentUser}]", client.CurrentUser.Username); | ||
await client.SetActivityAsync( | ||
new Game($"electricity flow, v{Assembly.GetEntryAssembly()?.GetName().Version?.ToString()}", | ||
ActivityType.Watching)); | ||
|
||
await interactionService.RegisterCommandsGloballyAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters