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

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; }
}