Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return IServiceCollection instead of void in ServiceCollectionExtensions #34

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions Discord.Addons.Hosting/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,42 @@ public static class ServiceCollectionExtensions
/// Adds and optionally configures a <see cref="DiscordShardedClient"/> along with the required services.
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="collection">The service collection to configure.</param>
/// <param name="config">The delegate for the <see cref="DiscordHostConfiguration" /> that will be used to configure the host.</param>
/// <exception cref="InvalidOperationException">Thrown if client is already added to the service collection</exception>
public static void AddDiscordShardedHost(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config)
public static IServiceCollection AddDiscordShardedHost(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config)
{
collection.AddDiscordHostInternal<DiscordShardedClient>(config);

if (collection.Any(x => x.ServiceType.BaseType == typeof(BaseSocketClient)))
throw new InvalidOperationException("Cannot add more than one Discord Client to host");

collection.AddSingleton<DiscordShardedClient, InjectableDiscordShardedClient>();

return collection;
}

/// <summary>
/// Adds and optionally configures a <see cref="DiscordSocketClient"/> along with the required services.
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="builder">The host builder to configure.</param>
/// <param name="config">The delegate for the <see cref="DiscordHostConfiguration" /> that will be used to configure the host.</param>
/// <exception cref="InvalidOperationException">Thrown if client is already added to the service collection</exception>
public static void AddDiscordHost(this IServiceCollection builder, Action<DiscordHostConfiguration, IServiceProvider> config)
public static IServiceCollection AddDiscordHost(this IServiceCollection builder, Action<DiscordHostConfiguration, IServiceProvider> config)
{
builder.AddDiscordHostInternal<DiscordSocketClient>(config);

if (builder.Any(x => x.ServiceType.BaseType == typeof(BaseSocketClient)))
throw new InvalidOperationException("Cannot add more than one Discord Client to host");

builder.AddSingleton<DiscordSocketClient, InjectableDiscordSocketClient>();

return builder;
}

private static void AddDiscordHostInternal<T>(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config) where T: BaseSocketClient
Expand Down Expand Up @@ -96,19 +100,19 @@ static bool ValidateToken(string token)
/// </summary>
/// <param name="collection">The service collection to configure.</param>
/// <exception cref="InvalidOperationException">Thrown if <see cref="CommandService"/> is already added to the collection</exception>
public static void AddCommandService(this IServiceCollection collection) => collection.AddCommandService((context, config) => { });
public static IServiceCollection AddCommandService(this IServiceCollection collection) => collection.AddCommandService((context, config) => { });

/// <summary>
/// Adds a <see cref="CommandService"/> instance to the host for use with a Discord.NET client. />
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="collection">The service collection to configure.</param>
/// <param name="config">The delegate for configuring the <see cref="CommandServiceConfig" /> that will be used to initialise the service.</param>
/// <exception cref="ArgumentNullException">Thrown if config is null</exception>
/// <exception cref="InvalidOperationException">Thrown if <see cref="CommandService"/> is already added to the collection</exception>
public static void AddCommandService(this IServiceCollection collection, Action<CommandServiceConfig, IServiceProvider> config)
public static IServiceCollection AddCommandService(this IServiceCollection collection, Action<CommandServiceConfig, IServiceProvider> config)
{
ArgumentNullException.ThrowIfNull(config);

Expand All @@ -119,27 +123,29 @@ public static void AddCommandService(this IServiceCollection collection, Action<

collection.AddSingleton<CommandService, InjectableCommandService>();
collection.AddHostedService<CommandServiceRegistrationHost>();

return collection;
}

/// <summary>
/// Adds a <see cref="InteractionService"/> instance to the host for use with a Discord.NET client. />
/// </summary>
/// <param name="collection">The service collection to configure.</param>
/// <exception cref="InvalidOperationException">Thrown if <see cref="InteractionService"/> is already added to the collection</exception>
public static void AddInteractionService(this IServiceCollection collection) => collection.AddInteractionService((_, _) => { });
public static IServiceCollection AddInteractionService(this IServiceCollection collection) => collection.AddInteractionService((_, _) => { });


/// <summary>
/// Adds a <see cref="InteractionService"/> instance to the host for use with a Discord.NET client. />
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="collection">The service collection to configure.</param>
/// <param name="config">The delegate for configuring the <see cref="InteractionServiceConfig" /> that will be used to initialise the service.</param>
/// <exception cref="ArgumentNullException">Thrown if config is null</exception>
/// <exception cref="InvalidOperationException">Thrown if <see cref="InteractionService"/> is already added to the collection</exception>
public static void AddInteractionService(this IServiceCollection collection, Action<InteractionServiceConfig, IServiceProvider> config)
public static IServiceCollection AddInteractionService(this IServiceCollection collection, Action<InteractionServiceConfig, IServiceProvider> config)
{
ArgumentNullException.ThrowIfNull(config);

Expand All @@ -150,6 +156,7 @@ public static void AddInteractionService(this IServiceCollection collection, Act

collection.AddSingleton<InteractionService, InjectableInteractionService>();
collection.AddHostedService<InteractionServiceRegistrationHost>();


return collection;
}
}
}
Loading