Don't load executor behaviors as a dep, but initialize after all services are loaded. Experimenting on services

This commit is contained in:
Kwoth
2021-07-01 23:53:06 +02:00
parent 3c82c1f919
commit 9f34f8f00f
5 changed files with 35 additions and 94 deletions

View File

@@ -3,7 +3,6 @@ using Discord.Commands;
using Discord.Net;
using Discord.WebSocket;
using NadekoBot.Common.Collections;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Extensions;
using System;
using System.Collections.Concurrent;
@@ -12,7 +11,6 @@ using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NadekoBot.Common.Configs;
using NadekoBot.Db;
using Serilog;
@@ -28,9 +26,9 @@ namespace NadekoBot.Services
private readonly BotConfigService _bss;
private readonly Bot _bot;
private readonly IBehaviourExecutor _behaviourExecutor;
private IServiceProvider _services;
private ConcurrentDictionary<ulong, string> _prefixes { get; } = new ConcurrentDictionary<ulong, string>();
private readonly IServiceProvider _services;
private readonly ConcurrentDictionary<ulong, string> _prefixes;
public event Func<IUserMessage, CommandInfo, Task> CommandExecuted = delegate { return Task.CompletedTask; };
public event Func<CommandInfo, ITextChannel, string, Task> CommandErrored = delegate { return Task.CompletedTask; };
@@ -106,7 +104,8 @@ namespace NadekoBot.Services
gc.Prefix = prefix;
uow.SaveChanges();
}
_prefixes.AddOrUpdate(guild.Id, prefix, (key, old) => prefix);
_prefixes[guild.Id] = prefix;
return prefix;
}
@@ -179,8 +178,8 @@ namespace NadekoBot.Services
"Message: {3}\n\t" +
"Error: {4}",
usrMsg.Author + " [" + usrMsg.Author.Id + "]", // {0}
(channel is null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]"), // {1}
(channel is null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), // {2}
channel is null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]", // {1}
channel is null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]", // {2}
usrMsg.Content,// {3}
errorMessage
//exec.Result.ErrorReason // {4}

View File

@@ -2,6 +2,7 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
namespace NadekoBot.Services
{
@@ -11,5 +12,7 @@ namespace NadekoBot.Services
public Task<string> RunInputTransformersAsync(SocketGuild guild, IUserMessage usrMsg);
Task<bool> RunLateBlockersAsync(ICommandContext context, CommandInfo cmd);
Task RunLateExecutorsAsync(SocketGuild guild, IUserMessage usrMsg);
public void Initialize();
}
}

View File

@@ -7,29 +7,29 @@ using Discord.WebSocket;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Extensions;
using Serilog;
using Microsoft.Extensions.DependencyInjection;
namespace NadekoBot.Services
{
public sealed class BehaviorExecutor : IBehaviourExecutor
{
private readonly DiscordSocketClient _client;
private readonly IEnumerable<ILateExecutor> _lateExecutors;
private readonly IEnumerable<ILateBlocker> _lateBlockers;
private readonly IEnumerable<IEarlyBehavior> _earlyBehaviors;
private readonly IEnumerable<IInputTransformer> _transformers;
private readonly IServiceProvider _services;
private IEnumerable<ILateExecutor> _lateExecutors;
private IEnumerable<ILateBlocker> _lateBlockers;
private IEnumerable<IEarlyBehavior> _earlyBehaviors;
private IEnumerable<IInputTransformer> _transformers;
public BehaviorExecutor(
DiscordSocketClient client,
IEnumerable<ILateExecutor> lateExecutors,
IEnumerable<ILateBlocker> lateBlockers,
IEnumerable<IEarlyBehavior> earlyBehaviors,
IEnumerable<IInputTransformer> transformers)
public BehaviorExecutor(IServiceProvider services)
{
_client = client;
_lateExecutors = lateExecutors;
_lateBlockers = lateBlockers;
_earlyBehaviors = earlyBehaviors;
_transformers = transformers;
_services = services;
}
public void Initialize()
{
_lateExecutors = _services.GetServices<ILateExecutor>();
_lateBlockers = _services.GetServices<ILateBlocker>();
_earlyBehaviors = _services.GetServices<IEarlyBehavior>();
_transformers = _services.GetServices<IInputTransformer>();
}
// todo early behaviors should print for themselves