AnsonBot/AnsonBot.Bot/Program.cs
2026-07-30 20:47:39 +01:00

75 lines
No EOL
2.5 KiB
C#

using AnsonBot.Bot;
using AnsonBot.Core;
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
// config binding
builder.Services.Configure<DockerOptions>(builder.Configuration.GetSection("Docker"));
builder.Services.Configure<DiscordOptions>(builder.Configuration.GetSection("Discord"));
builder.Services.Configure<TrackerOptions>(builder.Configuration.GetSection("Tracker"));
// our services
builder.Services.AddSingleton<IRconService, DockerRconService>();
// persistence
builder.Services.AddDbContextFactory<BotDbContext>(o =>
o.UseNpgsql(builder.Configuration.GetConnectionString("Postgres")));
// Registered as a singleton as well as a hosted service so the /players command
// and the notifier resolve the same instance the polling loop runs on.
builder.Services.AddSingleton<PlayerTrackerService>();
builder.Services.AddHostedService(x => x.GetRequiredService<PlayerTrackerService>());
// discord
builder.Services.AddSingleton(new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.Guilds,
AlwaysDownloadUsers = false,
LogLevel = LogSeverity.Info
});
builder.Services.AddSingleton<DiscordSocketClient>();
builder.Services.AddSingleton(x => new InteractionService(
x.GetRequiredService<DiscordSocketClient>()));
// dev-only: tunnel to the home server's Docker socket.
// Registered first so it starts before the bot and stops after it.
if (builder.Environment.IsDevelopment())
{
builder.Services.Configure<SshTunnelOptions>(builder.Configuration.GetSection("SshTunnel"));
builder.Services.AddHostedService<SshTunnelService>();
}
// the runner
builder.Services.AddHostedService<BotHostedService>();
// subscribes to tracker events; after BotHostedService so the client exists
builder.Services.AddHostedService<PlayerNotificationService>();
var app = builder.Build();
await app.RunAsync();
//DiscordSocketConfig config = new()
//{
// GatewayIntents = GatewayIntents.All | GatewayIntents.GuildMembers | GatewayIntents.GuildBans
//};
//DiscordSocketClient _client = new(config);
//DiscordService service = new(_client);
//var token = "MTUyNjk1Njk4MTM5MzI5MzQxMg.G46bg4.RTqBp1gvtEirLUtbojdpktz9MCZF76Sm4ecqOo";
//await _client.LoginAsync(TokenType.Bot, token);
//await _client.StartAsync();
//_client.Ready += service.ClientReady;
//_client.SlashCommandExecuted += service.SlashHandler;
//Console.WriteLine("Events registered!");
//await Task.Delay(-1);