Skip to content

Commit

Permalink
add some short-circuit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexedia committed Nov 2, 2024
1 parent 4f0d5d2 commit 3714f49
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/src/extensions/activity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:nyxx/nyxx.dart';

const mediaProxyUrl = 'https://media.discordapp.net';

extension ActivityAssetsExtension on Activity {
Uri? assetsLargeImageUrl({CdnFormat? format}) {
if (assets?.largeImage == null || applicationId == null) {
return null;
}

return parseAssetUrl(assets!.largeImage!, applicationId!, format ?? CdnFormat.png);
}

Uri? assetsSmallImageUrl({CdnFormat? format}) {
if (assets?.smallImage == null || applicationId == null) {
return null;
}

return parseAssetUrl(assets!.smallImage!, applicationId!, format ?? CdnFormat.png);
}
}

Uri parseAssetUrl(String assetUrl, Snowflake applicationId, CdnFormat format) {
var isMediaProxy = assetUrl.startsWith('mp:');

return isMediaProxy
? Uri.parse('$mediaProxyUrl/${assetUrl.substring(3)}')
: Uri.https('cdn.discordapp.com', '/app-assets/$applicationId/$assetUrl.${format.extension}');
}
11 changes: 10 additions & 1 deletion lib/src/extensions/application.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/src/extensions/cdn_asset.dart';

/// Extensions on [PartialApplication]s.
extension ApplicationExtensions on PartialApplication {
extension PartialApplicationExtensions on PartialApplication {
/// Get a URL users can visit to add this bot to a guild.
Uri getInviteUri({
List<String> scopes = const ['bot', 'applications.commands'],
Expand All @@ -21,3 +22,11 @@ extension ApplicationExtensions on PartialApplication {
},
);
}

extension ApplicationExtensions on Application {
/// The URL of this application's icon image.
Uri? iconUrl({CdnFormat? format, int? size}) => icon?.get(format: format, size: size);

/// The URL of this application's cover image.
Uri? coverUrl({CdnFormat? format, int? size}) => coverImage?.get(format: format, size: size);
}
1 change: 1 addition & 0 deletions lib/src/extensions/cdn_asset.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:nyxx/nyxx.dart';

extension CdnAssetExtensions on CdnAsset {
/// Get the URL for this asset whth the given [format] and [size].
Uri get({CdnFormat? format, int? size}) => getRequest(this, format ?? defaultFormat, size).prepare(client).url;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/src/extensions/events/guild.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:nyxx/nyxx.dart';

extension GuildDeleteEventExtensions on GuildDeleteEvent {
/// Whether the client was removed from the guild, due to a ban or kick.
bool get wasRemoved => isUnavailable == false;
}
13 changes: 13 additions & 0 deletions lib/src/extensions/guild.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/src/extensions/managers/guild_manager.dart';
import 'package:nyxx_extensions/src/extensions/cdn_asset.dart';

/// Extensions on [PartialGuild]s.
extension PartialGuildExtensions on PartialGuild {
Expand All @@ -21,4 +22,16 @@ extension GuildExtensions on Guild {
String get acronym {
return name.replaceAll(r"'s ", ' ').replaceAllMapped(RegExp(r'\w+'), (match) => match[0]![0]).replaceAll(RegExp(r'\s'), '');
}

/// The URL of this guild's icon image.
Uri? iconUrl({CdnFormat? format, int? size}) => icon?.get(format: format, size: size);

/// The URL of this guild's banner image.
Uri? bannerUrl({CdnFormat? format, int? size}) => banner?.get(format: format, size: size);

/// The URL of this guild's splash image.
Uri? splashUrl({CdnFormat? format, int? size}) => splash?.get(format: format, size: size);

/// The URL of this guild's discovery splash image.
Uri? discoverySplashUrl({CdnFormat? format, int? size}) => discoverySplash?.get(format: format, size: size);
}
21 changes: 21 additions & 0 deletions lib/src/extensions/member.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/src/utils/permissions.dart';
import 'package:nyxx_extensions/src/extensions/cdn_asset.dart';

/// Extensions on [PartialMember]s.
extension PartialMemberExtensions on PartialMember {
/// Compute this member's permissions in [channel].
///
/// {@macro compute_permissions_detail}
Future<Permissions> computePermissionsIn(GuildChannel channel) async => await computePermissions(channel, await get());

/// Kick this member from the guild.
///
/// External references:
///
/// - [MemberManager.delete]
/// - Discord API Reference: https://discord.com/developers/docs/resources/guild#remove-guild-member
Future<void> kick({String? auditLogReason}) => delete(auditLogReason: auditLogReason);
}

extension MemberExtensions on Member {
/// The URL of this member's avatar image.
Uri? avatarUrl({CdnFormat? format, int? size}) => avatar?.get(format: format, size: size);

/// The URL of this member's banner image.
Uri? bannerUrl({CdnFormat? format, int? size}) => banner?.get(format: format, size: size);

/// The URL of this member's avatar decoration.
// Same as in UserExtensions.
Uri? get avatarDecorationUrl => avatarDecoration?.get(format: CdnFormat.png);
}
6 changes: 6 additions & 0 deletions lib/src/extensions/role.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/src/utils/formatters.dart';
import 'package:nyxx_extensions/src/extensions/cdn_asset.dart';

/// Extensions on [PartialRole]s.
extension PartialRoleExtensions on PartialRole {
Expand Down Expand Up @@ -29,3 +30,8 @@ extension RoleList on List<Role> {
/// The roles in this list, sorted from lowest to highest.
List<Role> get sorted => List.of(this)..sort(compare);
}

extension RoleExtensions on Role {
/// The URL of this role's icon image.
Uri? iconUrl({CdnFormat? format, int? size}) => icon?.get(format: format, size: size);
}
6 changes: 6 additions & 0 deletions lib/src/extensions/scheduled_event.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/src/extensions/managers/scheduled_event_manager.dart';
import 'package:nyxx_extensions/src/utils/endpoint_paginator.dart';
import 'package:nyxx_extensions/src/extensions/cdn_asset.dart';

/// Extensions on [PartialScheduledEvent].
extension PartialScheduledEventExtensions on PartialScheduledEvent {
Expand All @@ -25,3 +26,8 @@ extension PartialScheduledEventExtensions on PartialScheduledEvent {
withMembers: withMembers,
);
}

extension ScheduledEventExtensions on ScheduledEvent {
/// The URL of this event's icon image.
Uri? coverUrl({CdnFormat? format, int? size}) => coverImage?.get(format: format, size: size);
}
7 changes: 7 additions & 0 deletions lib/src/extensions/team.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/src/extensions/cdn_asset.dart';

extension TeamExtensions on Team {
/// Returns the URL of the team's icon.
Uri? iconUrl({CdnFormat? format, int? size}) => icon?.get(format: format, size: size);
}
19 changes: 19 additions & 0 deletions lib/src/extensions/user.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/src/extensions/cdn_asset.dart';
import 'package:nyxx_extensions/src/utils/formatters.dart';

/// Extensions on [PartialUser].
Expand Down Expand Up @@ -28,3 +29,21 @@ extension PartialUserExtensions on PartialUser {
/// A mention of this user.
String get mention => userMention(id);
}

extension UserExtensions on User {
/// The user's unique username, if migrated, else a combination of their username and discriminator.
String get tag => discriminator == '0' ? username : '$username#$discriminator';

/// The URL of this user's avatar image.
Uri avatarUrl({CdnFormat? format, int? size}) => avatar.get(format: format, size: size);

/// The URL of this user's banner image.
Uri? bannerUrl({CdnFormat? format, int? size}) => banner?.get(format: format, size: size);

/// The URL of this user's default avatar image.
Uri get defaultAvatarUrl => defaultAvatar.url;

/// The URL of this user's avatar decoration.
// Forcefully add the `.png` extension, otherwise it's converted as a GIF if the hash starts with `a_`, and GIFs are not supported.
Uri? get avatarDecorationUrl => avatarDecoration?.get(format: CdnFormat.png);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:

dependencies:
http: ^1.1.0
nyxx: ^6.3.1
nyxx: ^6.5.2

dev_dependencies:
coverage: ^1.0.3
Expand Down

0 comments on commit 3714f49

Please sign in to comment.