Skip to content

Commit 5f41ee5

Browse files
committed
Added even more Models (WIP)
1 parent f1b876e commit 5f41ee5

File tree

64 files changed

+2206
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2206
-273
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
.buildlog/
99
.history
1010
.svn/
11+
.fvm/
1112

1213
# IntelliJ related
1314
*.iml

lib/src/discord_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dart:async';
22

3-
import 'package:discord_api/src/exceptions/discord_api_exceptions.dart';
3+
import 'exceptions/discord_api_exceptions.dart';
44

55
import 'models/discord_api_scope.dart';
66
import 'models/discord_authorization_information.dart';
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import 'discord_activity_flag.dart';
2+
import 'discord_activity_type.dart';
3+
import 'discord_emoji.dart';
4+
import 'discord_snowflake.dart';
5+
6+
import 'discord_activity_assets.dart';
7+
import 'discord_activity_button.dart';
8+
import 'discord_activity_party.dart';
9+
import 'discord_activity_secrets.dart';
10+
import 'discord_activity_timestamps.dart';
11+
12+
class DiscordActivity {
13+
/// the activity's name
14+
final String name;
15+
16+
/// [activity type](https://discord.com/developers/docs/topics/gateway#activity-object-activity-types)
17+
final int type;
18+
19+
late final DiscordActivityType _typeAsEnum;
20+
21+
/// stream url, is validated when type is 1 (Streaming)
22+
///
23+
/// is not always returned, and can be null
24+
final String? url;
25+
26+
/// unix timestamp (in milliseconds) of when the activity
27+
/// was added to the user's session
28+
final int createdAt;
29+
30+
late final DateTime? _createdAtAsDateTime;
31+
32+
/// unix timestamps for start and/or end of the game
33+
///
34+
/// is not always returned, hence the nullable property
35+
final DiscordActivityTimestamps? timestamps;
36+
37+
/// application id of the game
38+
///
39+
/// is not always returned, hence the nullable property
40+
final DiscordSnowflake? applicationId;
41+
42+
/// what the player is currently doing
43+
///
44+
/// is not always returned, and can be null
45+
final String? details;
46+
47+
/// the user's current party status
48+
///
49+
/// is not always returned, and can be null
50+
final String? state;
51+
52+
/// the emoji used for a custom status
53+
///
54+
/// is not always returned, and can be null
55+
final DiscordEmoji? emoji;
56+
57+
/// information for the current party of the player
58+
///
59+
/// is not always returned, hence the nullable property
60+
final DiscordActivityParty? party;
61+
62+
/// images for the presence and their hover texts
63+
///
64+
/// is not always returned, hence the nullable property
65+
final DiscordActivityAssets? assets;
66+
67+
/// secrets for Rich Presence joining and spectating
68+
///
69+
/// is not always returned, hence the nullable property
70+
final DiscordActivitySecrets? secrets;
71+
72+
/// whether or not the activity is an instanced game session
73+
///
74+
/// is not always returned, hence the nullable property
75+
final bool? instance;
76+
77+
/// activity flags **OR**'d together, describes what the payload includes
78+
///
79+
/// is not always returned, hence the nullable property
80+
final int? flags;
81+
82+
late final List<DiscordActivityFlag>? _flagsAsEnum;
83+
84+
/// the custom buttons shown in the Rich Presence (max 2)
85+
///
86+
/// is not always returned, hence the nullable property
87+
final List<DiscordActivityButton>? buttons;
88+
89+
static const nameEntry = 'name';
90+
static const typeEntry = 'type';
91+
static const urlEntry = 'url';
92+
static const createdAtEntry = 'created_at';
93+
static const timestampsEntry = 'timestamps';
94+
static const applicationIdEntry = 'application_id';
95+
static const detailsEntry = 'details';
96+
static const stateEntry = 'state';
97+
static const emojiEntry = 'emoji';
98+
static const partyEntry = 'party';
99+
static const assetsEntry = 'assets';
100+
static const secretsEntry = 'secrets';
101+
static const instanceEntry = 'instance';
102+
static const flagsEntry = 'flags';
103+
static const buttonsEntry = 'buttons';
104+
105+
DiscordActivity({
106+
required this.name,
107+
required this.type,
108+
this.url,
109+
required this.createdAt,
110+
this.timestamps,
111+
this.applicationId,
112+
this.details,
113+
this.state,
114+
this.emoji,
115+
this.party,
116+
this.assets,
117+
this.secrets,
118+
this.instance,
119+
this.flags,
120+
this.buttons,
121+
});
122+
123+
DateTime? get createdAtAsDateTime =>
124+
_createdAtAsDateTime ??= DateTime.fromMillisecondsSinceEpoch(createdAt);
125+
126+
DiscordActivityType get typeAsEnum => DiscordActivityType.values[type];
127+
128+
// TODO: RECOVER FLAGS FROM BITSET IN DISCORD ACTIVITY
129+
130+
// List<DiscordActivityFlag>? get flagsAsEnum =>
131+
// _flagsAsEnum ??= DiscordActivityFlag.valuesFromMask(flags);
132+
133+
factory DiscordActivity.fromJson(Map<String, dynamic> json) =>
134+
DiscordActivity(
135+
name: json[nameEntry] as String,
136+
type: json[typeEntry] as int,
137+
url: json[urlEntry] as String?,
138+
createdAt: json[createdAtEntry] as int,
139+
timestamps: DiscordActivityTimestamps.fromJson(
140+
json[timestampsEntry] as Map<String, dynamic>),
141+
applicationId: json[applicationIdEntry] != null
142+
? DiscordSnowflake(json[applicationIdEntry] as String)
143+
: null,
144+
details: json[detailsEntry] as String?,
145+
state: json[stateEntry] as String?,
146+
emoji: json[emojiEntry] != null
147+
? DiscordEmoji.fromJson(json[emojiEntry] as Map<String, dynamic>)
148+
: null,
149+
party: json[partyEntry] != null
150+
? DiscordActivityParty.fromJson(
151+
json[partyEntry] as Map<String, dynamic>)
152+
: null,
153+
assets: json[assetsEntry] != null
154+
? DiscordActivityAssets.fromJson(
155+
json[assetsEntry] as Map<String, dynamic>)
156+
: null,
157+
secrets: json[secretsEntry] != null
158+
? DiscordActivitySecrets.fromJson(
159+
json[secretsEntry] as Map<String, dynamic>)
160+
: null,
161+
instance: json[instanceEntry] as bool?,
162+
flags: json[flagsEntry] as int?,
163+
buttons: json[buttonsEntry] != null
164+
? List<DiscordActivityButton>.from(
165+
json[buttonsEntry].map(DiscordActivityButton.fromJson))
166+
: null,
167+
);
168+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class DiscordActivityAssets {
2+
/// see [Activity Asset Image](https://discord.com/developers/docs/topics/gateway#activity-object-activity-asset-image)
3+
///
4+
/// is not always returned, hence the nullable property
5+
final String? largeImage;
6+
7+
/// text displayed when hovering over the large image of the activity
8+
///
9+
/// is not always returned, hence the nullable property
10+
final String? largeText;
11+
12+
/// see [Activity Asset Image](https://discord.com/developers/docs/topics/gateway#activity-object-activity-asset-image)
13+
///
14+
/// is not always returned, hence the nullable property
15+
final String? smallImage;
16+
17+
/// text displayed when hovering over the small image of the activity
18+
///
19+
/// is not always returned, hence the nullable property
20+
final String? smallText;
21+
22+
static const largeImageEntry = 'large_image';
23+
static const largeTextEntry = 'large_text';
24+
static const smallImageEntry = 'small_image';
25+
static const smallTextEntry = 'small_text';
26+
27+
DiscordActivityAssets({
28+
this.largeImage,
29+
this.largeText,
30+
this.smallImage,
31+
this.smallText,
32+
});
33+
34+
factory DiscordActivityAssets.fromJson(Map<String, dynamic> json) =>
35+
DiscordActivityAssets(
36+
largeImage: json[largeImageEntry] as String?,
37+
largeText: json[largeTextEntry] as String?,
38+
smallImage: json[smallImageEntry] as String?,
39+
smallText: json[smallTextEntry] as String?,
40+
);
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class DiscordActivityButton {
2+
/// the text shown on the button (1-32 characters)
3+
final String label;
4+
5+
/// the url opened when clicking the button (1-512 characters)
6+
final String url;
7+
8+
static const labelEntry = 'label';
9+
static const urlEntry = 'url';
10+
11+
DiscordActivityButton({
12+
required this.label,
13+
required this.url,
14+
});
15+
16+
factory DiscordActivityButton.fromJson(Map<String, dynamic> json) =>
17+
DiscordActivityButton(
18+
label: json[labelEntry] as String,
19+
url: json[urlEntry] as String,
20+
);
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
enum DiscordActivityFlag {
2+
/// 1 << 0
3+
instance,
4+
5+
/// 1 << 1
6+
join,
7+
8+
/// 1 << 2
9+
spectate,
10+
11+
/// 1 << 3
12+
joinRequest,
13+
14+
/// 1 << 4
15+
sync,
16+
17+
/// 1 << 5
18+
play,
19+
20+
/// 1 << 6
21+
partyPrivacyFriends,
22+
23+
/// 1 << 7
24+
partyPrivacyVoiceChannel,
25+
26+
/// 1 << 8
27+
embedded,
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class DiscordActivityParty {
2+
/// the id of the party
3+
///
4+
/// is not always returned, hence the nullable property
5+
final String? id;
6+
7+
/// used to show the party's current and maximum size
8+
///
9+
/// is an array of two integers (current_size, max_size)
10+
final List<int>? size;
11+
12+
static const idEntry = "id";
13+
static const sizeEntry = "size";
14+
15+
DiscordActivityParty({
16+
this.id,
17+
this.size,
18+
});
19+
20+
factory DiscordActivityParty.fromJson(Map<String, dynamic> json) =>
21+
DiscordActivityParty(
22+
id: json[idEntry] as String?,
23+
size: List<int>.from(json[sizeEntry] as List<int>),
24+
);
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class DiscordActivitySecrets {
2+
/// the secret for joining a party
3+
///
4+
/// is not always returned, hence the nullable property
5+
final String? join;
6+
7+
/// the secret for spectating a game
8+
///
9+
/// is not always returned, hence the nullable property
10+
final String? spectate;
11+
12+
/// the secret for a specific instanced match
13+
///
14+
/// is not always returned, hence the nullable property
15+
final String? match;
16+
17+
static const joinEntry = 'join';
18+
static const spectateEntry = 'spectate';
19+
static const matchEntry = 'match';
20+
21+
DiscordActivitySecrets({
22+
this.join,
23+
this.spectate,
24+
this.match,
25+
});
26+
27+
factory DiscordActivitySecrets.fromJson(Map<String, dynamic> json) =>
28+
DiscordActivitySecrets(
29+
join: json[joinEntry] as String?,
30+
spectate: json[spectateEntry] as String?,
31+
match: json[matchEntry] as String?,
32+
);
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class DiscordActivityTimestamps {
2+
/// unix time (in milliseconds) of when the activity started
3+
///
4+
/// is not always returned, hence the nullable property
5+
final int? start;
6+
7+
late final DateTime? _startAsDateTime;
8+
9+
/// unix time (in milliseconds) of when the activity ends
10+
///
11+
/// is not always returned, hence the nullable property
12+
final int? end;
13+
14+
late final DateTime? _endAsDateTime;
15+
16+
static const startEntry = 'start';
17+
static const endEntry = 'end';
18+
19+
DiscordActivityTimestamps({
20+
this.start,
21+
this.end,
22+
});
23+
24+
DateTime? get startAsDateTime {
25+
if (start == null) return null;
26+
_startAsDateTime ??= DateTime.fromMillisecondsSinceEpoch(start!);
27+
return _startAsDateTime;
28+
}
29+
30+
DateTime? get endAsDateTime {
31+
if (end == null) return null;
32+
_endAsDateTime ??= DateTime.fromMillisecondsSinceEpoch(end!);
33+
return _endAsDateTime;
34+
}
35+
36+
factory DiscordActivityTimestamps.fromJson(Map<String, dynamic> json) =>
37+
DiscordActivityTimestamps(
38+
start: json['start'] as int?,
39+
end: json['end'] as int?,
40+
);
41+
}

0 commit comments

Comments
 (0)