Skip to content

Commit 15c1ac8

Browse files
committed
merge dev
2 parents 1317623 + 7186841 commit 15c1ac8

66 files changed

Lines changed: 2234 additions & 345 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Disc.NET.Shared.Configurations;
2+
3+
namespace Disc.NET.Client.SDK
4+
{
5+
public abstract class ClientBase
6+
{
7+
8+
protected readonly HttpClient HttpClient;
9+
protected AppConfiguration AppConfiguration { get; }
10+
11+
12+
protected ClientBase(AppConfiguration appConfiguration, HttpClient client)
13+
{
14+
AppConfiguration = appConfiguration;
15+
HttpClient = client;
16+
HttpClient.BaseAddress = new Uri("https://discord.com/api/v10/");
17+
HttpClient.DefaultRequestHeaders.Authorization =
18+
new System.Net.Http.Headers.AuthenticationHeaderValue("Bot", AppConfiguration.Token);
19+
}
20+
}
21+
}
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\Disc.NET.Shared\Disc.NET.Shared.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Disc.NET.Client.SDK.Messages;
2+
3+
namespace Disc.NET.Client.SDK.Interfaces;
4+
5+
public interface IClient
6+
{
7+
Task SendMessageAsync(string channelId,ApiMessage message, CancellationToken cancellation = default);
8+
9+
// Using ApiMessage temporarily, will create a specific class for receiving messages later
10+
Task<ApiMessage?> GetMessageAsync(string channelId, string messageId, CancellationToken cancellation = default);
11+
12+
Task RegisterGlobalSlashCommandAsync(string commandJson, CancellationToken cancellation = default);
13+
Task RegisterGuildSlashCommandAsync(string commandJson, string guildId, CancellationToken cancellation = default);
14+
15+
Task InteractionRespondingAsync(string interactionId, string interactionToken, string responseJson, CancellationToken cancellation = default);
16+
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text.Json.Serialization;
2+
using Disc.NET.Client.SDK.Messages.Embeds;
3+
using Disc.NET.Shared.Enums;
4+
5+
namespace Disc.NET.Client.SDK.Messages
6+
{
7+
public class ApiMessage
8+
{
9+
public string Content { get; set; } = string.Empty;
10+
public List<Embed> Embeds { get; set; } = [];
11+
12+
[JsonIgnore]
13+
public List<MessageFlag>? MessageFlags { get; set; }
14+
15+
public long Flags => MessageFlags?.Aggregate(0L, (current, flag) => current | (long)flag) ?? 0L;
16+
17+
18+
// Colocar os outros campos depois
19+
// https://discord.com/developers/docs/resources/message#message-object
20+
}
21+
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Disc.NET.Client.SDK.Messages.Embeds
2+
{
3+
public class Embed
4+
{
5+
public string? Title { get; set; }
6+
public string? Type { get; set; }
7+
public string? Description { get; set; }
8+
public string? Url { get; set; }
9+
public string? Timestamp { get; set; }
10+
public int? Color { get; set; }
11+
public EmbedFooter? Footer { get; set; }
12+
public EmbedImage? Image { get; set; }
13+
public EmbedImage? Thumbnail { get; set; }
14+
15+
public EmbedVideo? Video { get; set; }
16+
17+
public EmbedProvider? Provider { get; set; }
18+
public EmbedAuthor? Author { get; set; }
19+
public List<EmbedField>? Fields { get; set; }
20+
21+
}
22+
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Disc.NET.Client.SDK.Messages.Embeds
2+
{
3+
public class EmbedAuthor
4+
{
5+
public string? Name { get; set; }
6+
public string? Url { get; set; }
7+
public string? IconUrl { get; set; }
8+
public string? ProxyIconUrl { get; set; }
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Disc.NET.Client.SDK.Messages.Embeds
2+
{
3+
public class EmbedField
4+
{
5+
public string Name { get; set; } = string.Empty;
6+
public string Value { get; set; } = string.Empty;
7+
public bool Inline { get; set; } = false;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Disc.NET.Client.SDK.Messages.Embeds
2+
{
3+
public class EmbedFooter
4+
{
5+
public string Text { get; set; } = string.Empty;
6+
public string? IconUrl { get; set; }
7+
public string? ProxyIconUrl { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)