This repository was archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (73 loc) · 3 KB
/
Copy pathProgram.cs
File metadata and controls
86 lines (73 loc) · 3 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
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace DiscordYoutubeDL
{
// TODO: Refactor for Discord.Net 2.0
class Program
{
private readonly IConfiguration _config;
private DiscordSocketClient _client;
static void Main(string[] args)
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
new Program().MainAsync().GetAwaiter().GetResult();
}
public async Task MainAsync()
{
if (_config.GetValue<bool>("debug", false))
{
Console.WriteLine("Hello World!");
string configStr = "";
foreach (System.Collections.Generic.KeyValuePair<string,string> i in _config.AsEnumerable())
configStr += i.ToString();
await Log(new LogMessage(LogSeverity.Debug, "Config Dump", configStr));
}
using (var services = ConfigureServices())
{
var client = services.GetRequiredService<DiscordSocketClient>();
_client = client;
client.Log += Log;
services.GetRequiredService<CommandService>().Log += Log;
await client.LoginAsync(TokenType.Bot, _config["api_token"]);
// Set bot status
await client.SetStatusAsync(UserStatus.Online);
await client.StartAsync();
await services.GetRequiredService<CommandHandler>().InitializeAsync();
// Block this task until the program is closed.
await Task.Delay(-1);
}
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
if (_config.GetValue<bool>("debug", false) &&_client != null && _client.ConnectionState == ConnectionState.Connected && _client.LoginState == LoginState.LoggedIn)
_client.SetGameAsync(msg.ToString(), "https://github.com/varGeneric/DiscordYoutubeDownloader/");
return Task.CompletedTask;
}
public Program()
{
var builder = new ConfigurationBuilder() // Create a new instance of the config builder
#if DEBUG
.SetBasePath(AppContext.BaseDirectory + "../../..")
#else
.SetBasePath(AppContext.BaseDirectory)
#endif
.AddXmlFile("_configuration.xml", optional: false, reloadOnChange: true);
_config = builder.Build(); // Build the configuration
}
private ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddSingleton(_config)
.AddSingleton<DiscordSocketClient>()
.AddSingleton<CommandService>()
.AddSingleton<CommandHandler>()
.BuildServiceProvider();
}
}
}