mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
* add: .prunecancel to cancel active prunes
* change: .qs improved with thumbnails * change: .prune now reports progress * dev: DryIoc replacing Ninject
This commit is contained in:
@@ -32,7 +32,7 @@ public static class StringExtensions
|
||||
{
|
||||
if (hideDots)
|
||||
{
|
||||
return str?.Substring(0, maxLength);
|
||||
return str?.Substring(0, Math.Min(str?.Length ?? 0, maxLength));
|
||||
}
|
||||
|
||||
if (str is null || str.Length <= maxLength)
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using Discord.Commands.Builders;
|
||||
using DryIoc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nadeko.Common.Medusa;
|
||||
using Nadeko.Medusa.Adapters;
|
||||
@@ -20,7 +21,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
|
||||
private readonly IBehaviorHandler _behHandler;
|
||||
private readonly IPubSub _pubSub;
|
||||
private readonly IMedusaConfigService _medusaConfig;
|
||||
private readonly IKernel _kernel;
|
||||
private readonly IContainer _kernel;
|
||||
|
||||
private readonly ConcurrentDictionary<string, ResolvedMedusa> _resolved = new();
|
||||
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
|
||||
@@ -34,7 +35,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
|
||||
|
||||
public MedusaLoaderService(
|
||||
CommandService cmdService,
|
||||
IKernel kernel,
|
||||
IContainer kernel,
|
||||
IBehaviorHandler behHandler,
|
||||
IPubSub pubSub,
|
||||
IMedusaConfigService medusaConfig)
|
||||
@@ -337,14 +338,17 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
|
||||
|
||||
// load services
|
||||
ninjectModule = new MedusaNinjectModule(a, safeName);
|
||||
_kernel.Load(ninjectModule);
|
||||
|
||||
// todo medusa won't work, uncomment
|
||||
// _kernel.Load(ninjectModule);
|
||||
|
||||
var sis = LoadSneksFromAssembly(safeName, a);
|
||||
typeReaders = LoadTypeReadersFromAssembly(a, strings);
|
||||
|
||||
if (sis.Count == 0)
|
||||
{
|
||||
_kernel.Unload(safeName);
|
||||
// todo uncomment
|
||||
// _kernel.Unload(safeName);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -604,7 +608,8 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
|
||||
var km = lsi.KernelModule;
|
||||
lsi.KernelModule = null!;
|
||||
|
||||
_kernel.Unload(km.Name);
|
||||
// todo uncomment
|
||||
// _kernel.Unload(km.Name);
|
||||
|
||||
if (km is IDisposable d)
|
||||
d.Dispose();
|
||||
|
@@ -61,7 +61,7 @@ public abstract class NadekoModule : ModuleBase
|
||||
}
|
||||
|
||||
// TypeConverter typeConverter = TypeDescriptor.GetConverter(propType); ?
|
||||
public async Task<string> GetUserInputAsync(ulong userId, ulong channelId)
|
||||
public async Task<string> GetUserInputAsync(ulong userId, ulong channelId, Func<string, bool> validate = null)
|
||||
{
|
||||
var userInputTask = new TaskCompletionSource<string>();
|
||||
var dsc = (DiscordSocketClient)ctx.Client;
|
||||
@@ -89,6 +89,9 @@ public abstract class NadekoModule : ModuleBase
|
||||
|| userMsg.Channel.Id != channelId)
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (validate is not null && !validate(arg.Content))
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (userInputTask.TrySetResult(arg.Content))
|
||||
userMsg.DeleteAfter(1);
|
||||
|
||||
|
@@ -1,43 +1,51 @@
|
||||
using Ninject;
|
||||
using DryIoc;
|
||||
|
||||
namespace NadekoBot.Extensions;
|
||||
|
||||
public static class NinjectIKernelExtensions
|
||||
public static class DryIocExtensions
|
||||
{
|
||||
public static IKernel AddSingleton<TImpl>(this IKernel kernel)
|
||||
public static IContainer AddSingleton<TSvc, TImpl>(this IContainer container)
|
||||
where TImpl : TSvc
|
||||
{
|
||||
kernel.Bind<TImpl>().ToSelf().InSingletonScope();
|
||||
return kernel;
|
||||
container.Register<TSvc, TImpl>(Reuse.Singleton);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public static IContainer AddSingleton<TSvc, TImpl>(this IContainer container, TImpl obj)
|
||||
where TImpl : TSvc
|
||||
{
|
||||
container.RegisterInstance<TSvc>(obj);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public static IKernel AddSingleton<TInterface, TImpl>(this IKernel kernel)
|
||||
where TImpl : TInterface
|
||||
public static IContainer AddSingleton<TSvc, TImpl>(this IContainer container, Func<IResolverContext, TSvc> factory)
|
||||
where TImpl : TSvc
|
||||
{
|
||||
kernel.Bind<TInterface>().To<TImpl>().InSingletonScope();
|
||||
return kernel;
|
||||
container.RegisterDelegate(factory, Reuse.Singleton);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public static IKernel AddSingleton<TImpl>(this IKernel kernel, TImpl obj)
|
||||
=> kernel.AddSingleton<TImpl, TImpl>(obj);
|
||||
|
||||
public static IKernel AddSingleton<TInterface, TImpl>(this IKernel kernel, TImpl obj)
|
||||
where TImpl : TInterface
|
||||
public static IContainer AddSingleton<TImpl>(this IContainer container)
|
||||
{
|
||||
kernel.Bind<TInterface>().ToConstant(obj).InSingletonScope();
|
||||
return kernel;
|
||||
container.Register<TImpl>(Reuse.Singleton);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public static IKernel AddSingleton<TImpl, TInterface>(
|
||||
this IKernel kernel,
|
||||
Func<Ninject.Activation.IContext, TImpl> factory)
|
||||
where TImpl : TInterface
|
||||
public static IContainer AddSingleton<TImpl>(this IContainer container, TImpl obj)
|
||||
{
|
||||
kernel.Bind<TInterface>().ToMethod(factory).InSingletonScope();
|
||||
return kernel;
|
||||
}
|
||||
container.RegisterInstance<TImpl>(obj);
|
||||
|
||||
public static IKernel AddSingleton<TImpl>(
|
||||
this IKernel kernel,
|
||||
Func<Ninject.Activation.IContext, TImpl> factory)
|
||||
=> kernel.AddSingleton<TImpl, TImpl>(factory);
|
||||
return container;
|
||||
}
|
||||
|
||||
public static IContainer AddSingleton<TImpl>(this IContainer container, Func<IResolverContext, TImpl> factory)
|
||||
{
|
||||
container.RegisterDelegate(factory);
|
||||
|
||||
return container;
|
||||
}
|
||||
}
|
@@ -1,9 +1,8 @@
|
||||
using DryIoc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NadekoBot.Modules.Music;
|
||||
using NadekoBot.Modules.Music.Resolvers;
|
||||
using NadekoBot.Modules.Music.Services;
|
||||
using Ninject;
|
||||
using Ninject.Extensions.Conventions;
|
||||
using Ninject.Extensions.Conventions.Syntax;
|
||||
using StackExchange.Redis;
|
||||
using System.Net;
|
||||
@@ -15,92 +14,103 @@ namespace NadekoBot.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IKernel AddBotStringsServices(this IKernel kernel, BotCacheImplemenation botCache)
|
||||
public static IContainer AddBotStringsServices(this IContainer svcs, BotCacheImplemenation botCache)
|
||||
{
|
||||
if (botCache == BotCacheImplemenation.Memory)
|
||||
{
|
||||
kernel.Bind<IStringsSource>().To<LocalFileStringsSource>().InSingletonScope();
|
||||
kernel.Bind<IBotStringsProvider>().To<MemoryBotStringsProvider>().InSingletonScope();
|
||||
kernel.Bind<IBotStrings>().To<BotStrings>().InSingletonScope();
|
||||
svcs.AddSingleton<IStringsSource, LocalFileStringsSource>();
|
||||
svcs.AddSingleton<IBotStringsProvider, MemoryBotStringsProvider>();
|
||||
}
|
||||
else
|
||||
{
|
||||
kernel.Bind<IStringsSource>().To<LocalFileStringsSource>().InSingletonScope();
|
||||
kernel.Bind<IBotStringsProvider>().To<RedisBotStringsProvider>().InSingletonScope();
|
||||
kernel.Bind<IBotStrings>().To<BotStrings>().InSingletonScope();
|
||||
svcs.AddSingleton<IStringsSource, LocalFileStringsSource>();
|
||||
svcs.AddSingleton<IBotStringsProvider, RedisBotStringsProvider>();
|
||||
}
|
||||
|
||||
return kernel;
|
||||
svcs.AddSingleton<IBotStrings, BotStrings>();
|
||||
|
||||
return svcs;
|
||||
}
|
||||
|
||||
public static IKernel AddConfigServices(this IKernel kernel, Assembly a)
|
||||
public static IContainer AddConfigServices(this IContainer kernel, Assembly a)
|
||||
{
|
||||
kernel.Bind(x =>
|
||||
// kernel.RegisterMany([typeof(ConfigServiceBase<>)]);
|
||||
|
||||
foreach (var type in a.GetTypes()
|
||||
.Where(x => !x.IsAbstract && x.IsAssignableToGenericType(typeof(ConfigServiceBase<>))))
|
||||
{
|
||||
var configs = x.From(a)
|
||||
.SelectAllClasses()
|
||||
.Where(f => f.IsAssignableToGenericType(typeof(ConfigServiceBase<>)));
|
||||
kernel.RegisterMany([type],
|
||||
getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType),
|
||||
getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton));
|
||||
}
|
||||
|
||||
configs.BindToSelfWithInterfaces()
|
||||
.Configure(c => c.InSingletonScope());
|
||||
});
|
||||
//
|
||||
// kernel.Bind(x =>
|
||||
// {
|
||||
// var configs = x.From(a)
|
||||
// .SelectAllClasses()
|
||||
// .Where(f => f.IsAssignableToGenericType(typeof(ConfigServiceBase<>)));
|
||||
//
|
||||
// configs.BindToSelfWithInterfaces()
|
||||
// .Configure(c => c.InSingletonScope());
|
||||
// });
|
||||
|
||||
return kernel;
|
||||
}
|
||||
|
||||
public static IKernel AddConfigMigrators(this IKernel kernel, Assembly a)
|
||||
public static IContainer AddConfigMigrators(this IContainer kernel, Assembly a)
|
||||
=> kernel.AddSealedSubclassesOf(typeof(IConfigMigrator), a);
|
||||
|
||||
public static IKernel AddMusic(this IKernel kernel)
|
||||
public static IContainer AddMusic(this IContainer kernel)
|
||||
{
|
||||
kernel.Bind<IMusicService, IPlaceholderProvider>()
|
||||
.To<MusicService>()
|
||||
.InSingletonScope();
|
||||
kernel.RegisterMany<MusicService>(Reuse.Singleton);
|
||||
|
||||
kernel.Bind<ITrackResolveProvider>().To<TrackResolveProvider>().InSingletonScope();
|
||||
kernel.Bind<IYoutubeResolver>().To<YtdlYoutubeResolver>().InSingletonScope();
|
||||
kernel.Bind<ILocalTrackResolver>().To<LocalTrackResolver>().InSingletonScope();
|
||||
kernel.Bind<IRadioResolver>().To<RadioResolver>().InSingletonScope();
|
||||
kernel.Bind<ITrackCacher>().To<TrackCacher>().InSingletonScope();
|
||||
// kernel.Bind<YtLoader>().ToSelf().InSingletonScope();
|
||||
kernel.AddSingleton<ITrackResolveProvider, TrackResolveProvider>();
|
||||
kernel.AddSingleton<IYoutubeResolver, YtdlYoutubeResolver>();
|
||||
kernel.AddSingleton<ILocalTrackResolver, LocalTrackResolver>();
|
||||
kernel.AddSingleton<IRadioResolver, RadioResolver>();
|
||||
kernel.AddSingleton<ITrackCacher, TrackCacher>();
|
||||
|
||||
return kernel;
|
||||
}
|
||||
|
||||
public static IKernel AddSealedSubclassesOf(this IKernel kernel, Type baseType, Assembly a)
|
||||
public static IContainer AddSealedSubclassesOf(this IContainer cont, Type baseType, Assembly a)
|
||||
{
|
||||
kernel.Bind(x =>
|
||||
var classes = a.GetExportedTypes()
|
||||
.Where(x => x.IsClass && !x.IsAbstract && x.IsPublic)
|
||||
.Where(x => x.IsNested && baseType.IsAssignableFrom(x));
|
||||
|
||||
foreach (var c in classes)
|
||||
{
|
||||
var classes = x.From(a)
|
||||
.SelectAllClasses()
|
||||
.Where(c => c.IsPublic && c.IsNested && baseType.IsAssignableFrom(baseType));
|
||||
cont.RegisterMany([c], Reuse.Singleton);
|
||||
// var inters = c.GetInterfaces();
|
||||
|
||||
classes.BindToSelfWithInterfaces().Configure(x => x.InSingletonScope());
|
||||
});
|
||||
// cont.RegisterMany(inters, c);
|
||||
}
|
||||
|
||||
return kernel;
|
||||
return cont;
|
||||
}
|
||||
|
||||
public static IKernel AddCache(this IKernel kernel, IBotCredentials creds)
|
||||
public static IContainer AddCache(this IContainer cont, IBotCredentials creds)
|
||||
{
|
||||
if (creds.BotCache == BotCacheImplemenation.Redis)
|
||||
{
|
||||
var conf = ConfigurationOptions.Parse(creds.RedisOptions);
|
||||
kernel.Bind<ConnectionMultiplexer>().ToConstant(ConnectionMultiplexer.Connect(conf)).InSingletonScope();
|
||||
kernel.Bind<IBotCache>().To<RedisBotCache>().InSingletonScope();
|
||||
kernel.Bind<IPubSub>().To<RedisPubSub>().InSingletonScope();
|
||||
cont.AddSingleton<ConnectionMultiplexer>(ConnectionMultiplexer.Connect(conf));
|
||||
cont.AddSingleton<IBotCache, RedisBotCache>();
|
||||
cont.AddSingleton<IPubSub, RedisPubSub>();
|
||||
}
|
||||
else
|
||||
{
|
||||
kernel.Bind<IBotCache>().To<MemoryBotCache>().InSingletonScope();
|
||||
kernel.Bind<IPubSub>().To<EventPubSub>().InSingletonScope();
|
||||
cont.AddSingleton<IBotCache, MemoryBotCache>();
|
||||
cont.AddSingleton<IPubSub, EventPubSub>();
|
||||
}
|
||||
|
||||
return kernel
|
||||
return cont
|
||||
.AddBotStringsServices(creds.BotCache);
|
||||
}
|
||||
|
||||
public static IKernel AddHttpClients(this IKernel kernel)
|
||||
public static IContainer AddHttpClients(this IContainer kernel)
|
||||
{
|
||||
IServiceCollection svcs = new ServiceCollection();
|
||||
svcs.AddHttpClient();
|
||||
@@ -117,8 +127,8 @@ public static class ServiceCollectionExtensions
|
||||
});
|
||||
|
||||
var prov = svcs.BuildServiceProvider();
|
||||
kernel.Bind<IHttpClientFactory>().ToMethod(_ => prov.GetRequiredService<IHttpClientFactory>());
|
||||
kernel.Bind<HttpClient>().ToMethod(_ => prov.GetRequiredService<HttpClient>());
|
||||
kernel.RegisterDelegate<IHttpClientFactory>(_ => prov.GetRequiredService<IHttpClientFactory>());
|
||||
kernel.RegisterDelegate<HttpClient>(_ => prov.GetRequiredService<HttpClient>());
|
||||
|
||||
return kernel;
|
||||
}
|
||||
@@ -126,26 +136,39 @@ public static class ServiceCollectionExtensions
|
||||
public static IConfigureSyntax BindToSelfWithInterfaces(this IJoinExcludeIncludeBindSyntax matcher)
|
||||
=> matcher.BindSelection((type, types) => types.Append(type));
|
||||
|
||||
public static IKernel AddLifetimeServices(this IKernel kernel, Assembly a)
|
||||
public static IContainer AddLifetimeServices(this IContainer kernel, Assembly a)
|
||||
{
|
||||
kernel.Bind(scan =>
|
||||
Type[] types =
|
||||
[
|
||||
typeof(IExecOnMessage),
|
||||
typeof(IExecPreCommand),
|
||||
typeof(IExecPostCommand),
|
||||
typeof(IExecNoCommand),
|
||||
typeof(IInputTransformer),
|
||||
typeof(INService)
|
||||
];
|
||||
|
||||
foreach (var svc in a.GetTypes()
|
||||
.Where(type => type.IsClass && types.Any(t => type.IsAssignableTo(t)) && !type.HasAttribute<DIIgnoreAttribute>()))
|
||||
{
|
||||
scan.From(a)
|
||||
.SelectAllClasses()
|
||||
.Where(c => (c.IsAssignableTo(typeof(INService))
|
||||
|| c.IsAssignableTo(typeof(IExecOnMessage))
|
||||
|| c.IsAssignableTo(typeof(IInputTransformer))
|
||||
|| c.IsAssignableTo(typeof(IExecPreCommand))
|
||||
|| c.IsAssignableTo(typeof(IExecPostCommand))
|
||||
|| c.IsAssignableTo(typeof(IExecNoCommand)))
|
||||
&& !c.HasAttribute<DIIgnoreAttribute>()
|
||||
#if GLOBAL_NADEKO
|
||||
&& !c.HasAttribute<NoPublicBotAttribute>()
|
||||
#endif
|
||||
)
|
||||
.BindToSelfWithInterfaces()
|
||||
.Configure(c => c.InSingletonScope());
|
||||
});
|
||||
kernel.RegisterMany([svc],
|
||||
getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType),
|
||||
getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton));
|
||||
}
|
||||
//
|
||||
// kernel.RegisterMany(
|
||||
// [a],
|
||||
// #if GLOBAL_NADEKO
|
||||
// && !c.HasAttribute<NoPublicBotAttribute>()
|
||||
// #endif
|
||||
// ),
|
||||
// reuse:
|
||||
// Reuse.Singleton
|
||||
// );
|
||||
|
||||
|
||||
// todo maybe self is missing
|
||||
// todo maybe attribute doesn't work
|
||||
|
||||
return kernel;
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ public interface IGoogleApiService
|
||||
IReadOnlyDictionary<string, string> Languages { get; }
|
||||
|
||||
Task<IEnumerable<string>> GetVideoLinksByKeywordAsync(string keywords, int count = 1);
|
||||
Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync(string keywords, int count = 1);
|
||||
Task<IEnumerable<(string Name, string Id, string Url, string Thumbnail)>> GetVideoInfosByKeywordAsync(string keywords, int count = 1);
|
||||
Task<IEnumerable<string>> GetPlaylistIdsByKeywordsAsync(string keywords, int count = 1);
|
||||
Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1, string user = null);
|
||||
Task<IEnumerable<string>> GetPlaylistTracksAsync(string playlistId, int count = 50);
|
||||
|
Reference in New Issue
Block a user