Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Inject current ServiceProvider into DiscordClient
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Mar 11, 2022
1 parent ccdaca7 commit c4f1fd1
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions BrackeysBot/Plugins/SimplePluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ public Plugin LoadPlugin(string name)
jsonFileConfiguration.SaveDefault();
plugin.Configuration = jsonFileConfiguration;


var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddNLog();
});

serviceCollection.AddSingleton(this);
serviceCollection.AddSingleton(plugin);
serviceCollection.AddSingleton(plugin.Configuration);

var token = plugin.Configuration.Get<string>("discord.token");
if (string.IsNullOrWhiteSpace(token))
{
Expand All @@ -248,28 +260,21 @@ public Plugin LoadPlugin(string name)
if (intentsAttribute is not null)
intents = intentsAttribute.Intents;

plugin.DiscordClient ??= new DiscordClient(new DiscordConfiguration
serviceCollection.AddSingleton(provider =>
{
Intents = intents,
LoggerFactory = new NLogLoggerFactory(),
Token = token
var client = new DiscordClient(new DiscordConfiguration
{
Intents = intents,
LoggerFactory = new NLogLoggerFactory(),
ServiceProvider = provider,
Token = token
});
plugin.DiscordClient = client;
return client;
});
}

var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddNLog();
});

serviceCollection.AddSingleton(this);
serviceCollection.AddSingleton(plugin);
serviceCollection.AddSingleton(plugin.Configuration);

if (plugin.DiscordClient is not null)
serviceCollection.AddSingleton(plugin.DiscordClient);

plugin.ConfigureServices(serviceCollection);
plugin.ServiceProvider = serviceCollection.BuildServiceProvider();

Expand Down

0 comments on commit c4f1fd1

Please sign in to comment.