initial commit; very basic REST client
This commit is contained in:
commit
6b39228b67
18 changed files with 1177 additions and 0 deletions
69
Foxcord/Discord/Snowflake.cs
Normal file
69
Foxcord/Discord/Snowflake.cs
Normal file
|
@ -0,0 +1,69 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using NodaTime;
|
||||
|
||||
namespace Foxcord.Discord;
|
||||
|
||||
[JsonConverter(typeof(SnowflakeJsonConverter))]
|
||||
public readonly struct Snowflake(ulong value)
|
||||
{
|
||||
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 Instant Time => Instant.FromUnixTimeMilliseconds(Timestamp);
|
||||
|
||||
/// <summary>
|
||||
/// The Unix timestamp embedded in this snowflake, in milliseconds.
|
||||
/// </summary>
|
||||
public long Timestamp => (long)((Value >> 22) + Epoch);
|
||||
|
||||
/// <summary>
|
||||
/// The process ID embedded in this snowflake.
|
||||
/// </summary>
|
||||
public byte WorkerId => (byte)((Value & 0x3E0000) >> 17);
|
||||
|
||||
/// <summary>
|
||||
/// The thread 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 override int GetHashCode() => Value.GetHashCode();
|
||||
public override string ToString() => Value.ToString();
|
||||
|
||||
public class SnowflakeJsonConverter : JsonConverter<Snowflake>
|
||||
{
|
||||
public override Snowflake Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new(ulong.Parse(reader.GetString()!));
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Snowflake value, JsonSerializerOptions options) =>
|
||||
writer.WriteStringValue(value.Value.ToString());
|
||||
}
|
||||
}
|
26
Foxcord/Discord/User.cs
Normal file
26
Foxcord/Discord/User.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
namespace Foxcord.Discord;
|
||||
|
||||
public record User(
|
||||
Snowflake Id,
|
||||
string Username,
|
||||
string Discriminator,
|
||||
string? DisplayName,
|
||||
string? Avatar,
|
||||
string? Banner,
|
||||
int? AccentColor,
|
||||
string? Locale,
|
||||
bool Bot,
|
||||
bool System = false
|
||||
)
|
||||
{
|
||||
public string Tag => Discriminator == "0" ? Username : $"{Username}#{Discriminator}";
|
||||
|
||||
private string AvatarExt => Avatar?.StartsWith("a_") == true ? ".gif" : ".png";
|
||||
private string BannerExt => Banner?.StartsWith("a_") == true ? ".gif" : ".png";
|
||||
|
||||
public string AvatarUrl => Avatar == null
|
||||
? $"https://cdn.discordapp.com/embed/avatars/{(Id >> 22) % 6}"
|
||||
: $"https://cdn.discordapp.com/avatars/{Id}/{Avatar}{AvatarExt}";
|
||||
|
||||
public string? BannerUrl => Banner == null ? null : $"https://cdn.discordapp.com/banners/{Id}/{Banner}{BannerExt}";
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue