| @@ -71,7 +71,7 @@ namespace Discord.Audio | |||
| { | |||
| Id = id; | |||
| _config = client.Config; | |||
| Service = client.Services.Get<AudioService>(); | |||
| Service = client.GetService<AudioService>(); | |||
| Config = Service.Config; | |||
| Serializer = client.Serializer; | |||
| _gatewayState = (int)ConnectionState.Disconnected; | |||
| @@ -7,20 +7,20 @@ namespace Discord.Audio | |||
| { | |||
| public static DiscordClient UsingAudio(this DiscordClient client, AudioServiceConfig config = null) | |||
| { | |||
| client.Services.Add(new AudioService(config)); | |||
| client.AddService(new AudioService(config)); | |||
| return client; | |||
| } | |||
| public static DiscordClient UsingAudio(this DiscordClient client, Action<AudioServiceConfigBuilder> configFunc = null) | |||
| { | |||
| var builder = new AudioServiceConfigBuilder(); | |||
| configFunc(builder); | |||
| client.Services.Add(new AudioService(builder)); | |||
| client.AddService(new AudioService(builder)); | |||
| return client; | |||
| } | |||
| public static Task<IAudioClient> JoinAudio(this Channel channel) => channel.Client.Services.Get<AudioService>().Join(channel); | |||
| public static Task LeaveAudio(this Channel channel) => channel.Client.Services.Get<AudioService>().Leave(channel); | |||
| public static Task LeaveAudio(this Server server) => server.Client.Services.Get<AudioService>().Leave(server); | |||
| public static IAudioClient GetAudioClient(Server server) => server.Client.Services.Get<AudioService>().GetClient(server); | |||
| public static Task<IAudioClient> JoinAudio(this Channel channel) => channel.Client.GetService<AudioService>().Join(channel); | |||
| public static Task LeaveAudio(this Channel channel) => channel.Client.GetService<AudioService>().Leave(channel); | |||
| public static Task LeaveAudio(this Server server) => server.Client.GetService<AudioService>().Leave(server); | |||
| public static IAudioClient GetAudioClient(Server server) => server.Client.GetService<AudioService>().GetClient(server); | |||
| } | |||
| } | |||
| @@ -6,14 +6,14 @@ namespace Discord.Commands | |||
| { | |||
| public static DiscordClient UsingCommands(this DiscordClient client, CommandServiceConfig config = null) | |||
| { | |||
| client.Services.Add(new CommandService(config)); | |||
| client.AddService(new CommandService(config)); | |||
| return client; | |||
| } | |||
| public static DiscordClient UsingCommands(this DiscordClient client, Action<CommandServiceConfigBuilder> configFunc = null) | |||
| { | |||
| var builder = new CommandServiceConfigBuilder(); | |||
| configFunc(builder); | |||
| client.Services.Add(new CommandService(builder)); | |||
| client.AddService(new CommandService(builder)); | |||
| return client; | |||
| } | |||
| } | |||
| @@ -10,7 +10,7 @@ | |||
| internal PermissionLevelChecker(DiscordClient client, int minPermissions) | |||
| { | |||
| _service = client.Services.Get<PermissionLevelService>(true); | |||
| _service = client.GetService<PermissionLevelService>(true); | |||
| _minPermissions = minPermissions; | |||
| } | |||
| @@ -6,7 +6,7 @@ namespace Discord.Commands.Permissions.Levels | |||
| { | |||
| public static DiscordClient UsingPermissionLevels(this DiscordClient client, Func<User, Channel, int> permissionResolver) | |||
| { | |||
| client.Services.Add(new PermissionLevelService(permissionResolver)); | |||
| client.AddService(new PermissionLevelService(permissionResolver)); | |||
| return client; | |||
| } | |||
| @@ -6,7 +6,7 @@ | |||
| internal BlacklistChecker(DiscordClient client) | |||
| { | |||
| _service = client.Services.Get<BlacklistService>(true); | |||
| _service = client.GetService<BlacklistService>(true); | |||
| } | |||
| public bool CanRun(Command command, User user, Channel channel, out string error) | |||
| @@ -6,7 +6,7 @@ namespace Discord.Commands.Permissions.Userlist | |||
| { | |||
| public static DiscordClient UsingGlobalBlacklist(this DiscordClient client, params ulong[] initialUserIds) | |||
| { | |||
| client.Services.Add(new BlacklistService(initialUserIds)); | |||
| client.AddService(new BlacklistService(initialUserIds)); | |||
| return client; | |||
| } | |||
| @@ -27,22 +27,22 @@ namespace Discord.Commands.Permissions.Userlist | |||
| } | |||
| public static IEnumerable<ulong> GetBlacklistedUserIds(this DiscordClient client) | |||
| => client.Services.Get<BlacklistService>().UserIds; | |||
| => client.GetService<BlacklistService>().UserIds; | |||
| public static void BlacklistUser(this DiscordClient client, User user) | |||
| { | |||
| client.Services.Get<BlacklistService>().Add(user.Id); | |||
| client.GetService<BlacklistService>().Add(user.Id); | |||
| } | |||
| public static void BlacklistUser(this DiscordClient client, ulong userId) | |||
| { | |||
| client.Services.Get<BlacklistService>().Add(userId); | |||
| client.GetService<BlacklistService>().Add(userId); | |||
| } | |||
| public static void UnBlacklistUser(this DiscordClient client, User user) | |||
| { | |||
| client.Services.Get<BlacklistService>().Remove(user.Id); | |||
| client.GetService<BlacklistService>().Remove(user.Id); | |||
| } | |||
| public static void UnBlacklistUser(this DiscordClient client, ulong userId) | |||
| { | |||
| client.Services.Get<BlacklistService>().Remove(userId); | |||
| client.GetService<BlacklistService>().Remove(userId); | |||
| } | |||
| } | |||
| } | |||
| @@ -6,7 +6,7 @@ | |||
| internal WhitelistChecker(DiscordClient client) | |||
| { | |||
| _service = client.Services.Get<WhitelistService>(true); | |||
| _service = client.GetService<WhitelistService>(true); | |||
| } | |||
| public bool CanRun(Command command, User user, Channel channel, out string error) | |||
| @@ -6,7 +6,7 @@ namespace Discord.Commands.Permissions.Userlist | |||
| { | |||
| public static DiscordClient UsingGlobalWhitelist(this DiscordClient client, params ulong[] initialUserIds) | |||
| { | |||
| client.Services.Add(new WhitelistService(initialUserIds)); | |||
| client.AddService(new WhitelistService(initialUserIds)); | |||
| return client; | |||
| } | |||
| @@ -27,22 +27,22 @@ namespace Discord.Commands.Permissions.Userlist | |||
| } | |||
| public static IEnumerable<ulong> GetWhitelistedUserIds(this DiscordClient client) | |||
| => client.Services.Get<WhitelistService>().UserIds; | |||
| => client.GetService<WhitelistService>().UserIds; | |||
| public static void WhitelistUser(this DiscordClient client, User user) | |||
| { | |||
| client.Services.Get<WhitelistService>().Add(user.Id); | |||
| client.GetService<WhitelistService>().Add(user.Id); | |||
| } | |||
| public static void WhitelistUser(this DiscordClient client, ulong userId) | |||
| { | |||
| client.Services.Get<WhitelistService>().Add(userId); | |||
| client.GetService<WhitelistService>().Add(userId); | |||
| } | |||
| public static void UnWhitelistUser(this DiscordClient client, User user) | |||
| { | |||
| client.Services.Get<WhitelistService>().Remove(user.Id); | |||
| client.GetService<WhitelistService>().Remove(user.Id); | |||
| } | |||
| public static void RemoveFromWhitelist(this DiscordClient client, ulong userId) | |||
| { | |||
| client.Services.Get<WhitelistService>().Remove(userId); | |||
| client.GetService<WhitelistService>().Remove(userId); | |||
| } | |||
| } | |||
| } | |||
| @@ -4,24 +4,22 @@ | |||
| { | |||
| public static DiscordClient UsingModules(this DiscordClient client) | |||
| { | |||
| client.Services.Add(new ModuleService()); | |||
| client.AddService(new ModuleService()); | |||
| return client; | |||
| } | |||
| public static DiscordClient AddModule<T>(this DiscordClient client, T instance, string name = null, ModuleFilter filter = ModuleFilter.None) | |||
| public static void AddModule<T>(this DiscordClient client, T instance, string name = null, ModuleFilter filter = ModuleFilter.None) | |||
| where T : class, IModule | |||
| { | |||
| client.Modules().Add(instance, name ?? nameof(T), filter); | |||
| return client; | |||
| client.GetService<ModuleService>().Add(instance, name ?? nameof(T), filter); | |||
| } | |||
| public static DiscordClient AddModule<T>(this DiscordClient client, string name = null, ModuleFilter filter = ModuleFilter.None) | |||
| public static void AddModule<T>(this DiscordClient client, string name = null, ModuleFilter filter = ModuleFilter.None) | |||
| where T : class, IModule, new() | |||
| { | |||
| client.Modules().Add(new T(), name ?? nameof(T), filter); | |||
| return client; | |||
| client.GetService<ModuleService>().Add(new T(), name ?? nameof(T), filter); | |||
| } | |||
| public static ModuleService Modules(this DiscordClient client, bool required = true) | |||
| => client.Services.Get<ModuleService>(required); | |||
| public static ModuleManager<T> GetModule<T>(this DiscordClient client) | |||
| where T : class, IModule | |||
| => client.GetService<ModuleService>().Get<T>(); | |||
| } | |||
| } | |||
| @@ -7,6 +7,17 @@ using System.Linq; | |||
| namespace Discord.Modules | |||
| { | |||
| public class ModuleManager<T> : ModuleManager | |||
| where T : class, IModule | |||
| { | |||
| public new T Instance => base.Instance as T; | |||
| internal ModuleManager(DiscordClient client, T instance, string name, ModuleFilter filterType) | |||
| : base(client, instance, name, filterType) | |||
| { | |||
| } | |||
| } | |||
| public class ModuleManager | |||
| { | |||
| public event EventHandler<ServerEventArgs> ServerEnabled = delegate { }; | |||
| @@ -115,7 +126,7 @@ namespace Discord.Modules | |||
| public void CreateCommands(string prefix, Action<CommandGroupBuilder> config) | |||
| { | |||
| var commandService = Client.Services.Get<CommandService>(); | |||
| var commandService = Client.GetService<CommandService>(); | |||
| commandService.CreateGroup(prefix, x => | |||
| { | |||
| x.Category(Name); | |||
| @@ -8,11 +8,11 @@ namespace Discord.Modules | |||
| public DiscordClient Client { get; private set; } | |||
| public IEnumerable<ModuleManager> Modules => _modules.Values; | |||
| private readonly Dictionary<IModule, ModuleManager> _modules; | |||
| private readonly Dictionary<Type, ModuleManager> _modules; | |||
| public ModuleService() | |||
| { | |||
| _modules = new Dictionary<IModule, ModuleManager>(); | |||
| _modules = new Dictionary<Type, ModuleManager>(); | |||
| } | |||
| void IService.Install(DiscordClient client) | |||
| @@ -20,29 +20,30 @@ namespace Discord.Modules | |||
| Client = client; | |||
| } | |||
| public T Add<T>(T module, string name, ModuleFilter type) | |||
| public T Add<T>(T module, string name, ModuleFilter filterType) | |||
| where T : class, IModule | |||
| { | |||
| if (module == null) throw new ArgumentNullException(nameof(module)); | |||
| if (name == null) throw new ArgumentNullException(nameof(name)); | |||
| if (Client == null) | |||
| throw new InvalidOperationException("Service needs to be added to a DiscordClient before modules can be installed."); | |||
| if (_modules.ContainsKey(module)) | |||
| Type type = typeof(T); | |||
| if (_modules.ContainsKey(type)) | |||
| throw new InvalidOperationException("This module has already been added."); | |||
| var manager = new ModuleManager(Client, module, name, type); | |||
| _modules.Add(module, manager); | |||
| var manager = new ModuleManager<T>(Client, module, name, filterType); | |||
| _modules.Add(type, manager); | |||
| module.Install(manager); | |||
| return module; | |||
| } | |||
| public ModuleManager GetManager(IModule module) | |||
| { | |||
| if (module == null) throw new ArgumentNullException(nameof(module)); | |||
| ModuleManager result = null; | |||
| _modules.TryGetValue(module, out result); | |||
| return result; | |||
| } | |||
| public ModuleManager<T> Get<T>() | |||
| where T : class, IModule | |||
| { | |||
| ModuleManager manager; | |||
| if (_modules.TryGetValue(typeof(T), out manager)) | |||
| return manager as ModuleManager<T>; | |||
| return null; | |||
| } | |||
| } | |||
| } | |||
| @@ -17,7 +17,7 @@ | |||
| "dependencies": { | |||
| "Discord.Net": "0.9.0-rc3-3", | |||
| "Discord.Net.Commands": "0.9.0-rc3" | |||
| "Discord.Net.Commands": "0.9.0-rc3-1" | |||
| }, | |||
| "frameworks": { | |||
| "net45": { }, | |||
| @@ -481,15 +481,15 @@ | |||
| <Compile Include="..\Discord.Net\Events\UserUpdatedEventArgs.cs"> | |||
| <Link>Events\UserUpdatedEventArgs.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\Extensions.cs"> | |||
| <Link>Extensions.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\Format.cs"> | |||
| <Link>Format.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\IMentionable.cs"> | |||
| <Link>IMentionable.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\InternalExtensions.cs"> | |||
| <Link>InternalExtensions.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\IService.cs"> | |||
| <Link>IService.cs</Link> | |||
| </Compile> | |||
| @@ -592,8 +592,8 @@ | |||
| <Compile Include="..\Discord.Net\Net\WebSockets\WS4NetEngine.cs"> | |||
| <Link>Net\WebSockets\WS4NetEngine.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\ServiceManager.cs"> | |||
| <Link>ServiceManager.cs</Link> | |||
| <Compile Include="..\Discord.Net\ServiceCollection.cs"> | |||
| <Link>ServiceCollection.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\TaskManager.cs"> | |||
| <Link>TaskManager.cs</Link> | |||
| @@ -27,6 +27,7 @@ namespace Discord | |||
| private readonly ManualResetEvent _disconnectedEvent; | |||
| private readonly ManualResetEventSlim _connectedEvent; | |||
| private readonly TaskManager _taskManager; | |||
| private readonly ServiceCollection _services; | |||
| private readonly ConcurrentDictionary<ulong, Server> _servers; | |||
| private readonly ConcurrentDictionary<ulong, Channel> _channels; | |||
| private readonly ConcurrentDictionary<ulong, Channel> _privateChannels; //Key = RecipientId | |||
| @@ -44,8 +45,6 @@ namespace Discord | |||
| public RestClient StatusAPI { get; } | |||
| /// <summary> Gets the internal WebSocket for the Gateway event stream. </summary> | |||
| public GatewaySocket GatewaySocket { get; } | |||
| /// <summary> Gets the service manager used for adding extensions to this client. </summary> | |||
| public ServiceManager Services { get; } | |||
| /// <summary> Gets the queue used for outgoing messages, if enabled. </summary> | |||
| public MessageQueue MessageQueue { get; } | |||
| /// <summary> Gets the JSON serializer used by this client. </summary> | |||
| @@ -66,6 +65,8 @@ namespace Discord | |||
| /// <summary> Gets the game the current user is displayed as playing. </summary> | |||
| public string CurrentGame { get; private set; } | |||
| /// <summary> Gets a collection of all extensions added to this DiscordClient. </summary> | |||
| public IEnumerable<IService> Services => _services; | |||
| /// <summary> Gets a collection of all servers this client is a member of. </summary> | |||
| public IEnumerable<Server> Servers => _servers.Select(x => x.Value); | |||
| /// <summary> Gets a collection of all private channels this client is a member of. </summary> | |||
| @@ -152,7 +153,7 @@ namespace Discord | |||
| MessageQueue = new MessageQueue(ClientAPI, Log.CreateLogger("MessageQueue")); | |||
| //Extensibility | |||
| Services = new ServiceManager(this); | |||
| _services = new ServiceCollection(this); | |||
| } | |||
| /// <summary> Connects to the Discord server with the provided email and password. </summary> | |||
| @@ -1010,6 +1011,18 @@ namespace Discord | |||
| } | |||
| #endregion | |||
| #region Services | |||
| public T AddService<T>(T instance) | |||
| where T : class, IService | |||
| => _services.Add(instance); | |||
| public T AddService<T>() | |||
| where T : class, IService, new() | |||
| => _services.Add(new T()); | |||
| public T GetService<T>(bool isRequired = true) | |||
| where T : class, IService | |||
| => _services.Get<T>(isRequired); | |||
| #endregion | |||
| #region Async Wrapper | |||
| /// <summary> Blocking call that will execute the provided async method and wait until the client has been manually stopped. This is mainly intended for use in console applications. </summary> | |||
| public void ExecuteAndWait(Func<Task> asyncAction) | |||
| @@ -8,22 +8,6 @@ using System.Runtime.CompilerServices; | |||
| namespace Discord | |||
| { | |||
| public static class DiscordClientExtensions | |||
| { | |||
| public static DiscordClient AddService<T>(this DiscordClient client, T instance) | |||
| where T : class, IService | |||
| { | |||
| client.Services.Add(instance); | |||
| return client; | |||
| } | |||
| public static DiscordClient AddService<T>(this DiscordClient client) | |||
| where T : class, IService, new() | |||
| { | |||
| client.Services.Add(new T()); | |||
| return client; | |||
| } | |||
| } | |||
| internal static class InternalExtensions | |||
| { | |||
| internal static readonly IFormatProvider _format = CultureInfo.InvariantCulture; | |||
| @@ -4,13 +4,13 @@ using System.Collections.Generic; | |||
| namespace Discord | |||
| { | |||
| public class ServiceManager : IEnumerable<IService> | |||
| internal class ServiceCollection : IEnumerable<IService> | |||
| { | |||
| private readonly Dictionary<Type, IService> _services; | |||
| internal DiscordClient Client { get; } | |||
| internal ServiceManager(DiscordClient client) | |||
| internal ServiceCollection(DiscordClient client) | |||
| { | |||
| Client = client; | |||
| _services = new Dictionary<Type, IService>(); | |||