This repository was archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (109 loc) · 4.01 KB
/
Copy pathProgram.cs
File metadata and controls
126 lines (109 loc) · 4.01 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Serilog;
using DevNet.Services;
using Microsoft.Extensions.DependencyInjection;
using Discord.Commands;
using Discord.Addons.Interactive;
using System.Configuration;
using DevNet.Modules;
using System.Diagnostics;
using System.IO;
namespace DevNet
{
/// <summary>
/// Runs discord client, while injecting dependencies.
/// </summary>
public class Program
{
//internal static ConfigurationBuilders confBuilder;
// Setup our fields
private ConfigurationBuilders builders;
private DiscordSocketClient client;
private static string _logLevel;
/// <summary>
/// Initializing Client
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
if (args.Count() != 0)
{
_logLevel = args[0];
}
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/devnet.log", rollingInterval: RollingInterval.Day)
.WriteTo.Console()
.CreateLogger();
new Program().MainAsync().GetAwaiter().GetResult();
}
/// <summary>
/// Builds Configuration file.
/// </summary>
public Program()
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builders = new ConfigurationBuilders
{
ConnString = ConfigurationManager.ConnectionStrings["CelestialError"]?.ConnectionString,
SmsMessageKey = ConfigurationManager.AppSettings["SmsMessageKey"]
};
}
/// <summary>
/// Starts discord client.
/// </summary>
/// <returns>
/// returns nothing, awaiting commands to be executed.
/// </returns>
public async Task MainAsync()
{
RoleModule roleModule = new RoleModule();
using ServiceProvider services = ConfigureServices();
var socketConfig = new DiscordSocketConfig
{
ExclusiveBulkDelete = true
};
client = new DiscordSocketClient(socketConfig);
DiscordSocketClient getClient = services.GetRequiredService<DiscordSocketClient>();
client = getClient;
client.UserJoined += roleModule.UserJoined;
// Setup loggingservice and ready event.
services.GetRequiredService<LoggingService>();
//await client.LoginAsync(TokenType.Bot, config["Token"]);
await client.LoginAsync(TokenType.Bot, token: $"{Environment.GetEnvironmentVariable("BotToken")}", true);
await client.StartAsync();
// Get the command handler class here.
// Initialize async method to start things up.
await services.GetRequiredService<CommandHandler>().InitializeAsync();
// Create a task, that completes after a delay.
// -1 to wait indefinitely.
await Task.Delay(-1);
}
/// <summary>
/// Configuration of service dependensies.
/// </summary>
/// <returns>
/// Return a ServiceCollection.
/// </returns>
private ServiceProvider ConfigureServices()
{
var services = new ServiceCollection()
.AddSingleton<DiscordSocketClient>()
.AddSingleton<CommandService>()
.AddSingleton<CommandHandler>()
.AddSingleton<LoggingService>()
.AddSingleton<InteractiveService>()
.AddSingleton<ConfigurationBuilders>()
.AddLogging(configure => configure.AddSerilog());
var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
}
}