-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
111 lines (91 loc) · 3.21 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
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
// See https://aka.ms/new-console-template for more information
using Discord;
using Discord.Commands;
using Discord.Net;
using Discord.WebSocket;
using DiscordGameAssist.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
var config = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json")
.AddEnvironmentVariables()
.Build();
var discordConfig = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug
};
var client = new DiscordSocketClient(discordConfig);
var commands = new CommandService(new CommandServiceConfig
{
LogLevel = LogSeverity.Info,
CaseSensitiveCommands = false,
});
// Setup your DI container.
Bootstrapper.Init();
Bootstrapper.RegisterInstance(client);
Bootstrapper.RegisterInstance(commands);
Bootstrapper.RegisterInstance(config);
RegisterSlashCommands();
RegisterServices();
RegisterRepositories();
RegisterFactories();
void RegisterRepositories()
{
Bootstrapper.RegisterType<ISlashCommandRepository, SlashCommandRepository>();
}
void RegisterFactories()
{
Bootstrapper.RegisterType<ISlashCommandFactory, SlashCommandFactory>();
}
void RegisterServices()
{
Bootstrapper.RegisterType<ISlashCommandService, SlashCommandService>();
Bootstrapper.RegisterType<ICommandHandler, CommandHandler>();
Bootstrapper.RegisterType<ISlashCommandHandler, SlashCommandHandler>();
}
void RegisterSlashCommands()
{
Bootstrapper.RegisterType<ISlashCommand, GenerateMemeCommand>();
Bootstrapper.RegisterType<ISlashCommand, JokeCommand>();
Bootstrapper.RegisterType<ISlashCommand, PingCommand>();
Bootstrapper.RegisterType<ISlashCommand, PlexInviteCommand>();
Bootstrapper.RegisterType<ISlashCommand, ProcessCheckCommand>();
Bootstrapper.RegisterType<ISlashCommand, StartServiceCommand>();
Bootstrapper.RegisterType<ISlashCommand, StopServiceCommand>();
}
await MainAsync();
async Task MainAsync()
{
client.Ready += Client_Ready;
var token = config.GetRequiredSection("Settings")["DiscordBotToken"];
if (string.IsNullOrWhiteSpace(token))
{
Console.WriteLine("Token is null or empty.");
return;
}
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
// Wait infinitely so your bot actually stays connected.
await Task.Delay(Timeout.Infinite);
}
async Task Client_Ready()
{
Console.WriteLine("Client Ready");
var slashCommandFactory = Bootstrapper.ServiceProvider.GetRequiredService<ISlashCommandFactory>();
var slashCommandBuilders = slashCommandFactory.CreateSlashCommandBuilders();
var slashCommandHandler = Bootstrapper.ServiceProvider.GetRequiredService<ISlashCommandHandler>();
client.SlashCommandExecuted += slashCommandHandler.Handle;
try
{
foreach (var slashCommandBuilder in slashCommandBuilders)
{
await client.CreateGlobalApplicationCommandAsync(slashCommandBuilder.Build());
}
}
catch (HttpException exception)
{
var json = JsonConvert.SerializeObject(exception.Message, Formatting.Indented);
Console.WriteLine(json);
}
}