Finish implementing initial objects

This commit is contained in:
zontreck 2024-07-08 23:25:51 -07:00
parent 161484f693
commit 39dac3b191
4 changed files with 206 additions and 65 deletions

View file

@ -1,50 +1,62 @@
import 'dart:convert';
import 'package:libac_dart/discord/user.dart';
import 'package:libac_dart/utils/DictTools.dart';
class ApplicationPacket {
bool botPublic;
bool botRequiresCodeGrant;
String coverImage;
String? coverImage;
String description;
String guildId;
String? guildId;
String? icon;
String id;
Map<String, ApplicationIntegrationType> integrationConfig;
Map<String, ApplicationIntegrationType>? integrationConfig;
String name;
User? owner;
String? primarySkuId;
String? slug;
String summary;
ApplicationPacket(
{required this.botPublic,
required this.botRequiresCodeGrant,
required this.coverImage,
this.coverImage,
required this.description,
required this.guildId,
this.guildId,
required this.id,
required this.integrationConfig,
this.integrationConfig,
required this.name,
this.icon,
this.owner});
this.owner,
this.primarySkuId,
this.slug,
required this.summary});
Map<String, dynamic> toJson() {
Map<String, dynamic> itc = {};
for (MapEntry<String, ApplicationIntegrationType> entry
in integrationConfig.entries) {
itc[entry.key] = entry.value.toJson();
if (integrationConfig != null) {
for (MapEntry<String, ApplicationIntegrationType> entry
in integrationConfig!.entries) {
itc[entry.key] = entry.value.toJson();
}
}
return {
"bot_public": botPublic,
"bot_require_code_grant": botRequiresCodeGrant,
"cover_image": coverImage,
if (coverImage != null) "cover_image": coverImage,
"description": description,
"guild_id": guildId,
if (guildId != null) "guild_id": guildId,
"icon": icon,
"id": id,
"integration_types_config": itc,
if (integrationConfig != null) "integration_types_config": itc,
"name": name,
"owner": owner.toJson()
if (owner != null) "owner": owner!.toJson(),
if (primarySkuId != null) "primary_sku_id": primarySkuId,
if (slug != null) "slug": slug,
"summary": summary,
};
}
@ -57,24 +69,30 @@ class ApplicationPacket {
}
factory ApplicationPacket.decode(Map<String, dynamic> js) {
var itc = {};
var itc_js = js["integration_types_config"] as Map<String, dynamic>;
for (MapEntry<String, dynamic> jsx in itc_js.entries) {
itc[jsx.key] =
ApplicationIntegrationType.decode(jsx.value as Map<String, dynamic>);
Map<String, ApplicationIntegrationType>? itc = null;
if (js.containsKey("integration_types_config")) {
itc = {};
var itc_js = js["integration_types_config"] as Map<String, dynamic>;
for (MapEntry<String, dynamic> jsx in itc_js.entries) {
itc[jsx.key] = ApplicationIntegrationType.decode(
jsx.value as Map<String, dynamic>);
}
}
return ApplicationPacket(
botPublic: js['bot_public'] as bool,
botRequiresCodeGrant: js['bot_require_code_grant'] as bool,
coverImage: js['cover_image'] as String,
coverImage: setor(js, 'cover_image', null),
description: js['description'] as String,
guildId: js['guild_id'] as String,
guildId: setor(js, 'guild_id', null),
id: js['id'] as String,
integrationConfig: itc as Map<String, ApplicationIntegrationType>,
integrationConfig: itc,
name: js['name'] as String,
owner: User.decode(js['owner']),
icon: js['icon']);
icon: js['icon'],
primarySkuId: setor(js, 'primary_sku_id', null),
slug: setor(js, "slug", null),
summary: js['summary']);
}
}

View file

@ -18,9 +18,15 @@ class User {
String? locale;
bool? verified;
String? email;
UserFlags? flags;
PremiumType? premiumType;
UserFlags? publicFlags;
/// UserFlags
BitMask? flags;
/// PremiumType
BitMask? premiumType;
/// UserFlags
BitMask? publicFlags;
AvatarDecorationData? decoration;
User(
@ -61,18 +67,19 @@ class User {
if (locale != null) "locale": locale,
if (verified != null) "verified": verified,
if (email != null) "email": email,
if (flags != null) "flags": flags!.flags,
if (premiumType != null) "premium_type": premiumType!.flag,
if (publicFlags != null) "public_flags": publicFlags!.flags,
if (flags != null) "flags": flags!.value,
if (premiumType != null) "premium_type": premiumType!.value,
if (publicFlags != null) "public_flags": publicFlags!.value,
if (decoration != null) "avatar_decoration_data": decoration!.toJson()
};
}
factory User.fromJson(String js) {
return User.decode(json.decode(js));
return User.decode(json.decode(js))!;
}
factory User.decode(Map<String, dynamic> js) {
static User? decode(Map<String, dynamic>? js) {
if (js == null) return null;
return User(
id: js['id'] as String,
username: js['username'] as String,
@ -87,15 +94,15 @@ class User {
locale: setor(js, "locale", null),
verified: setor(js, "verified", null),
email: setor(js, "email", null),
flags: UserFlags.decode(setor(js, "flags", null)),
premiumType: PremiumType.decode(setor(js, "premium_type", null)),
publicFlags: UserFlags.decode(setor(js, "public_flags", null)),
flags: BitMask.of(setor(js, "flags", null)),
premiumType: BitMask.of(setor(js, "premium_type", null)),
publicFlags: BitMask.of(setor(js, "public_flags", null)),
decoration: AvatarDecorationData.decode(
setor(js, "avatar_decoration_data", null)));
}
}
enum UserFlags implements MaskEnum {
enum UserFlags {
DiscordEmployee(1 << 0),
PartneredServerOwner(1 << 1),
HypeSquadEvents(1 << 2),
@ -115,28 +122,6 @@ enum UserFlags implements MaskEnum {
final int flags;
const UserFlags(this.flags);
static UserFlags? decode(dynamic value) {
if (value == null) return null;
int flag = value as int;
for (var val in values) {
if (val == flag) {
return val;
}
}
return null;
}
@override
int getValue() {
return flags;
}
@override
List<UserFlags> getValues() {
return values;
}
}
enum PremiumType {
@ -173,3 +158,120 @@ class AvatarDecorationData {
asset: js['asset'] as String, sku: js['sku_id'] as String);
}
}
class Team {
String? icon;
String id;
List<TeamMember> members;
String name;
String ownerUserId;
Team(
{required this.icon,
required this.id,
required this.members,
required this.name,
required this.ownerUserId});
String encode() {
return json.encode(toJson());
}
Map<String, dynamic> toJson() {
List<Map<String, dynamic>> membersJs = [];
for (TeamMember tm in members) {}
return {
"icon": icon,
"id": id,
"members": membersJs,
"name": name,
"owner_user_id": ownerUserId
};
}
factory Team.fromJson(String js) {
return Team.decode(json.decode(js));
}
factory Team.decode(Map<String, dynamic> js) {
List<TeamMember> memberjs = [];
List<Map<String, dynamic>> mmap =
js['members'] as List<Map<String, dynamic>>;
for (var entry in mmap) {
memberjs.add(TeamMember.decode(entry));
}
return Team(
icon: js['icon'],
id: js['id'],
members: memberjs,
name: js['name'],
ownerUserId: js['owner_user_id']);
}
}
class TeamMember {
MembershipState membershipState;
String teamId;
User user;
String role;
TeamMember(
{required this.membershipState,
required this.teamId,
required this.user,
required this.role});
String encode() {
return json.encode(toJson());
}
Map<String, dynamic> toJson() {
return {
"membership_state": membershipState,
"team_id": teamId,
"user": user.toJson(),
"role": role
};
}
factory TeamMember.fromJson(String js) {
return TeamMember.decode(json.decode(js));
}
factory TeamMember.decode(Map<String, dynamic> js) {
return TeamMember(
membershipState: MembershipState.of(js['membership_state']),
teamId: js['team_id'],
user: User.decode(js['user'] as Map<String, dynamic>)!,
role: js['role']);
}
}
enum MembershipState {
INVITED(1),
ACCEPTED(2);
final int val;
const MembershipState(this.val);
static MembershipState of(int val) {
switch (val) {
case 1:
{
return MembershipState.INVITED;
}
case 2:
{
return MembershipState.ACCEPTED;
}
default:
{
return MembershipState.INVITED;
}
}
}
}

View file

@ -1,7 +1,27 @@
class BitMask {}
abstract class MaskEnum {
int getValue();
List<MaskEnum> getValues();
mixin EFlags on Enum {
int get value => 1 << index;
}
/// A simple universal bitmask object
class BitMask {
int _mask = 0;
BitMask(this._mask);
void setBit(int bit) {
_mask |= bit;
}
void unsetBit(int bit) {
_mask ^= bit;
}
int get value => _mask;
factory BitMask.of(int? val) {
if (val == null)
return BitMask(0);
else
return BitMask(val);
}
}

View file

@ -1,6 +1,6 @@
name: libac_dart
description: "Aria's Creations code library"
version: 1.1.070324+0002
version: 1.2.070824+2226
homepage: "https://zontreck.com"
@ -9,6 +9,7 @@ environment:
# Add regular dependencies here.
dependencies:
dio: ^5.5.0+1
# path: ^1.8.0
dev_dependencies:
@ -16,7 +17,7 @@ dev_dependencies:
test: ^1.24.0
# following page: https://dart.dev/tools/pub/pubspec
# following page: https://dart.dev/tools/pub/pubspec
# To add assets to your package, add an assets section, like this: