mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	Added and applied styles for private readonly fields, private fields to Extensions and Common folders.
- Some renamings and code cleanups - Chained method calls, binary expressions and binary patterns will now break into newlines - Type param constraints and base constructor calls will be on the new line
This commit is contained in:
		
							
								
								
									
										7
									
								
								src/NadekoBot/_Extensions/BotCredentialsExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/NadekoBot/_Extensions/BotCredentialsExtensions.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
namespace NadekoBot.Extensions;
 | 
			
		||||
 | 
			
		||||
public static class BotCredentialsExtensions
 | 
			
		||||
{
 | 
			
		||||
    public static bool IsOwner(this IBotCredentials creds, IUser user)
 | 
			
		||||
        => creds.OwnerIds.Contains(user.Id);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										73
									
								
								src/NadekoBot/_Extensions/ServiceCollectionExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								src/NadekoBot/_Extensions/ServiceCollectionExtensions.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,73 @@
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using NadekoBot.Modules.Music;
 | 
			
		||||
using NadekoBot.Modules.Music.Resolvers;
 | 
			
		||||
using NadekoBot.Modules.Music.Services;
 | 
			
		||||
using StackExchange.Redis;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Extensions;
 | 
			
		||||
 | 
			
		||||
public static class ServiceCollectionExtensions
 | 
			
		||||
{
 | 
			
		||||
    public static IServiceCollection AddBotStringsServices(this IServiceCollection services, int totalShards)
 | 
			
		||||
        => totalShards <= 1
 | 
			
		||||
            ? services.AddSingleton<IStringsSource, LocalFileStringsSource>()
 | 
			
		||||
                .AddSingleton<IBotStringsProvider, LocalBotStringsProvider>()
 | 
			
		||||
                .AddSingleton<IBotStrings, BotStrings>()
 | 
			
		||||
            : services.AddSingleton<IStringsSource, LocalFileStringsSource>()
 | 
			
		||||
                .AddSingleton<IBotStringsProvider, RedisBotStringsProvider>()
 | 
			
		||||
                .AddSingleton<IBotStrings, BotStrings>();
 | 
			
		||||
 | 
			
		||||
    public static IServiceCollection AddConfigServices(this IServiceCollection services)
 | 
			
		||||
    {
 | 
			
		||||
        var baseType = typeof(ConfigServiceBase<>);
 | 
			
		||||
 | 
			
		||||
        foreach (var type in Assembly.GetCallingAssembly().ExportedTypes.Where(x => x.IsSealed))
 | 
			
		||||
        {
 | 
			
		||||
            if (type.BaseType?.IsGenericType == true &&
 | 
			
		||||
                type.BaseType.GetGenericTypeDefinition() == baseType)
 | 
			
		||||
            {
 | 
			
		||||
                services.AddSingleton(type);
 | 
			
		||||
                services.AddSingleton(x => (IConfigService)x.GetRequiredService(type));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return services;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static IServiceCollection AddConfigMigrators(this IServiceCollection services)
 | 
			
		||||
        => services.AddSealedSubclassesOf(typeof(IConfigMigrator));
 | 
			
		||||
 | 
			
		||||
    public static IServiceCollection AddMusic(this IServiceCollection services)
 | 
			
		||||
        => services.AddSingleton<IMusicService, MusicService>()
 | 
			
		||||
            .AddSingleton<ITrackResolveProvider, TrackResolveProvider>()
 | 
			
		||||
            .AddSingleton<IYoutubeResolver, YtdlYoutubeResolver>()
 | 
			
		||||
            .AddSingleton<ISoundcloudResolver, SoundcloudResolver>()
 | 
			
		||||
            .AddSingleton<ILocalTrackResolver, LocalTrackResolver>()
 | 
			
		||||
            .AddSingleton<IRadioResolver, RadioResolver>()
 | 
			
		||||
            .AddSingleton<ITrackCacher, RedisTrackCacher>()
 | 
			
		||||
            .AddSingleton<YtLoader>()
 | 
			
		||||
            .AddSingleton<IPlaceholderProvider>(svc => svc.GetRequiredService<IMusicService>());
 | 
			
		||||
 | 
			
		||||
    // consider using scrutor, because slightly different versions
 | 
			
		||||
    // of this might be needed in several different places
 | 
			
		||||
    public static IServiceCollection AddSealedSubclassesOf(this IServiceCollection services, Type baseType)
 | 
			
		||||
    {
 | 
			
		||||
        var subTypes = Assembly.GetCallingAssembly()
 | 
			
		||||
            .ExportedTypes.Where(type => type.IsSealed && baseType.IsAssignableFrom(type));
 | 
			
		||||
 | 
			
		||||
        foreach (var subType in subTypes)
 | 
			
		||||
        {
 | 
			
		||||
            services.AddSingleton(baseType, subType);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return services;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static IServiceCollection AddRedis(this IServiceCollection services, string redisOptions)
 | 
			
		||||
    {
 | 
			
		||||
        var conf = ConfigurationOptions.Parse(redisOptions);
 | 
			
		||||
        services.AddSingleton(ConnectionMultiplexer.Connect(conf));
 | 
			
		||||
        return services;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user