-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
66 lines (52 loc) · 1.74 KB
/
Program.cs
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
using System.Data;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using Microsoft.Extensions.DependencyInjection;
using DiscordBot;
using DiscordBot.Commands;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
MainAsync().GetAwaiter().GetResult();
static async Task MainAsync()
{
var botConfig = TryGetBotConfig();
var discordConfiguration = new DiscordConfiguration()
{
Token = botConfig.Token,
TokenType = TokenType.Bot
};
var discord = new DiscordClient(discordConfiguration);
var services = new ServiceCollection()
.AddTransient<IPieceManager, PieceManager>()
.AddSingleton<IVoiceChat, VoiceChat>(x =>
{
return new VoiceChat(discord,
x.GetRequiredService<IPieceManager>());
})
.BuildServiceProvider();
var commandConfig = new CommandsNextConfiguration()
{
Services = services,
StringPrefixes = new[] {botConfig.Prefix},
CaseSensitive = false
};
var commands = discord.UseCommandsNext(commandConfig);
commands.RegisterCommands<VoiceChatAudioCommandModule>();
await discord.ConnectAsync();
var voiceChatService = (IVoiceChat)services.GetService(typeof(IVoiceChat));
await voiceChatService.StartVoiceChatCheck();
await Task.Delay(-1);
BotConfig TryGetBotConfig()
{
BotConfig botConfig;
try
{
botConfig = JsonSerializer.Deserialize<BotConfig>(File.OpenRead("config.json")) ?? throw new NoNullAllowedException("Bot config is null.");
}
catch (Exception)
{
throw new JsonSerializationException("Unsuccessful deserialization of bot config.");
}
return botConfig;
}
}