Some reorganization, still completely broken

This commit is contained in:
Kwoth
2023-02-11 22:07:29 +01:00
parent 307003e6fe
commit 1efda23c3d
7 changed files with 42 additions and 34 deletions

View File

@@ -132,37 +132,10 @@ public sealed class Bot
kernel.Bind<ICoordinator, IReadyExecutor>().To<RemoteGrpcCoordinator>().InSingletonScope(); kernel.Bind<ICoordinator, IReadyExecutor>().To<RemoteGrpcCoordinator>().InSingletonScope();
} }
kernel.Bind(scan => kernel.AddLifetimeServices();
{
var classes = scan.FromThisAssembly()
.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<DontAddToIocContainerAttribute>()
#if GLOBAL_NADEKO
&& !c.HasAttribute<NoPublicBotAttribute>()
#endif
);
classes
.BindAllInterfaces()
.Configure(c => c.InSingletonScope());
classes.BindToSelf()
.Configure(c => c.InSingletonScope());
});
kernel.Bind<IServiceProvider>().ToConstant(kernel).InSingletonScope(); kernel.Bind<IServiceProvider>().ToConstant(kernel).InSingletonScope();
var services = kernel.GetServices(typeof(INService));
foreach (var s in services)
{
Console.WriteLine(s.GetType().FullName);
}
//initialize Services //initialize Services
Services = kernel; Services = kernel;
Services.GetRequiredService<IBehaviorHandler>().Initialize(); Services.GetRequiredService<IBehaviorHandler>().Initialize();
@@ -348,6 +321,7 @@ public sealed class Bot
{ {
try try
{ {
Console.WriteLine(toExec.GetType().FullName);
await toExec.OnReadyAsync(); await toExec.OnReadyAsync();
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -18,6 +18,7 @@ public sealed record MedusaStats(string Name,
IReadOnlyCollection<SnekStats> Sneks); IReadOnlyCollection<SnekStats> Sneks);
public sealed record SnekStats(string Name, public sealed record SnekStats(string Name,
string? Prefix,
IReadOnlyCollection<SnekCommandStats> Commands); IReadOnlyCollection<SnekCommandStats> Commands);
public sealed record SnekCommandStats(string Name); public sealed record SnekCommandStats(string Name);

View File

@@ -79,7 +79,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
commands.Add(new SnekCommandStats(command.Aliases.First())); commands.Add(new SnekCommandStats(command.Aliases.First()));
} }
sneks.Add(new SnekStats(snekInfos.Name, commands)); sneks.Add(new SnekStats(snekInfos.Name, snekInfos.Instance.Prefix, commands));
} }
toReturn.Add(new MedusaStats(name, resolvedData.Strings.GetDescription(culture), sneks)); toReturn.Add(new MedusaStats(name, resolvedData.Strings.GetDescription(culture), sneks));

View File

@@ -150,9 +150,10 @@ public partial class Medusa : NadekoModule<IMedusaLoaderService>
var cmdCount = found.Sneks.Sum(x => x.Commands.Count); var cmdCount = found.Sneks.Sum(x => x.Commands.Count);
var cmdNames = found.Sneks var cmdNames = found.Sneks
.SelectMany(x => x.Commands) .SelectMany(x => Format.Code(string.IsNullOrWhiteSpace(x.Prefix)
.Select(x => Format.Code(x.Name)) ? x.Name
.Join(" | "); : $"{x.Prefix} {x.Name}"))
.Join("\n");
var eb = _eb.Create(ctx) var eb = _eb.Create(ctx)
.WithOkColor() .WithOkColor()

View File

@@ -36,7 +36,7 @@ public partial class SearchesConfig : ICloneable<SearchesConfig>
- `invidious` - recommended advanced, uses invidious api. Requires at least one invidious instance specified in the `invidiousInstances` property - `invidious` - recommended advanced, uses invidious api. Requires at least one invidious instance specified in the `invidiousInstances` property
""")] """)]
public YoutubeSearcher YtProvider { get; set; } = YoutubeSearcher.Ytdl; public YoutubeSearcher YtProvider { get; set; } = YoutubeSearcher.Ytdlp;
[Comment(""" [Comment("""
Set the searx instance urls in case you want to use 'searx' for either img or web search. Set the searx instance urls in case you want to use 'searx' for either img or web search.

View File

@@ -55,10 +55,13 @@ public class CommandHandler : INService, IReadyExecutor
_prefixes = bot.AllGuildConfigs.Where(x => x.Prefix is not null) _prefixes = bot.AllGuildConfigs.Where(x => x.Prefix is not null)
.ToDictionary(x => x.GuildId, x => x.Prefix) .ToDictionary(x => x.GuildId, x => x.Prefix)
.ToConcurrent(); .ToConcurrent();
Console.WriteLine("Command handler created");
} }
public async Task OnReadyAsync() public async Task OnReadyAsync()
{ {
Log.Information("Command handler runnning on ready");
// clear users on short cooldown every GLOBAL_COMMANDS_COOLDOWN miliseconds // clear users on short cooldown every GLOBAL_COMMANDS_COOLDOWN miliseconds
using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(GLOBAL_COMMANDS_COOLDOWN)); using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(GLOBAL_COMMANDS_COOLDOWN));
while (await timer.WaitForNextTickAsync()) while (await timer.WaitForNextTickAsync())

View File

@@ -8,6 +8,8 @@ using Ninject.Extensions.Conventions;
using StackExchange.Redis; using StackExchange.Redis;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using NadekoBot.Common.ModuleBehaviors;
using Ninject.Infrastructure.Language;
namespace NadekoBot.Extensions; namespace NadekoBot.Extensions;
@@ -125,4 +127,31 @@ public static class ServiceCollectionExtensions
return kernel; return kernel;
} }
public static IKernel AddLifetimeServices(this IKernel kernel)
{
Assembly.GetExecutingAssembly()
.ExportedTypes
.Where(x => x.IsPublic && x.IsClass && !x.IsAbstract)
.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<DontAddToIocContainerAttribute>()
#if GLOBAL_NADEKO
&& !c.HasAttribute<NoPublicBotAttribute>()
#endif
);
/*
classes.BindToSelf()
.Configure(c => c.InSingletonScope());
*/
});
return kernel;
}
} }