-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (62 loc) · 2.6 KB
/
Program.cs
File metadata and controls
69 lines (62 loc) · 2.6 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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json;
using Discord;
using Discord.Net;
using Discord.WebSocket;
using Discord.Interactions;
using DiscordBot.Models;
using DiscordBot.Controllers;
namespace DiscordBot
{
class Program
{
static Task Main(string[] args) => new Program().RunBot();
// Creating the necessary variables
private DiscordSocketClient _client;
private InteractionService _interactionService;
private LoggingService _loggingService;
private ConfigModel _config;
private IServiceProvider _services;
// Runbot task
public async Task RunBot()
{
// Read config.json into ConfigModel object
_config = JsonSerializer.Deserialize<ConfigModel>(File.ReadAllText("config.json"));
var socketConfig = new DiscordSocketConfig() // Set up socket config
{
GatewayIntents = GatewayIntents.AllUnprivileged,
AlwaysDownloadUsers = true
};
var interactionConfig = new InteractionServiceConfig() // Set up interaction service config
{
DefaultRunMode = Discord.Interactions.RunMode.Async
};
_client = new DiscordSocketClient(socketConfig); // Define _client
_interactionService = new InteractionService(_client, interactionConfig); // Define _interactionService
_loggingService = new LoggingService(_client, _interactionService); // Define _loggingService
_services = new ServiceCollection() // Define _services
.AddSingleton(_client)
.AddSingleton(_interactionService)
.AddSingleton(_loggingService)
.AddSingleton(_config)
.AddSingleton<InteractionHandler>()
.AddSingleton<ClientController>()
.AddTransient<GameSaveController>()
.BuildServiceProvider();
try // Initialize handlers
{
using IServiceScope scope = _services.CreateScope();
await scope.ServiceProvider.GetRequiredService<InteractionHandler>().InitializeAsync();
await scope.ServiceProvider.GetRequiredService<ClientController>().InitializeAsync();
await Task.Delay(-1); // Delay for -1 to keep the console window opened
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}