-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (74 loc) · 3.26 KB
/
Program.cs
File metadata and controls
89 lines (74 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using WhitelistBot.Database.Contexts;
using WhitelistBot.Services;
namespace WhitelistBot;
public class Program
{
private static IConfiguration _configuration = null!;
private static IServiceProvider _services = null!;
private const string ConfigurationFileName = "WhitelistBot.json";
private static readonly DiscordSocketConfig SocketConfig = new()
{
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.GuildMembers | GatewayIntents.MessageContent,
AlwaysDownloadUsers = true,
};
// TODO: rewrite this entire fucking bot
// for now, i shitcode
private static readonly List<string> ConnectAddresses =
[
"denstation.net/salvation",
"denstation.net/damnation"
];
public static async Task Main(string[] args)
{
_configuration = new ConfigurationBuilder()
.AddJsonFile(ConfigurationFileName, false, false)
.Build();
var databaseHost = _configuration["database.host"];
var databaseUsername = _configuration["database.username"];
var databasePassword = _configuration["database.password"];
var databasePort = _configuration["database.port"];
var databaseTable = _configuration["database.table"];
var connectionString = $"Server={databaseHost};Port={databasePort};Database={databaseTable};User Id={databaseUsername};Password={databasePassword};";
var apiToken = _configuration["admin_api_token"];
if (string.IsNullOrWhiteSpace(apiToken))
return;
_services = new ServiceCollection()
.AddSingleton(_configuration)
.AddSingleton(SocketConfig)
.AddSingleton<DiscordSocketClient>()
.AddSingleton<CommandService>()
.AddSingleton<CommandHandlingService>()
.AddSingleton<WhitelistVoteService>()
.AddScoped<WhitelistService>(_ => new WhitelistService(ConnectAddresses, apiToken))
.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(connectionString))
.BuildServiceProvider();
var client = _services.GetRequiredService<DiscordSocketClient>();
client.Log += LogAsync;
// Here we can initialize the service that will register and execute our commands
await InitializeServices(_services);
// Bot token can be provided from the Configuration object we set up earlier
await client.LoginAsync(TokenType.Bot, _configuration["token"]);
await client.StartAsync();
// Never quit the program until manually forced to.
await Task.Delay(Timeout.Infinite);
}
private static async Task InitializeServices(IServiceProvider services)
{
await _services.GetRequiredService<CommandHandlingService>()
.InitializeAsync();
await _services.GetRequiredService<WhitelistVoteService>()
.InitializeAsync();
}
private static Task LogAsync(LogMessage message)
{
Console.WriteLine(message.ToString());
return Task.CompletedTask;
}
}