Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user clans #733

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/src/http/managers/user_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:nyxx/src/models/user/application_role_connection.dart';
import 'package:nyxx/src/models/user/avatar_decoration_data.dart';
import 'package:nyxx/src/models/user/connection.dart';
import 'package:nyxx/src/models/user/user.dart';
import 'package:nyxx/src/models/user/user_clan.dart';
import 'package:nyxx/src/utils/cache_helpers.dart';
import 'package:nyxx/src/utils/parsing_helpers.dart';

Expand Down Expand Up @@ -58,6 +59,7 @@ class UserManager extends ReadOnlyManager<User> {
publicFlags: hasPublicFlags ? UserFlags(raw['public_flags'] as int) : null,
avatarDecorationHash: avatarDecorationData?.asset,
avatarDecorationData: avatarDecorationData,
clan: maybeParse(raw['clan'], parseUserClan),
);
}

Expand Down Expand Up @@ -101,6 +103,17 @@ class UserManager extends ReadOnlyManager<User> {
);
}

/// Parse a [UserClan] from [raw].
UserClan parseUserClan(Map<String, Object?> raw) {
return UserClan(
identityGuildId: Snowflake.parse(raw['identity_guild_id']!),
isIdentityEnabled: raw['identity_enabled'] as bool,
tag: raw['tag'] as String,
badgeHash: raw['badge'] as String,
manager: this,
);
}

@override
Future<User> fetch(Snowflake id) async {
final route = HttpRoute()..users(id: id.toString());
Expand Down
3 changes: 3 additions & 0 deletions lib/src/http/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,7 @@ extension RouteHelpers on HttpRoute {

/// Adds the [`send-soundboard-sound`](https://discord.com/developers/docs/resources/soundboard#send-soundboard-sound) part to this [HttpRoute].
void sendSoundboardSound() => add(HttpRoutePart('send-soundboard-sound'));

/// Adds the [`clan-badges`](https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints)
void clanBadges({String? id}) => add(HttpRoutePart('clan-badges', [if (id != null) HttpRouteParam(id)]));
}
5 changes: 5 additions & 0 deletions lib/src/models/user/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:nyxx/src/models/locale.dart';
import 'package:nyxx/src/models/message/author.dart';
import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart';
import 'package:nyxx/src/models/user/avatar_decoration_data.dart';
import 'package:nyxx/src/models/user/user_clan.dart';
import 'package:nyxx/src/utils/enum_like.dart';
import 'package:nyxx/src/utils/flags.dart';

Expand Down Expand Up @@ -77,6 +78,9 @@ class User extends PartialUser implements MessageAuthor, CommandOptionMentionabl
/// The user's avatar deciration data.
final AvatarDecorationData? avatarDecorationData;

/// The user's clan data.
final UserClan? clan;

/// {@macro user}
/// @nodoc
User({
Expand All @@ -97,6 +101,7 @@ class User extends PartialUser implements MessageAuthor, CommandOptionMentionabl
required this.publicFlags,
required this.avatarDecorationHash,
required this.avatarDecorationData,
required this.clan,
});

/// This user's banner.
Expand Down
30 changes: 30 additions & 0 deletions lib/src/models/user/user_clan.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:nyxx/src/http/cdn/cdn_asset.dart';
import 'package:nyxx/src/http/managers/user_manager.dart';
import 'package:nyxx/src/http/route.dart';
import 'package:nyxx/src/models/guild/guild.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart';

class UserClan with ToStringHelper {
final UserManager manager;

/// The ID of this user's primary clan
final Snowflake identityGuildId;

/// Whether the user is displaying their clan tag
final bool isIdentityEnabled;

/// The text of the user's clan tag. Limited to 4 characters
final String tag;

/// The clan badge hash.
final String badgeHash;

UserClan({required this.identityGuildId, required this.badgeHash, required this.isIdentityEnabled, required this.tag, required this.manager});

/// The guild of this user's primary clan
PartialGuild get identifyGuild => manager.client.guilds[identityGuildId];

/// The clan badge
CdnAsset get badge => CdnAsset(client: manager.client, base: HttpRoute()..clanBadges(id: identityGuildId.toString()), hash: badgeHash);
}
16 changes: 16 additions & 0 deletions test/unit/http/managers/user_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ final sampleUser = {
"accent_color": 16711680,
"premium_type": 1,
"public_flags": 64,
"avatar_decoration_data": {
"sku_id": "1144058844004233369",
"asset": "a_fed43ab12698df65902ba06727e20c0e"
},
"clan": {
"identity_guild_id": "80351110224678912",
"identity_enabled": true,
"tag": "CUTE",
"badge": "0fcf9a436df1a4b0de5644d3d42188e2"
}
};

void checkSampleUser(User user) {
Expand All @@ -36,6 +46,12 @@ void checkSampleUser(User user) {
expect(user.flags, equals(UserFlags(64)));
expect(user.nitroType, equals(NitroType.classic));
expect(user.publicFlags, equals(UserFlags(64)));
expect(user.avatarDecorationHash, equals('a_fed43ab12698df65902ba06727e20c0e'));
expect(user.avatarDecorationData!.skuId, equals(Snowflake(1144058844004233369)));
expect(user.clan!.identityGuildId, equals(Snowflake(80351110224678912)));
expect(user.clan!.isIdentityEnabled, isTrue);
expect(user.clan!.tag, equals('CUTE'));
expect(user.clan!.badgeHash, equals('0fcf9a436df1a4b0de5644d3d42188e2'));
}

final sampleConnection = {
Expand Down
Loading