public DiscordSlashClient(DiscordSlashConfiguration config)
        {
            IServiceCollection internalServices = new ServiceCollection();

            internalServices.AddSingleton <SlashCommandHandlingService>()
            .AddSingleton <HttpClient>();

            this._config  = config;
            this._discord = this._config.Client;
            this._sharded = this._config.ShardedClient;

            if (config.Logger is not null)
            {
                internalServices.AddSingleton(config.Logger);
            }
            else
            {
                if (_discord is not null)
                {
                    internalServices.AddSingleton(_discord.Logger);
                }
                else if (_sharded is not null)
                {
                    internalServices.AddSingleton(_sharded !.Logger);
                }
            }

            this._services         = config.Services ?? new ServiceCollection().BuildServiceProvider();
            this._internalServices = internalServices.BuildServiceProvider();
            this._logger           = this._internalServices.GetRequiredService <ILogger>();
            this._http             = this._internalServices.GetRequiredService <HttpClient>();
            this._slash            = new SlashCommandHandlingService(this._services, this._http, this._logger);
            this._http.DefaultRequestHeaders.Authorization = new("Bot", this._config.Token);

            if (this._discord is null && this._sharded is null)
            {
                throw new Exception("A Discord Client or Sharded Client is required.");
            }
        }
Пример #2
0
        public DiscordSlashClient(DiscordSlashConfiguration config, IServiceCollection?collection = null)
        {
            IServiceCollection services = new ServiceCollection();

            services.AddSingleton <SlashCommandHandlingService>()
            .AddSingleton <HttpClient>()
            .AddLogging(o => o.AddConsole());

            if (collection is not null)
            {
                foreach (var c in collection)
                {
                    services.Add(c);
                }
            }

            this._services = services.BuildServiceProvider();

            this._logger = this._services.GetRequiredService <ILogger <DiscordSlashClient> >();

            this._slash = this._services.GetRequiredService <SlashCommandHandlingService>();

            this._config = config;

            this._http = this._services.GetRequiredService <HttpClient>();

            this._http.DefaultRequestHeaders.Authorization = new("Bot", this._config.Token);

            this._discord = this._config.Client;
            this._sharded = this._config.ShardedClient;

            if (this._discord is null && this._sharded is null)
            {
                throw new Exception("A Discord Client or Sharded Client is required.");
            }
        }