158 lines
4.5 KiB
Dart
158 lines
4.5 KiB
Dart
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 {
|
|
bool botPublic;
|
|
bool botRequiresCodeGrant;
|
|
String? coverImage;
|
|
String description;
|
|
Snowflake? guildId;
|
|
String? icon;
|
|
Snowflake id;
|
|
Map<String, ApplicationIntegrationType>? integrationConfig;
|
|
String name;
|
|
User? owner;
|
|
Snowflake? primarySkuId;
|
|
String? slug;
|
|
String summary;
|
|
|
|
ApplicationPacket(
|
|
{required this.botPublic,
|
|
required this.botRequiresCodeGrant,
|
|
this.coverImage,
|
|
required this.description,
|
|
this.guildId,
|
|
required this.id,
|
|
this.integrationConfig,
|
|
required this.name,
|
|
this.icon,
|
|
this.owner,
|
|
this.primarySkuId,
|
|
this.slug,
|
|
required this.summary});
|
|
|
|
Map<String, dynamic> toJson() {
|
|
Map<String, dynamic> itc = {};
|
|
|
|
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,
|
|
if (coverImage != null) "cover_image": coverImage,
|
|
"description": description,
|
|
if (guildId != null) "guild_id": guildId.toString(),
|
|
"icon": icon,
|
|
"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.toString(),
|
|
if (slug != null) "slug": slug,
|
|
"summary": summary,
|
|
};
|
|
}
|
|
|
|
String encode() {
|
|
return json.encode(toJson());
|
|
}
|
|
|
|
factory ApplicationPacket.fromJson(String js) {
|
|
return ApplicationPacket.decode(json.decode(js));
|
|
}
|
|
|
|
factory ApplicationPacket.decode(Map<String, dynamic> js) {
|
|
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: setor(js, 'cover_image', null),
|
|
description: js['description'] 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: js.containsKey("primary_sku_id")
|
|
? Snowflake.parse(
|
|
js['primary_sku_id'] as String, Snowflake.DiscordEpoch)
|
|
: null,
|
|
slug: setor(js, "slug", null),
|
|
summary: js['summary']);
|
|
}
|
|
}
|
|
|
|
class OAuth2InstallParams {
|
|
List<String> scopes;
|
|
String permissions;
|
|
|
|
OAuth2InstallParams({required this.scopes, required this.permissions});
|
|
|
|
String encode() {
|
|
return json.encode(toJson());
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {"scopes": scopes, "permissions": permissions};
|
|
}
|
|
|
|
factory OAuth2InstallParams.fromJson(String js) {
|
|
return OAuth2InstallParams.decode(json.decode(js));
|
|
}
|
|
|
|
factory OAuth2InstallParams.decode(Map<String, dynamic> js) {
|
|
return OAuth2InstallParams(
|
|
scopes: js['scopes'] as List<String>,
|
|
permissions: js['permissions'] as String);
|
|
}
|
|
}
|
|
|
|
class ApplicationIntegrationType {
|
|
OAuth2InstallParams? installParams;
|
|
|
|
ApplicationIntegrationType({this.installParams});
|
|
|
|
String encode() {
|
|
return json.encode(toJson());
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {"oauth2_install_params": installParams};
|
|
}
|
|
|
|
factory ApplicationIntegrationType.fromJson(String js) {
|
|
Map<String, dynamic> jd = json.decode(js);
|
|
return ApplicationIntegrationType.decode(jd);
|
|
}
|
|
|
|
factory ApplicationIntegrationType.decode(Map<String, dynamic> js) {
|
|
ApplicationIntegrationType ait = ApplicationIntegrationType();
|
|
if (js["oauth2_install_params"] == null) {
|
|
ait = ApplicationIntegrationType(
|
|
installParams:
|
|
OAuth2InstallParams.decode(js['oauth2_install_params']));
|
|
}
|
|
|
|
return ait;
|
|
}
|
|
}
|