1+ using Disc . NET . Client . SDK . Interfaces ;
2+ using Disc . NET . Client . SDK . Messages ;
3+ using Disc . NET . Shared . Configurations ;
4+ using Disc . NET . Shared . Exceptions ;
5+ using Disc . NET . Shared . Serializer ;
6+ using System ;
7+ using System . Net ;
8+ using System . Text ;
9+ using System . Text . Json ;
10+
11+ namespace Disc . NET . Client . SDK ;
12+
13+ public class Client : ClientBase , IClient
14+ {
15+ private readonly AppConfiguration _appConfiguration ;
16+ public Client ( AppConfiguration appConfiguration , HttpClient client ) : base ( appConfiguration , client )
17+ {
18+ _appConfiguration = appConfiguration ;
19+ }
20+
21+ public async Task SendMessageAsync ( string channelId , ApiMessage message , CancellationToken cancellation = default )
22+ {
23+ var serializer = DiscNetSerializer . GetInstance ( ) ;
24+ var json = serializer . Serialize ( message ) ;
25+ var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
26+ var response = await HttpClient . PostAsync ( $ "channels/{ channelId } /messages", content , cancellation ) . ConfigureAwait ( false ) ;
27+ if ( ! response . IsSuccessStatusCode )
28+ {
29+ var error = await response . Content . ReadAsStringAsync ( cancellation ) . ConfigureAwait ( false ) ;
30+ throw new DiscNetClientSdkException ( error , response . StatusCode ) ;
31+ }
32+ }
33+
34+ public async Task < ApiMessage ? > GetMessageAsync ( string channelId , string messageId , CancellationToken cancellation = default )
35+ {
36+ var serializer = DiscNetSerializer . GetInstance ( ) ;
37+
38+ var response = await HttpClient . GetAsync ( $ "channels/{ channelId } /messages/{ messageId } ", cancellation )
39+ . ConfigureAwait ( false ) ;
40+ if ( ! response . IsSuccessStatusCode )
41+ {
42+ var error = await response . Content . ReadAsStringAsync ( cancellation ) . ConfigureAwait ( false ) ;
43+ throw new DiscNetClientSdkException ( error , response . StatusCode ) ;
44+ }
45+ var content = await response . Content . ReadAsStreamAsync ( cancellation ) . ConfigureAwait ( false ) ;
46+ return await serializer . DeserializeAsync < ApiMessage > ( content , cancellation ) . ConfigureAwait ( false ) ;
47+ }
48+
49+ public async Task RegisterGlobalSlashCommandAsync ( string commandJson , CancellationToken cancellation = default )
50+ {
51+ await PostAsync ( commandJson , $ "applications/{ _appConfiguration . ApplicationId } /commands",
52+ cancellation ) ;
53+ }
54+
55+ public async Task RegisterGuildSlashCommandAsync ( string commandJson , string guildId , CancellationToken cancellation = default )
56+ {
57+ await PostAsync ( commandJson , $ "applications/{ _appConfiguration . ApplicationId } /guilds/{ guildId } /commands",
58+ cancellation ) ;
59+ }
60+
61+ public async Task InteractionRespondingAsync ( string interactionId , string interactionToken , string responseJson ,
62+ CancellationToken cancellation = default )
63+ {
64+ var url = $ "https://discord.com/api/v10/interactions/{ interactionId } /{ interactionToken } /callback";
65+ await PostAsync ( responseJson , url , cancellation ) ;
66+ }
67+
68+
69+
70+ private async Task PostAsync ( string json , string uri , CancellationToken cancellation = default )
71+ {
72+ var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
73+
74+ var response = await HttpClient . PostAsync ( uri , content , CancellationToken . None ) . ConfigureAwait ( false ) ;
75+ if ( ! response . IsSuccessStatusCode )
76+ {
77+ var error = await response . Content . ReadAsStringAsync ( cancellation ) . ConfigureAwait ( false ) ;
78+ throw new DiscNetClientSdkException ( error , response . StatusCode ) ;
79+ }
80+ }
81+ }
0 commit comments