Skip to content

Commit 1fd7ce4

Browse files
committed
dependency injection
1 parent 3b13b7f commit 1fd7ce4

24 files changed

Lines changed: 222 additions & 195 deletions

src/Disc.NET.Client.SDK/ClientBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ namespace Disc.NET.Client.SDK
44
{
55
public abstract class ClientBase
66
{
7+
78
protected readonly HttpClient HttpClient;
89
protected AppConfiguration AppConfiguration { get; }
9-
10+
1011

1112
protected ClientBase(AppConfiguration appConfiguration, HttpClient client)
1213
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Disc.NET.Client.SDK.Interfaces;
2+
using Disc.NET.Shared.Configurations;
3+
4+
namespace Disc.NET.Client.SDK
5+
{
6+
public sealed class ClientSingleton
7+
{
8+
private static IClient? _instance;
9+
10+
public static IClient GetInstance(AppConfiguration appConfiguration)
11+
{
12+
if (_instance == null)
13+
_instance = new Client(appConfiguration, new HttpClient());
14+
15+
return _instance;
16+
}
17+
18+
}
19+
}

src/Disc.NET.Commands/CommandBase.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Disc.NET.Commands/Contexts/CommandContext.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Disc.NET.Commands.Contexts.Models;
1+
using Disc.NET.Client.SDK;
2+
using Disc.NET.Client.SDK.Interfaces;
3+
using Disc.NET.Client.SDK.Messages;
4+
using Disc.NET.Commands.Contexts.Models;
5+
using Disc.NET.Shared.Configurations;
26

37
namespace Disc.NET.Commands.Contexts
48
{
@@ -15,5 +19,18 @@ public class CommandContext : IContext
1519
public DateTime? Timestamp { get; set; }
1620
public int Type { get; set; }
1721
public DateTime? EditedTimestamp { get; set; }
22+
23+
public CommandResponse Response { get; set; }
24+
}
25+
26+
public class CommandResponse(AppConfiguration appConfiguration)
27+
{
28+
private readonly IClient _client = ClientSingleton.GetInstance(appConfiguration);
29+
public string ChannelId { get; set; } = string.Empty;
30+
31+
public async Task SendMessageAsync(ApiMessage message, CancellationToken cancellation = default)
32+
{
33+
await _client.SendMessageAsync(ChannelId, message, cancellation);
34+
}
1835
}
1936
}

src/Disc.NET.Commands/Contexts/IContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ namespace Disc.NET.Commands.Contexts
88
{
99
public interface IContext
1010
{
11+
1112
}
1213
}

src/Disc.NET.Commands/Contexts/InteractionContext.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using Disc.NET.Client.SDK;
8+
using Disc.NET.Client.SDK.Interfaces;
9+
using Disc.NET.Client.SDK.Messages;
10+
using Disc.NET.Shared.Configurations;
711

812
namespace Disc.NET.Commands.Contexts
913
{
@@ -12,8 +16,22 @@ public class InteractionContext : IContext
1216
public Member? Member { get; set; }
1317
public string Id { get; set; } = string.Empty;
1418
public string GuildId { get; set; } = string.Empty;
15-
public Channel? Channel { get; set; }
19+
public Channel? Channel { get; set; }
1620
public int Type { get; set; }
1721
public int Context { get; set; }
22+
23+
public InteractionResponse Response { get; set; }
1824
}
25+
26+
public class InteractionResponse(AppConfiguration appConfiguration)
27+
{
28+
private readonly IClient _client = ClientSingleton.GetInstance(appConfiguration);
29+
public string ChannelId { get; set; } = string.Empty;
30+
31+
public async Task SendMessageAsync(ApiMessage message, CancellationToken cancellation = default)
32+
{
33+
await _client.SendMessageAsync(ChannelId, message, cancellation);
34+
}
35+
}
36+
1937
}

src/Disc.NET.Commands/ISlashCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Disc.NET.Commands
1010
{
1111
public interface ISlashCommand : ICommand<InteractionContext>
1212
{
13-
Task<bool> RunAsync(InteractionContext context, SlashCommandParamsResult paramsResult);
13+
Task<bool> RunAsync(InteractionContext context, SlashCommandParamsResult @params);
1414
List<SlashCommandOptions> BuildOptions() => [];
1515

1616
}

src/Disc.NET.Shared/Configurations/AppConfiguration.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Disc.NET.Shared.Enums;
1+
using Autofac;
2+
using Disc.NET.Shared.Enums;
3+
24

35
namespace Disc.NET.Shared.Configurations
46
{
@@ -7,7 +9,7 @@ public class AppConfiguration
79
public string Token { get; }
810
public char BotPrefix { get; init; }
911
public required long ApplicationId { get; init; }
10-
12+
public bool UseContainer { get; set; }
1113
public List<GatewayIntent> Intents { get; init; } = new()
1214
{
1315
GatewayIntent.GUILD_MESSAGES,
@@ -18,5 +20,6 @@ public AppConfiguration(string token, char botPrefix = '!')
1820
BotPrefix = botPrefix;
1921
Token = token;
2022
}
23+
2124
}
2225
}

src/Disc.NET.Shared/Disc.NET.Shared.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Include="Autofac" Version="9.0.0" />
11+
</ItemGroup>
12+
913
</Project>

src/Disc.NET/App.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.ComponentModel;
1+
using Disc.NET.Configuration;
22
using Disc.NET.Gateway;
33
using Disc.NET.Shared.Configurations;
44
using Microsoft.Extensions.Logging;
@@ -8,14 +8,17 @@ namespace Disc.NET
88
public class App
99
{
1010
private ILogger<GatewayConnection> _logger;
11-
1211
public App()
1312
{
1413
_logger = CreateLogger(LogLevel.Information);
1514
}
1615

1716
public async Task RunAsync(AppConfiguration configuration)
1817
{
18+
if (configuration.UseContainer)
19+
{
20+
DiscNetContainer.GetInstance().RegisterDependencies();
21+
}
1922
var gateway = new GatewayConnection(_logger);
2023
await gateway.ConnectAsync(configuration);
2124
}
@@ -36,5 +39,13 @@ private ILogger<GatewayConnection> CreateLogger(LogLevel level)
3639

3740
return loggerFactory.CreateLogger<GatewayConnection>();
3841
}
42+
43+
public DiscNetContainer UseDependencyInjection(AppConfiguration appConfiguration)
44+
{
45+
appConfiguration.UseContainer = true;
46+
return DiscNetContainer.GetInstance();
47+
}
48+
3949
}
50+
4051
}

0 commit comments

Comments
 (0)