-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
84 lines (71 loc) · 2.87 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
using System;
using System.Threading.Tasks;
using BJR_bot.Services;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.KeyVault.Models;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.DependencyInjection;
namespace BJR_bot
{
class Program
{
private DiscordSocketClient _client;
// Discord.Net heavily utilizes TAP for async, so we create
// an asynchronous context from the beginning.
static void Main(string[] args)
{
new Program().MainAsync().GetAwaiter().GetResult();
}
public async Task MainAsync()
{
var token = await GetTokenAsync();
using var services = ConfigureServices();
_client = services.GetRequiredService<DiscordSocketClient>();
_client.Log += LogAsync;
services.GetRequiredService<CommandService>().Log += LogAsync;
_client.Ready += ReadyAsync;
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
// Here we initialize the logic required to register our commands.
await services.GetRequiredService<CommandHandlingService>().InitializeAsync();
// Block the program until it is closed.
await Task.Delay(-1);
}
private async Task<string> GetTokenAsync()
{
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
KeyVaultClient keyVaultClient =
new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient
.GetSecretAsync(
"https://discord-bot-keyvault.vault.azure.net/secrets/bot-token/f1b36f4771cf4671b9c0e43d963c625c")
.ConfigureAwait(false);
string token = secret.Value;
return token;
}
private Task LogAsync(LogMessage log)
{
Console.WriteLine(log.ToString());
return Task.CompletedTask;
}
// The Ready event indicates that the client has opened a
// connection and it is now safe to access the cache.
private Task ReadyAsync()
{
Console.WriteLine($"{_client.CurrentUser} is connected!");
return Task.CompletedTask;
}
private ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddSingleton<DiscordSocketClient>()
.AddSingleton<CommandService>()
.AddSingleton<CommandHandlingService>()
.AddSingleton<LolService>()
.BuildServiceProvider();
}
}
}