-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientBuilder.cs
111 lines (88 loc) · 3.42 KB
/
ClientBuilder.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
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
namespace Orikivo.Framework
{
// TODO: Learn how to shard.
/// <summary>
/// Represents a constructor for an <see cref="Client"/>.
/// </summary>
public class ClientBuilder
{
private const string CONFIG_FILE_NAME = "config.json";
/// <summary>
/// Initializes a new default <see cref="ClientBuilder"/>.
/// </summary>
/// <param name="configPath">The base directory at which the configuration will be stored. If unspecified, it will default to <see cref="Directory.GetCurrentDirectory"/>.</param>
public ClientBuilder(string configPath = null)
{
Config = new ConfigurationBuilder();
if (!string.IsNullOrWhiteSpace(configPath))
SetConfigPath(configPath);
Services = new ServiceCollection();
}
private string _configPath;
private IConfigurationBuilder Config { get; }
/// <summary>
/// Represents the collection of services that can be referenced in a <see cref="Client"/>.
/// </summary>
public ServiceCollection Services { get; }
public Dictionary<Type, TypeReader> TypeReaders { get; set; } = new Dictionary<Type, TypeReader>();
public List<Type> Modules { get; set; } = new List<Type>();
public DiscordSocketConfig SocketConfig { get; set; }
public CommandServiceConfig CommandConfig { get; set; }
public void SetConfigPath(string path)
{
string directory = Path.GetDirectoryName(path);
_configPath = Path.GetFileName(path);
Config.SetBasePath(directory);
}
public void SetDefaultConfigPath()
{
string directory = Directory.GetCurrentDirectory();
_configPath = null;
Config.SetBasePath(directory);
}
public ClientBuilder AddTypeReader<T>(TypeReader reader)
=> AddTypeReader(typeof(T), reader);
public ClientBuilder AddTypeReader(Type type, TypeReader reader)
{
if (!TypeReaders.TryAdd(type, reader))
TypeReaders[type] = reader;
return this;
}
public void RemoveTypeReader<T>()
=> RemoveTypeReader(typeof(T));
public void RemoveTypeReader(Type type)
{
if (TypeReaders.ContainsKey(type))
TypeReaders.Remove(type);
}
public ClientBuilder AddModule<T>()
where T : class
{
if (!Modules.Contains(typeof(T)))
Modules.Add(typeof(T));
return this;
}
public void RemoveModule<T>()
where T : class
{
if (Modules.Contains(typeof(T)))
Modules.Remove(typeof(T));
}
public Client Build()
{
Services
.AddSingleton(new DiscordSocketClient(SocketConfig))
.AddSingleton(new CommandService(CommandConfig))
.AddSingleton(Config.AddJsonFile(!string.IsNullOrWhiteSpace(_configPath) ? _configPath : CONFIG_FILE_NAME).Build())
.AddSingleton<ConnectionService>();
return new Client(Services.BuildServiceProvider(), TypeReaders, Modules);
}
}
}