This commit is contained in:
sam 2024-09-03 00:07:12 +02:00
commit b3bf3a7c16
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
43 changed files with 2057 additions and 0 deletions

14
Foxcord/Foxcord.csproj Normal file
View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Polly.Core" Version="8.4.1" />
<PackageReference Include="Serilog" Version="4.0.1" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,53 @@
using System.Text.Json;
using Foxcord.Gateway.Events.Dispatch;
using Foxcord.Rest;
using Foxcord.Rest.Types;
namespace Foxcord.Gateway;
public partial class DiscordGatewayClient
{
private IDispatch ParseDispatchEvent(string rawType, JsonElement rawPayload)
{
switch (rawType)
{
case DispatchEventTypeName.Ready:
return rawPayload.Deserialize<ReadyEvent>(_jsonSerializerOptions)!;
case DispatchEventTypeName.GuildCreate:
return rawPayload.Deserialize<GuildCreateEvent>(_jsonSerializerOptions)!;
case DispatchEventTypeName.MessageCreate:
return rawPayload.Deserialize<MessageCreateEvent>(_jsonSerializerOptions)!;
default:
throw new ArgumentOutOfRangeException(nameof(rawType), $"Unknown dispatch event '{rawType}'");
}
}
private async Task HandleDispatch(IDispatch dispatch, CancellationToken ct = default)
{
switch (dispatch)
{
case ReadyEvent ready:
_logger.Debug("Received READY! API version: {Version}, user: {UserId}, shard: {Id}/{Total}",
ready.Version, ready.User.Id, ready.Shard.ShardId, ready.Shard.NumShards);
break;
case GuildCreateEvent guildCreate:
_logger.Debug("Received guild create for guild {Id} / {Name}", guildCreate.Id, guildCreate.Name);
break;
case MessageCreateEvent m:
_logger.Debug("Received message create from {User} in {Channel}. Content: {Content}", m.Author.Tag,
m.ChannelId, m.Content);
if (m.Content == "!ping")
{
var rest = new DiscordRestClient(_logger,
new DiscordRestClientOptions { Token = _token["Bot ".Length..] });
await rest.CreateMessageAsync(m.ChannelId,
new CreateMessageParams(Content: $"Pong! Latency: {Latency.TotalMilliseconds}ms"), ct);
}
break;
default:
_logger.Debug("Received dispatch event {DispatchType}", dispatch.GetType().Name);
break;
}
}
}

View file

@ -0,0 +1,17 @@
namespace Foxcord.Gateway;
public partial class DiscordGatewayClient
{
private void HandleHeartbeatAck()
{
_lastHeartbeatAck = DateTimeOffset.UtcNow;
_logger.Verbose("Received heartbeat ACK after {Latency}", _lastHeartbeatAck - _lastHeartbeatSend);
}
private async Task HandleHeartbeatRequest(CancellationToken ct = default)
{
_logger.Information("Early heartbeat requested, sending heartbeat");
await WritePacket(new GatewayPacket { Opcode = GatewayOpcode.Heartbeat, Payload = _lastSequence }, ct);
_lastHeartbeatSend = DateTimeOffset.UtcNow;
}
}

View file

@ -0,0 +1,231 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Net.WebSockets;
using System.Text.Json;
using Foxcord.Gateway.Events;
using Foxcord.Serialization;
using Serilog;
namespace Foxcord.Gateway;
public partial class DiscordGatewayClient
{
private const int GatewayVersion = 10;
private const int BufferSize = 64 * 1024;
private readonly ILogger _logger;
private ClientWebSocket? _ws;
private readonly string _token;
private readonly Uri _gatewayUri;
private readonly GatewayIntent _intents;
private readonly JsonSerializerOptions _jsonSerializerOptions = JsonSerializerExtensions.CreateSerializer();
private long? _lastSequence = null;
private DateTimeOffset _lastHeartbeatSend = DateTimeOffset.UnixEpoch;
private DateTimeOffset _lastHeartbeatAck = DateTimeOffset.UnixEpoch;
public TimeSpan Latency => _lastHeartbeatAck - _lastHeartbeatSend;
public DiscordGatewayClient(ILogger logger, DiscordGatewayClientOptions opts)
{
_logger = logger.ForContext<DiscordGatewayClient>();
_token = $"Bot {opts.Token}";
var uriBuilder = new UriBuilder(opts.Uri)
{
Query = $"v={GatewayVersion}&encoding=json"
};
_gatewayUri = uriBuilder.Uri;
_intents = opts.Intents;
}
public ConnectionStatus Status { get; private set; } = ConnectionStatus.Dead;
/// <summary>
/// Connects to the gateway. This method returns after a connection is established.
/// <c>ct</c> is stored by the client and cancelling it will close the connection.
/// The caller must pause indefinitely or the bot will shut down immediately.
/// </summary>
[MemberNotNull("_ws")]
public async Task ConnectAsync(CancellationToken ct = default)
{
if (Status != ConnectionStatus.Dead)
throw new DiscordGatewayRequestError(
"Gateway is connecting or connected, only one concurrent connection allowed.");
try
{
_ws = new ClientWebSocket();
Status = ConnectionStatus.Connecting;
await _ws.ConnectAsync(_gatewayUri, ct);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
cts.CancelAfter(TimeSpan.FromSeconds(30));
var (rawHelloPacketType, rawHelloPacket) = await ReadPacket(cts.Token);
if (rawHelloPacketType == WebSocketMessageType.Close)
throw new DiscordGatewayRequestError("First packet received was a close message");
if (!TryDeserializeEvent(rawHelloPacket, out var rawHelloEvent))
throw new DiscordGatewayRequestError("First packet was not a valid event");
if (rawHelloEvent is not HelloEvent hello)
throw new DiscordGatewayRequestError("First event was not a HELLO event");
_logger.Debug("Received HELLO, heartbeat interval is {HeartbeatInterval}", hello.HeartbeatInterval);
var _ = HeartbeatLoopAsync(hello.HeartbeatInterval, ct);
var __ = ReceiveLoopAsync(ct);
_logger.Debug("Sending IDENTIFY");
await WritePacket(new GatewayPacket
{
Opcode = GatewayOpcode.Identify,
Payload = new IdentifyEvent { Token = _token, Intents = _intents }
}, ct);
}
catch (Exception e)
{
_logger.Error(e, "Error connecting to gateway");
Status = ConnectionStatus.Dead;
_ws = null;
throw;
}
}
private async Task HeartbeatLoopAsync(int heartbeatInterval, CancellationToken ct = default)
{
var delay = TimeSpan.FromMilliseconds(heartbeatInterval * Random.Shared.NextDouble());
_logger.Debug("Waiting {Delay} before sending first heartbeat", delay);
using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(heartbeatInterval));
while (await timer.WaitForNextTickAsync(ct))
{
_logger.Debug("Sending heartbeat with sequence {Sequence}", _lastSequence);
_lastHeartbeatSend = DateTimeOffset.UtcNow;
await WritePacket(new GatewayPacket { Opcode = GatewayOpcode.Heartbeat, Payload = _lastSequence }, ct);
}
}
private async Task ReceiveLoopAsync(CancellationToken ct = default)
{
while (!ct.IsCancellationRequested)
{
try
{
var (type, packet) = await ReadPacket(ct);
if (type == WebSocketMessageType.Close || packet == null)
{
// TODO: close websocket
return;
}
_ = ReceiveAsync(packet, ct);
}
catch (Exception e)
{
_logger.Error(e, "Error while receiving data");
}
}
async Task ReceiveAsync(GatewayPacket packet, CancellationToken ct2 = default)
{
if (!TryDeserializeEvent(packet, out var gatewayEvent))
{
_logger.Debug("Event {EventType} didn't have payload", packet.Opcode);
return;
}
switch (gatewayEvent)
{
case HeartbeatEvent:
await HandleHeartbeatRequest(ct2);
break;
case HeartbeatAckEvent:
HandleHeartbeatAck();
break;
case DispatchEvent dispatch:
await HandleDispatch(dispatch.Payload, ct2);
break;
}
}
}
private async ValueTask WritePacket(GatewayPacket packet, CancellationToken ct = default)
{
using var buf = MemoryPool<byte>.Shared.Rent(BufferSize);
var json = JsonSerializer.SerializeToUtf8Bytes(packet, _jsonSerializerOptions);
await _ws!.SendAsync(json.AsMemory(), WebSocketMessageType.Text, true, ct);
}
private async ValueTask<(WebSocketMessageType type, GatewayPacket? packet)> ReadPacket(
CancellationToken ct = default)
{
using var buf = MemoryPool<byte>.Shared.Rent(BufferSize);
var res = await _ws!.ReceiveAsync(buf.Memory, ct);
if (res.MessageType == WebSocketMessageType.Close) return (res.MessageType, null);
if (res.EndOfMessage)
return DeserializePacket(res, buf.Memory.Span[..res.Count]);
return await DeserializeMultipleBuffer(res, buf);
}
private async Task<(WebSocketMessageType type, GatewayPacket packet)> DeserializeMultipleBuffer(
ValueWebSocketReceiveResult res, IMemoryOwner<byte> buf)
{
await using var stream = new MemoryStream(BufferSize * 4);
stream.Write(buf.Memory.Span.Slice(0, res.Count));
while (!res.EndOfMessage)
{
res = await _ws!.ReceiveAsync(buf.Memory, default);
stream.Write(buf.Memory.Span.Slice(0, res.Count));
}
return DeserializePacket(res, stream.GetBuffer().AsSpan(0, (int)stream.Length));
}
private (WebSocketMessageType type, GatewayPacket packet) DeserializePacket(
ValueWebSocketReceiveResult res, Span<byte> span) => (res.MessageType,
JsonSerializer.Deserialize<GatewayPacket>(span, _jsonSerializerOptions)!);
private bool TryDeserializeEvent(GatewayPacket? packet, [NotNullWhen(true)] out IGatewayEvent? gatewayEvent)
{
gatewayEvent = null;
if (packet == null) return false;
var payload = packet.Payload is JsonElement element ? element : default;
switch (packet.Opcode)
{
case GatewayOpcode.Hello:
gatewayEvent = payload.Deserialize<HelloEvent>(_jsonSerializerOptions)!;
break;
case GatewayOpcode.Dispatch:
_lastSequence = packet.Sequence;
gatewayEvent = new DispatchEvent { Payload = ParseDispatchEvent(packet.EventType!, payload) };
break;
case GatewayOpcode.Heartbeat:
gatewayEvent = new HeartbeatEvent();
break;
case GatewayOpcode.Reconnect:
case GatewayOpcode.InvalidSession:
throw new NotImplementedException();
case GatewayOpcode.HeartbeatAck:
gatewayEvent = new HeartbeatAckEvent();
break;
default:
throw new ArgumentOutOfRangeException();
}
return true;
}
public enum ConnectionStatus
{
Dead,
Connecting,
Connected
}
}
public class DiscordGatewayClientOptions
{
public required string Token { get; init; }
public required string Uri { get; init; }
public required GatewayIntent Intents { get; init; }
}

View file

@ -0,0 +1,3 @@
namespace Foxcord.Gateway;
public class DiscordGatewayRequestError(string message) : Exception(message);

View file

@ -0,0 +1,9 @@
using Foxcord.Models;
namespace Foxcord.Gateway.Events.Dispatch;
public class GuildCreateEvent : Guild, IDispatch
{
public bool Unavailable { get; init; } = false;
public int MemberCount { get; init; }
}

View file

@ -0,0 +1,13 @@
namespace Foxcord.Gateway.Events.Dispatch;
public interface IDispatch
{
}
public static class DispatchEventTypeName
{
public const string Ready = "READY";
public const string GuildCreate = "GUILD_CREATE";
public const string MessageCreate = "MESSAGE_CREATE";
}

View file

@ -0,0 +1,8 @@
using Foxcord.Models;
namespace Foxcord.Gateway.Events.Dispatch;
public class MessageCreateEvent : Message, IDispatch
{
}

View file

@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
using Foxcord.Models;
namespace Foxcord.Gateway.Events.Dispatch;
public class ReadyEvent : IDispatch
{
[JsonPropertyName("v")] public int Version { get; init; }
public required User User { get; init; }
public string? SessionId { get; init; }
public string? ResumeGatewayUrl { get; init; }
[JsonPropertyName("shard")] public int[]? RawShard { private get; init; }
[JsonIgnore] public ShardInfo Shard => new(RawShard?[0] ?? 0, RawShard?[1] ?? 1);
[JsonIgnore] public bool CanResume => !string.IsNullOrEmpty(SessionId) && !string.IsNullOrEmpty(ResumeGatewayUrl);
}
public record ShardInfo(int ShardId, int NumShards);

View file

@ -0,0 +1,8 @@
using Foxcord.Gateway.Events.Dispatch;
namespace Foxcord.Gateway.Events;
internal class DispatchEvent : IGatewayEvent
{
public required IDispatch Payload { get; init; }
}

View file

@ -0,0 +1,5 @@
namespace Foxcord.Gateway.Events;
internal class HeartbeatEvent : IGatewayEvent;
internal class HeartbeatAckEvent : IGatewayEvent;

View file

@ -0,0 +1,6 @@
namespace Foxcord.Gateway.Events;
internal class HelloEvent : IGatewayEvent
{
public int HeartbeatInterval { get; init; }
}

View file

@ -0,0 +1,6 @@
namespace Foxcord.Gateway.Events;
internal interface IGatewayEvent
{
}

View file

@ -0,0 +1,15 @@
namespace Foxcord.Gateway.Events;
public class IdentifyEvent : IGatewayEvent
{
public required string Token { get; init; }
public IdentifyProperties Properties { get; init; } = new();
public GatewayIntent Intents { get; init; }
}
public class IdentifyProperties
{
public string Os { get; init; } = "Linux";
public string Browser { get; init; } = "Foxcord";
public string Device { get; init; } = "Foxcord";
}

View file

@ -0,0 +1,22 @@
namespace Foxcord.Gateway;
[Flags]
public enum GatewayIntent
{
Guilds = 1 << 0,
GuildMembers = 1 << 1,
GuildBans = 1 << 2,
GuildEmojis = 1 << 3,
GuildIntegrations = 1 << 4,
GuildWebhooks = 1 << 5,
GuildInvites = 1 << 6,
GuildVoiceStates = 1 << 7,
GuildPresences = 1 << 8,
GuildMessages = 1 << 9,
GuildMessageReactions = 1 << 10,
GuildMessageTyping = 1 << 11,
DirectMessages = 1 << 12,
DirectMessageReactions = 1 << 13,
DirectMessageTyping = 1 << 14,
MessageContent = 1 << 15,
}

View file

@ -0,0 +1,33 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Foxcord.Gateway;
public record GatewayPacket
{
[JsonPropertyName("op")] public GatewayOpcode Opcode { get; init; }
[JsonPropertyName("d")] public object? Payload { get; init; }
[JsonPropertyName("s")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? Sequence { get; init; }
[JsonPropertyName("t")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? EventType { get; init; }
}
public enum GatewayOpcode
{
Dispatch = 0,
Heartbeat = 1,
Identify = 2,
PresenceUpdate = 3,
VoiceStateUpdate = 4,
Resume = 6,
Reconnect = 7,
RequestGuildMembers = 8,
InvalidSession = 9,
Hello = 10,
HeartbeatAck = 11
}

View file

@ -0,0 +1,8 @@
using Foxcord.Models;
namespace Foxcord.Gateway.Types;
public class UnavailableGuild
{
public Snowflake Id { get; init; }
}

7
Foxcord/Models/Guild.cs Normal file
View file

@ -0,0 +1,7 @@
namespace Foxcord.Models;
public class Guild
{
public Snowflake Id { get; init; }
public string Name { get; init; } = string.Empty;
}

View file

@ -0,0 +1,9 @@
namespace Foxcord.Models;
public class Message
{
public Snowflake Id { get; init; }
public Snowflake ChannelId { get; init; }
public string Content { get; init; } = string.Empty;
public required User Author { get; init; }
}

View file

@ -0,0 +1,80 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Foxcord.Models;
[JsonConverter(typeof(JsonConverter))]
public readonly struct Snowflake(ulong value) : IEquatable<Snowflake>
{
public const long Epoch = 1_420_070_400_000; // 2015-01-01 at 00:00:00 UTC
public readonly ulong Value = value;
/// <summary>
/// The time this snowflake was created.
/// </summary>
public DateTimeOffset Time => DateTimeOffset.FromUnixTimeMilliseconds(Timestamp);
/// <summary>
/// The Unix timestamp embedded in this snowflake, in milliseconds.
/// </summary>
public long Timestamp => (long)((Value >> 22) + Epoch);
/// <summary>
/// The worker ID embedded in this snowflake.
/// </summary>
public byte WorkerId => (byte)((Value & 0x3E0000) >> 17);
/// <summary>
/// The process ID embedded in this snowflake.
/// </summary>
public byte ProcessId => (byte)((Value & 0x1F000) >> 12);
/// <summary>
/// The increment embedded in this snowflake.
/// </summary>
public short Increment => (short)(Value & 0xFFF);
public static bool operator <(Snowflake arg1, Snowflake arg2) => arg1.Value < arg2.Value;
public static bool operator >(Snowflake arg1, Snowflake arg2) => arg1.Value > arg2.Value;
public static bool operator ==(Snowflake arg1, Snowflake arg2) => arg1.Value == arg2.Value;
public static bool operator !=(Snowflake arg1, Snowflake arg2) => arg1.Value != arg2.Value;
public static implicit operator ulong(Snowflake s) => s.Value;
public static implicit operator long(Snowflake s) => (long)s.Value;
public static implicit operator Snowflake(ulong n) => new(n);
public static implicit operator Snowflake(long n) => new((ulong)n);
public static bool TryParse(string? input, [NotNullWhen(true)] out Snowflake? snowflake)
{
snowflake = null;
if (!ulong.TryParse(input, out var res)) return false;
snowflake = new Snowflake(res);
return true;
}
public override bool Equals(object? obj) => obj is Snowflake other && Value == other.Value;
public bool Equals(Snowflake other)
{
return Value == other.Value;
}
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
public class JsonConverter : JsonConverter<Snowflake>
{
public override Snowflake Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var raw = reader.GetString();
if (!TryParse(raw, out var snowflake)) throw new FormatException("Snowflake is not a long within a string");
return snowflake.Value;
}
public override void Write(Utf8JsonWriter writer, Snowflake value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}

21
Foxcord/Models/User.cs Normal file
View file

@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
namespace Foxcord.Models;
public class User
{
public Snowflake Id { get; init; }
public required string Username { get; init; }
public required string Discriminator { get; init; }
[JsonIgnore] public string Tag => Discriminator != "0" ? $"{Username}#{Discriminator}" : Username;
public string? GlobalName { get; init; }
public string? Avatar { get; init; }
public string? Banner { get; init; }
public int? AccentColor { get; init; }
public string? Locale { get; init; }
public bool Bot { get; init; } = false;
public bool System { get; init; } = false;
public bool? MfaEnabled { get; init; }
public bool? Verified { get; init; }
public string? Email { get; init; }
}

View file

@ -0,0 +1,128 @@
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using Polly;
using Serilog;
namespace Foxcord.Rest;
public class BaseRestClient
{
public HttpClient Client { get; }
private readonly ILogger _logger;
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly Func<string>? _tokenFactory;
private readonly Func<string, string> _pathCleaner;
private readonly Func<HttpResponseMessage, CancellationToken, Task> _errorHandler;
protected ResiliencePipeline<HttpResponseMessage> Pipeline { get; set; } =
new ResiliencePipelineBuilder<HttpResponseMessage>().Build();
private readonly string _apiBaseUrl;
public BaseRestClient(ILogger logger, RestClientOptions options)
{
Client = new HttpClient();
Client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", options.UserAgent);
_logger = logger.ForContext<BaseRestClient>();
_jsonSerializerOptions = options.JsonSerializerOptions ?? JsonSerializerOptions.Default;
_apiBaseUrl = options.ApiBaseUrl;
_tokenFactory = options.TokenFactory;
_pathCleaner = options.PathLogCleaner ?? (s => s);
_errorHandler = options.ErrorHandler;
}
public async Task<T> RequestAsync<T>(HttpMethod method, string path, CancellationToken ct = default)
where T : class
{
var req = new HttpRequestMessage(method, $"{_apiBaseUrl}{path}");
if (_tokenFactory != null)
req.Headers.Add("Authorization", _tokenFactory());
var resp = await DoRequestAsync(path, req, ct);
return await resp.Content.ReadFromJsonAsync<T>(_jsonSerializerOptions, ct) ??
throw new DiscordRequestError("Content was deserialized as null");
}
public async Task<TResponse> RequestAsync<TRequest, TResponse>(HttpMethod method, string path, TRequest reqBody,
CancellationToken ct = default)
{
var req = new HttpRequestMessage(method, $"{_apiBaseUrl}{path}");
if (_tokenFactory != null)
req.Headers.Add("Authorization", _tokenFactory());
var body = JsonSerializer.Serialize(reqBody, _jsonSerializerOptions);
req.Content = new StringContent(body, new MediaTypeHeaderValue("application/json", "utf-8"));
var resp = await DoRequestAsync(path, req, ct);
return await resp.Content.ReadFromJsonAsync<TResponse>(_jsonSerializerOptions, ct) ??
throw new DiscordRequestError("Content was deserialized as null");
}
private async Task<HttpResponseMessage> DoRequestAsync(string path, HttpRequestMessage req,
CancellationToken ct = default)
{
var context = ResilienceContextPool.Shared.Get(ct);
context.Properties.Set(new ResiliencePropertyKey<string>("Path"), path);
try
{
return await Pipeline.ExecuteAsync(async ctx =>
{
HttpResponseMessage resp;
var stopwatch = new Stopwatch();
stopwatch.Start();
try
{
resp = await Client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead,
ctx.CancellationToken);
stopwatch.Stop();
}
catch (Exception e)
{
_logger.Error(e, "HTTP error: {Method} {Path}", req.Method, _pathCleaner(path));
throw;
}
_logger.Debug("Response: {Method} {Path} -> {StatusCode} {ReasonPhrase} (in {ResponseMs} ms)",
req.Method, _pathCleaner(path), (int)resp.StatusCode, resp.ReasonPhrase,
stopwatch.ElapsedMilliseconds);
await _errorHandler(resp, context.CancellationToken);
return resp;
}, context);
}
finally
{
ResilienceContextPool.Shared.Return(context);
}
}
}
public class RestClientOptions
{
public JsonSerializerOptions? JsonSerializerOptions { get; init; }
public required string UserAgent { get; init; }
public required string ApiBaseUrl { get; init; }
/// <summary>
/// A function that converts non-2XX responses to errors. This should usually throw;
/// if not, the request will continue to be handled normally, which will probably cause errors.
/// </summary>
public required Func<HttpResponseMessage, CancellationToken, Task> ErrorHandler { get; init; }
/// <summary>
/// A function that returns a token. If not set, the client will not add an Authorization header.
/// </summary>
public Func<string>? TokenFactory { get; init; }
/// <summary>
/// A function that cleans up paths for logging. This should remove sensitive content (i.e. tokens).
/// If not set, paths will not be cleaned before being logged.
/// </summary>
public Func<string, string>? PathLogCleaner { get; init; }
}

View file

@ -0,0 +1,66 @@
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Foxcord.Models;
using Foxcord.Rest.Rate;
using Foxcord.Rest.Types;
using Foxcord.Serialization;
using Polly;
using Serilog;
namespace Foxcord.Rest;
public class DiscordRestClient : BaseRestClient
{
public DiscordRestClient(ILogger logger, DiscordRestClientOptions options) : base(logger, new RestClientOptions
{
JsonSerializerOptions = JsonSerializerOptions,
ApiBaseUrl = options.ApiBaseUrl ?? DefaultApiBaseUrl,
UserAgent = options.UserAgent ?? DefaultUserAgent,
TokenFactory = () => $"Bot {options.Token}",
ErrorHandler = HandleError,
})
{
Pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddDiscordStrategy(new RateLimiter(logger.ForContext<DiscordRestClient>()))
.Build();
}
public const string DefaultUserAgent = "DiscordBot (https://code.vulpine.solutions/sam/Foxcord, v1)";
public const string DefaultApiBaseUrl = "https://discord.com/api/v10";
public async Task<GetGatewayBotResponse> GatewayBotAsync(CancellationToken ct = default) =>
await RequestAsync<GetGatewayBotResponse>(HttpMethod.Get, "/gateway/bot", ct);
public async Task<Message> CreateMessageAsync(Snowflake channelId, CreateMessageParams message,
CancellationToken ct = default) =>
await RequestAsync<CreateMessageParams, Message>(HttpMethod.Post, $"/channels/{channelId}/messages", message,
ct);
#region BaseRestClient parameters
private static readonly JsonSerializerOptions JsonSerializerOptions = JsonSerializerExtensions.CreateSerializer();
private static async Task HandleError(HttpResponseMessage resp, CancellationToken ct)
{
if (resp.IsSuccessStatusCode) return;
if (resp.StatusCode == HttpStatusCode.TooManyRequests)
{
throw new RateLimitError(resp.Headers);
}
var error = await resp.Content.ReadFromJsonAsync<DiscordRestError>(JsonSerializerOptions, ct);
if (error == null) throw new ArgumentNullException(nameof(resp), "Response was deserialized as null");
throw error;
}
#endregion
}
public class DiscordRestClientOptions
{
public required string Token { get; init; }
public string? UserAgent { get; init; }
public string? ApiBaseUrl { get; init; }
}

View file

@ -0,0 +1,22 @@
using System.Net.Http.Headers;
namespace Foxcord.Rest;
public class DiscordRestError : Exception
{
public required DiscordErrorCode Code { get; init; }
public new required string Message { get; init; }
}
public enum DiscordErrorCode
{
GeneralError = 0,
UnknownAccount = 10001,
}
public class DiscordRequestError(string message) : Exception(message);
public class RateLimitError(HttpHeaders headers) : Exception("Rate limit error")
{
public HttpHeaders Headers { get; } = headers;
}

View file

@ -0,0 +1,65 @@
using System.Text;
namespace Foxcord.Rest.Rate;
// All of this code is taken from Arikawa:
// https://github.com/diamondburned/arikawa/blob/v3/api/rate/rate.go
internal static class BucketKeyUtils
{
private static readonly string[] MajorRootPaths = ["channels", "guilds"];
internal static string Parse(string path)
{
path = path.Split("?", 2)[0];
var parts = path.Split("/");
if (parts.Length == 0) return path;
parts = parts.Skip(1).ToArray();
var skip = 0;
if (MajorRootPaths.Contains(parts[0])) skip = 2;
skip++;
for (; skip < parts.Length; skip += 2)
{
if (long.TryParse(parts[skip], out _) || StringIsEmojiOnly(parts[skip]) || StringIsCustomEmoji(parts[skip]))
parts[skip] = "";
}
path = string.Join("/", parts);
return $"/{path}";
}
private static bool StringIsCustomEmoji(string emoji)
{
var parts = emoji.Split(":");
if (parts.Length != 2) return false;
if (!long.TryParse(parts[1], out _)) return false;
if (parts[0].Contains(' ')) return false;
return true;
}
private static bool StringIsEmojiOnly(string emoji)
{
var runes = emoji.EnumerateRunes().ToArray();
switch (runes.Length)
{
case 0:
return false;
case 1:
case 2:
return EmojiRune(runes[0]);
}
return false;
}
private static bool EmojiRune(Rune r)
{
if (r == new Rune('\u00a9') || r == new Rune('\u00ae') ||
(r >= new Rune('\u2000') && r <= new Rune('\u3300'))) return true;
return false;
}
}

View file

@ -0,0 +1,34 @@
using Polly;
namespace Foxcord.Rest.Rate;
public class DiscordResilienceStrategy(RateLimiter rateLimiter)
: ResilienceStrategy<HttpResponseMessage>
{
protected override async ValueTask<Outcome<HttpResponseMessage>> ExecuteCore<TState>(
Func<ResilienceContext, TState, ValueTask<Outcome<HttpResponseMessage>>> callback, ResilienceContext context,
TState state)
{
var path = context.Properties.GetValue(new ResiliencePropertyKey<string>("Path"), string.Empty);
if (path == string.Empty) throw new DiscordRequestError("Path was not set in Polly context");
var b = await rateLimiter.LockBucket(BucketKeyUtils.Parse(path), context.CancellationToken);
var response = await callback(context, state).ConfigureAwait(context.ContinueOnCapturedContext);
if (response.Exception is RateLimitError rateLimitError)
b.Release(rateLimitError.Headers);
else if (response.Result != null)
b.Release(response.Result.Headers);
return response;
}
}
public class DiscordResilienceStrategyOptions : ResilienceStrategyOptions;
public static class DiscordResilienceStrategyExtensions
{
public static ResiliencePipelineBuilder<HttpResponseMessage> AddDiscordStrategy(
this ResiliencePipelineBuilder<HttpResponseMessage> builder, RateLimiter rateLimiter) =>
builder.AddStrategy(_ => new DiscordResilienceStrategy(rateLimiter), new DiscordResilienceStrategyOptions());
}

View file

@ -0,0 +1,161 @@
using System.Collections.Concurrent;
using System.Globalization;
using System.Net.Http.Headers;
using Serilog;
namespace Foxcord.Rest.Rate;
// Most of this code is taken from discordgo:
// https://github.com/bwmarrin/discordgo/blob/master/ratelimit.go
public class RateLimiter(ILogger logger)
{
private readonly ILogger _logger = logger.ForContext<RateLimiter>();
private readonly ConcurrentDictionary<string, Bucket> _buckets = new();
private readonly ConcurrentDictionary<string, CustomRateLimit> _customRateLimits = new([
new KeyValuePair<string, CustomRateLimit>("//reactions//", new CustomRateLimit
{
Requests = 1,
Reset = TimeSpan.FromMilliseconds(200)
})
]);
internal long Global;
internal Bucket GetBucket(string key)
{
key = BucketKeyUtils.Parse(key);
var bucket = _buckets.GetOrAdd(key, _ => new Bucket
{
Key = key,
Remaining = 1,
RateLimiter = this,
Logger = _logger
});
if (_customRateLimits.Any(r => key.EndsWith(r.Key)))
bucket.CustomRateLimit = _customRateLimits.First(r => key.EndsWith(r.Key)).Value;
return bucket;
}
internal TimeSpan GetWaitTime(Bucket b, int minRemaining)
{
if (b.Remaining < minRemaining && b.Reset > DateTimeOffset.UtcNow)
return b.Reset - DateTimeOffset.UtcNow;
var sleepTo = DateTimeOffset.FromUnixTimeMilliseconds(Global);
if (sleepTo > DateTimeOffset.UtcNow)
return sleepTo - DateTimeOffset.UtcNow;
return TimeSpan.Zero;
}
internal async Task<Bucket> LockBucket(string bucketId, CancellationToken ct = default) =>
await LockBucket(GetBucket(bucketId), ct);
internal async Task<Bucket> LockBucket(Bucket b, CancellationToken ct = default)
{
_logger.Verbose("Locking bucket {Bucket}", b.Key);
await b.Semaphore.WaitAsync(ct);
var waitTime = GetWaitTime(b, 1);
if (waitTime > TimeSpan.Zero) await Task.Delay(waitTime, ct);
b.Remaining--;
_logger.Verbose("Letting request for bucket {Bucket} through", b.Key);
return b;
}
}
internal class CustomRateLimit
{
internal int Requests;
internal TimeSpan Reset;
}
internal class Bucket
{
internal readonly SemaphoreSlim Semaphore = new(1);
internal required string Key;
internal required ILogger Logger { private get; init; }
internal int Remaining;
internal DateTimeOffset Reset;
private DateTimeOffset _lastReset;
internal CustomRateLimit? CustomRateLimit;
internal required RateLimiter RateLimiter;
// discordgo mentions that this is required to prevent 429s, I trust that
private static readonly TimeSpan ExtraResetTime = TimeSpan.FromMilliseconds(250);
internal void Release(HttpHeaders headers)
{
try
{
if (CustomRateLimit != null)
{
if (DateTimeOffset.UtcNow - _lastReset >= CustomRateLimit.Reset)
{
Remaining = CustomRateLimit.Requests - 1;
_lastReset = DateTimeOffset.UtcNow;
}
if (Remaining < 1)
{
Reset = DateTimeOffset.UtcNow + CustomRateLimit.Reset;
}
return;
}
var remaining = TryGetHeaderValue(headers, "X-RateLimit-Remaining");
var reset = TryGetHeaderValue(headers, "X-RateLimit-Reset");
var global = TryGetHeaderValue(headers, "X-RateLimit-Global");
var resetAfter = TryGetHeaderValue(headers, "X-RateLimit-Reset-After");
if (resetAfter != null)
{
if (!double.TryParse(resetAfter, out var parsedResetAfter))
throw new InvalidRateLimitHeaderException("X-RateLimit-Reset-After was not a valid double");
var resetAt = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(parsedResetAfter);
if (global != null) RateLimiter.Global = resetAt.ToUnixTimeMilliseconds();
else Reset = resetAt;
}
else if (reset != null)
{
var dateHeader = TryGetHeaderValue(headers, "Date");
if (dateHeader == null) throw new InvalidRateLimitHeaderException("Date header was not set");
if (!DateTimeOffset.TryParseExact(dateHeader, "r", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal, out var parsedDate))
throw new InvalidRateLimitHeaderException("Date was not a valid date");
if (!long.TryParse(reset, out var parsedReset))
throw new InvalidRateLimitHeaderException("X-RateLimit-Reset was not a valid long");
var delta = DateTimeOffset.FromUnixTimeMilliseconds(parsedReset) - parsedDate + ExtraResetTime;
Reset = DateTimeOffset.UtcNow + delta;
}
if (remaining == null) return;
if (!int.TryParse(remaining, out var parsedRemaining))
throw new InvalidRateLimitHeaderException("X-RateLimit-Remaining was not a valid integer");
Remaining = parsedRemaining;
Logger.Verbose("New remaining for bucket {Bucket} is {Remaining}", Key, Remaining);
}
finally
{
Logger.Verbose("Releasing bucket {Bucket}", Key);
Semaphore.Release();
}
}
private static string? TryGetHeaderValue(HttpHeaders headers, string key) =>
headers.TryGetValues(key, out var values) ? values.FirstOrDefault() : null;
}
public class InvalidRateLimitHeaderException(string message) : Exception(message);

View file

@ -0,0 +1,6 @@
namespace Foxcord.Rest.Types;
public record CreateMessageParams(
string? Content = null,
object? Nonce = null,
bool Tts = false);

View file

@ -0,0 +1,5 @@
namespace Foxcord.Rest.Types;
public record GetGatewayBotResponse(string Url, int Shards, SessionStartLimit SessionStartLimit);
public record SessionStartLimit(int Total, int Remaining, int ResetAfter, int MaxConcurrency);

View file

@ -0,0 +1,19 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Foxcord.Serialization;
public static class JsonSerializerExtensions
{
public static JsonSerializerOptions CreateSerializer()
{
var opts = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
IncludeFields = true
};
return opts;
}
}