-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cs
More file actions
81 lines (65 loc) · 2.5 KB
/
Bot.cs
File metadata and controls
81 lines (65 loc) · 2.5 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using ProjectB.Services;
using JikanDotNet;
namespace ProjectB
{
class Bot
{
// The interface to the discord client
public DiscordSocketClient m_discordClient { get; private set; }
// Bot token
// Currently gets this values from an environment variable.
private string m_botToken;
public async Task RunAsync()
{
IServiceProvider services = ConfigureServices();
m_discordClient = services.GetRequiredService<DiscordSocketClient>();
m_discordClient.Log += LogAsync;
services.GetRequiredService<CommandService>().Log += LogAsync;
m_discordClient.Ready += ReadyAsync;
m_botToken = Environment.GetEnvironmentVariable("ProjectBToken");
await m_discordClient.LoginAsync(TokenType.Bot, m_botToken);
await m_discordClient.StartAsync();
// Initialize the command handler
await services.GetRequiredService<CommandHandler>().InitializeAsync();
// Initialize the event handler
await services.GetRequiredService<Services.EventHandler>().InitializeAsync();
// Initialize Azur Lane services
await services.GetRequiredService<AzurLaneService>().InitializeAsync();
// Block until program is closed.
await Task.Delay(Timeout.Infinite);
}
private Task LogAsync(LogMessage message)
{
Console.WriteLine(message.ToString());
return Task.CompletedTask;
}
private Task ReadyAsync()
{
Console.WriteLine($"{m_discordClient.CurrentUser} has connected!");
return Task.CompletedTask;
}
// Add the required services for the bot to function
private ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddSingleton<DiscordSocketClient>()
.AddSingleton<CommandService>()
.AddSingleton<CommandHandler>()
.AddSingleton<ProjectB.Services.EventHandler>()
.AddSingleton<Jikan>()
.AddSingleton<MyAnimeListService>()
.AddSingleton<AzurLaneService>()
.BuildServiceProvider();
}
}
}