|
| 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 | +} |
0 commit comments