Implement snowflakes

This commit is contained in:
zontreck 2024-07-10 02:09:10 -07:00
parent 660b7df562
commit 662b93e011
4 changed files with 103 additions and 25 deletions

View file

@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:libac_dart/discord/structures/user.dart';
import 'package:libac_dart/structs/Snowflake.dart';
import 'package:libac_dart/utils/DictTools.dart';
class ApplicationPacket {
@ -8,13 +9,13 @@ class ApplicationPacket {
bool botRequiresCodeGrant;
String? coverImage;
String description;
String? guildId;
Snowflake? guildId;
String? icon;
String id;
Snowflake id;
Map<String, ApplicationIntegrationType>? integrationConfig;
String name;
User? owner;
String? primarySkuId;
Snowflake? primarySkuId;
String? slug;
String summary;
@ -48,13 +49,13 @@ class ApplicationPacket {
"bot_require_code_grant": botRequiresCodeGrant,
if (coverImage != null) "cover_image": coverImage,
"description": description,
if (guildId != null) "guild_id": guildId,
if (guildId != null) "guild_id": guildId.toString(),
"icon": icon,
"id": id,
"id": id.toString(),
if (integrationConfig != null) "integration_types_config": itc,
"name": name,
if (owner != null) "owner": owner!.toJson(),
if (primarySkuId != null) "primary_sku_id": primarySkuId,
if (primarySkuId != null) "primary_sku_id": primarySkuId.toString(),
if (slug != null) "slug": slug,
"summary": summary,
};
@ -84,13 +85,18 @@ class ApplicationPacket {
botRequiresCodeGrant: js['bot_require_code_grant'] as bool,
coverImage: setor(js, 'cover_image', null),
description: js['description'] as String,
guildId: setor(js, 'guild_id', null),
id: js['id'] as String,
guildId: js.containsKey("guild_id")
? Snowflake.parse(js['guild_id'] as String, Snowflake.DiscordEpoch)
: null,
id: Snowflake.parse(js['id'] as String, Snowflake.DiscordEpoch),
integrationConfig: itc,
name: js['name'] as String,
owner: User.decode(js['owner']),
icon: js['icon'],
primarySkuId: setor(js, 'primary_sku_id', null),
primarySkuId: js.containsKey("primary_sku_id")
? Snowflake.parse(
js['primary_sku_id'] as String, Snowflake.DiscordEpoch)
: null,
slug: setor(js, "slug", null),
summary: js['summary']);
}

View file

@ -3,9 +3,10 @@ import 'dart:convert';
import 'package:libac_dart/utils/DictTools.dart';
import '../../structs/Bitmasks.dart';
import '../../structs/Snowflake.dart';
class User {
String id;
Snowflake id;
String username;
String discriminator;
String? globalName;
@ -54,7 +55,7 @@ class User {
Map<String, dynamic> toJson() {
return {
"id": id,
"id": id.toString(),
"username": username,
"discriminator": discriminator,
"global_name": globalName,
@ -81,7 +82,7 @@ class User {
static User? decode(Map<String, dynamic>? js) {
if (js == null) return null;
return User(
id: js['id'] as String,
id: Snowflake.parse(js['id'] as String, Snowflake.DiscordEpoch),
username: js['username'] as String,
discriminator: js['discrimination'] as String,
globalName: js['global_name'],
@ -137,7 +138,7 @@ enum PremiumType {
class AvatarDecorationData {
String asset;
String sku;
Snowflake sku;
AvatarDecorationData({required this.asset, required this.sku});
@ -146,7 +147,7 @@ class AvatarDecorationData {
}
Map<String, dynamic> toJson() {
return {"asset": asset, "sku_id": sku};
return {"asset": asset, "sku_id": sku.toString()};
}
factory AvatarDecorationData.fromJson(String js) {
@ -155,16 +156,17 @@ class AvatarDecorationData {
factory AvatarDecorationData.decode(Map<String, dynamic> js) {
return AvatarDecorationData(
asset: js['asset'] as String, sku: js['sku_id'] as String);
asset: js['asset'] as String,
sku: Snowflake.parse(js['sku_id'] as String, Snowflake.DiscordEpoch));
}
}
class Team {
String? icon;
String id;
Snowflake id;
List<TeamMember> members;
String name;
String ownerUserId;
Snowflake ownerUserId;
Team(
{required this.icon,
@ -183,10 +185,10 @@ class Team {
return {
"icon": icon,
"id": id,
"id": id.toString(),
"members": membersJs,
"name": name,
"owner_user_id": ownerUserId
"owner_user_id": ownerUserId.toString()
};
}
@ -204,16 +206,17 @@ class Team {
return Team(
icon: js['icon'],
id: js['id'],
id: Snowflake.parse(js['id'], Snowflake.DiscordEpoch),
members: memberjs,
name: js['name'],
ownerUserId: js['owner_user_id']);
ownerUserId:
Snowflake.parse(js['owner_user_id'], Snowflake.DiscordEpoch));
}
}
class TeamMember {
MembershipState membershipState;
String teamId;
Snowflake teamId;
User user;
String role;
@ -230,7 +233,7 @@ class TeamMember {
Map<String, dynamic> toJson() {
return {
"membership_state": membershipState,
"team_id": teamId,
"team_id": teamId.toString(),
"user": user.toJson(),
"role": role
};
@ -243,7 +246,7 @@ class TeamMember {
factory TeamMember.decode(Map<String, dynamic> js) {
return TeamMember(
membershipState: MembershipState.of(js['membership_state']),
teamId: js['team_id'],
teamId: Snowflake.parse(js['team_id'], Snowflake.DiscordEpoch),
user: User.decode(js['user'] as Map<String, dynamic>)!,
role: js['role']);
}

View file

@ -0,0 +1,69 @@
class Snowflake implements Comparable<Snowflake> {
/// Discord Epoch
static final DiscordEpoch = DateTime.utc(2015, 1, 1, 0, 0, 0);
/// Aria's Creations Epoch
static final ACEpoch = DateTime.utc(2024, 7, 9, 23, 0, 0);
final DateTime epoch;
final int value;
static final ZERO = Snowflake(epoch: DiscordEpoch, value: 0);
Snowflake({required this.epoch, required this.value});
DateTime get timestamp =>
epoch.add(Duration(milliseconds: millisecondsSinceEpoch));
int get millisecondsSinceEpoch => value >> 22;
int get workerId => (value & 0x3E0000) >> 17;
int get processId => (value & 0x1F000) >> 12;
int get increment => value & 0xFFF;
bool get isZero => value == 0;
factory Snowflake.parse(dynamic value, DateTime epoch) {
int val = int.parse(value.toString());
return Snowflake(epoch: epoch, value: val);
}
factory Snowflake.now(DateTime epoch) =>
Snowflake.fromDateTime(DateTime.now(), epoch);
factory Snowflake.fromDateTime(DateTime dt, DateTime epoch) {
return Snowflake(
epoch: epoch, value: dt.difference(epoch).inMilliseconds << 22);
}
bool isBefore(Snowflake other) => timestamp.isBefore(other.timestamp);
bool isAfter(Snowflake other) => timestamp.isAfter(other.timestamp);
bool isAtSameMomentAs(Snowflake other) =>
timestamp.isAtSameMomentAs(other.timestamp);
Snowflake operator +(Duration duration) =>
Snowflake.fromDateTime(timestamp.add(duration), epoch);
Snowflake operator -(Duration duration) =>
Snowflake.fromDateTime(timestamp.subtract(duration), epoch);
@override
int compareTo(Snowflake other) => value.compareTo(other.value);
bool operator <(Snowflake other) => isBefore(other);
bool operator >(Snowflake other) => isAfter(other);
@override
bool operator ==(Object other) =>
identical(this, other) || (other is Snowflake && other.value == value);
@override
int get hashCode => value.hashCode;
@override
String toString() => value.toString();
}

View file

@ -1,6 +1,6 @@
name: libac_dart
description: "Aria's Creations code library"
version: 1.2.070924+2145
version: 1.2.071024+0207
homepage: "https://zontreck.com"