Skip to content

Commit

Permalink
add some short-circuit methods (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexedia authored Dec 12, 2024
1 parent 4f0d5d2 commit 96f29bf
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 15 deletions.
21 changes: 11 additions & 10 deletions lib/nyxx_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
/// Extensions and additional utilities for working with [nyxx](https://pub.dev/packages/nyxx).
library nyxx_extensions;

export 'src/utils/emoji.dart';
export 'src/utils/endpoint_paginator.dart' hide streamPaginatedEndpoint;
export 'src/utils/formatters.dart';
export 'src/utils/guild_joins.dart';
export 'src/utils/pagination.dart';
export 'src/utils/permissions.dart';
export 'src/utils/sanitizer.dart';

export 'src/extensions/application.dart';
export 'src/extensions/cdn_asset.dart' hide getRequest;
export 'src/extensions/channel.dart';
export 'src/extensions/client.dart';
export 'src/extensions/date_time.dart';
export 'src/extensions/embed.dart';
export 'src/extensions/emoji.dart';
export 'src/extensions/events/guild.dart';
export 'src/extensions/guild.dart';
export 'src/extensions/list.dart';
export 'src/extensions/managers/audit_log_manager.dart';
export 'src/extensions/managers/channel_manager.dart';
export 'src/extensions/managers/entitlement_manager.dart';
Expand All @@ -30,5 +25,11 @@ export 'src/extensions/role.dart';
export 'src/extensions/scheduled_event.dart';
export 'src/extensions/snowflake_entity.dart';
export 'src/extensions/user.dart';
export 'src/extensions/list.dart';
export 'src/extensions/application.dart';

export 'src/utils/emoji.dart';
export 'src/utils/endpoint_paginator.dart' hide streamPaginatedEndpoint;
export 'src/utils/formatters.dart';
export 'src/utils/guild_joins.dart';
export 'src/utils/pagination.dart';
export 'src/utils/permissions.dart';
export 'src/utils/sanitizer.dart';
2 changes: 1 addition & 1 deletion lib/src/extensions/application.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:nyxx/nyxx.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 Down
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;
}
4 changes: 1 addition & 3 deletions lib/src/extensions/guild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ extension PartialGuildExtensions on PartialGuild {
/// Extensions on [Guild]s.
extension GuildExtensions on Guild {
/// The acronym of the guild if no icon is chosen.
String get acronym {
return name.replaceAll(r"'s ", ' ').replaceAllMapped(RegExp(r'\w+'), (match) => match[0]![0]).replaceAll(RegExp(r'\s'), '');
}
String get acronym => name.replaceAll(r"'s ", ' ').replaceAllMapped(RegExp(r'\w+'), (match) => match[0]![0]).replaceAll(RegExp(r'\s'), '');
}
15 changes: 15 additions & 0 deletions lib/src/extensions/member.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
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 decoration.
// Same as in UserExtensions.
Uri? get avatarDecorationUrl => avatarDecoration?.get(format: CdnFormat.png);
}
5 changes: 5 additions & 0 deletions lib/src/extensions/message.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use

import 'package:nyxx/nyxx.dart';
import 'package:nyxx_extensions/nyxx_extensions.dart';

Expand All @@ -21,6 +23,9 @@ extension MessageExtensions on Message {
suppressEmbeds: builder.suppressEmbeds,
suppressNotifications: builder.suppressNotifications,
tts: builder.tts,
enforceNonce: builder.enforceNonce,
poll: builder.poll,
referencedMessage: builder.referencedMessage,
);

return channel.sendMessage(copiedBuilder);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/extensions/snowflake_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ extension SnowflakeEntityExtensions<T extends SnowflakeEntity<T>> on SnowflakeEn
return null;
}
}

/// The date and time this entity was created.
DateTime get createdAt => id.timestamp;
}

/// Extensions on [ManagedSnowflakeEntity]s.
Expand Down
10 changes: 10 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,12 @@ 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 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: 2 additions & 0 deletions lib/src/utils/emoji.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: implementation_imports

import 'dart:convert';

import 'package:http/http.dart' as http;
Expand Down
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 96f29bf

Please sign in to comment.