Skip to content

Commit

Permalink
Fix project
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Nov 1, 2024
1 parent 9ea159c commit 0a3a8cf
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
run: dart pub get

- name: Format
run: dart format --set-exit-if-changed -l120 ./lib
run: dart format --set-exit-if-changed -l 160 ./lib

unit-tests:
needs: [ format, analyze, fix ]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ help:
@fgrep -h "##" $(MAKEFILE_LIST) | sed -e 's/\(\:.*\#\#\)/\:\ /' | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

format: ## Run dart format
dart format -l120 .
dart format -l 160 .

fix: ## Run dart fix
dart fix --apply
Expand Down
30 changes: 11 additions & 19 deletions lib/src/extensions/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ extension PartialChannelExtensions on PartialChannel {
/// Extensions on [Channel]s.
extension ChannelExtensions on Channel {
/// A URL clients can visit to navigate to this channel.
Uri get url => Uri.https(manager.client.apiOptions.host,
'/channels/${this is GuildChannel ? '${(this as GuildChannel).guildId}' : '@me'}/$id');
Uri get url => Uri.https(manager.client.apiOptions.host, '/channels/${this is GuildChannel ? '${(this as GuildChannel).guildId}' : '@me'}/$id');
}

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

/// Extensions on all [GuildChannel] types
Expand All @@ -49,9 +47,8 @@ extension GuildChannelsExtension<T extends GuildChannel> on T {
) as GuildChannelBuilder<T>,
GuildVoiceChannel channel => GuildVoiceChannelBuilder(
name: channel.name,
permissionOverwrites: channel.permissionOverwrites
.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny))
.toList(),
permissionOverwrites:
channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(),
position: channel.position,
isNsfw: channel.isNsfw,
parentId: channel.parentId,
Expand All @@ -65,9 +62,8 @@ extension GuildChannelsExtension<T extends GuildChannel> on T {
bitRate: channel.bitrate,
isNsfw: channel.isNsfw,
parentId: channel.parentId,
permissionOverwrites: channel.permissionOverwrites
.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny))
.toList(),
permissionOverwrites:
channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(),
position: channel.position,
rtcRegion: channel.rtcRegion,
userLimit: channel.userLimit,
Expand All @@ -86,29 +82,25 @@ extension GuildChannelsExtension<T extends GuildChannel> on T {
) as GuildChannelBuilder<T>,
GuildCategory category => GuildCategoryBuilder(
name: category.name,
permissionOverwrites: category.permissionOverwrites
.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny))
.toList(),
permissionOverwrites:
category.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(),
position: category.position,
) as GuildChannelBuilder<T>,
GuildAnnouncementChannel channel => GuildAnnouncementChannelBuilder(
name: channel.name,
defaultAutoArchiveDuration: channel.defaultAutoArchiveDuration,
isNsfw: channel.isNsfw,
parentId: channel.parentId,
permissionOverwrites: channel.permissionOverwrites
.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny))
.toList(),
permissionOverwrites:
channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(),
position: channel.position,
topic: channel.topic,
) as GuildChannelBuilder<T>,
_ => GuildChannelBuilder(
name: name,
type: type,
position: position,
permissionOverwrites: permissionOverwrites
.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny))
.toList(),
permissionOverwrites: permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(),
),
};
}
Expand Down
6 changes: 2 additions & 4 deletions lib/src/extensions/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import 'package:nyxx_extensions/src/utils/emoji.dart';
extension NyxxRestExtensions on NyxxRest {
/// List all the text emoji available to this client.
Future<List<TextEmoji>> getTextEmojis() async => (await getEmojiDefinitions())
.map((definition) =>
TextEmoji(id: Snowflake.zero, manager: guilds[Snowflake.zero].emojis, name: definition.surrogates))
.map((definition) => TextEmoji(id: Snowflake.zero, manager: guilds[Snowflake.zero].emojis, name: definition.surrogates))
.toList();

/// Get a text emoji by name.
TextEmoji getTextEmoji(String name) =>
TextEmoji(id: Snowflake.zero, manager: guilds[Snowflake.zero].emojis, name: name);
TextEmoji getTextEmoji(String name) => TextEmoji(id: Snowflake.zero, manager: guilds[Snowflake.zero].emojis, name: name);
}
4 changes: 2 additions & 2 deletions lib/src/extensions/emoji.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import 'package:nyxx_extensions/src/utils/emoji.dart';
/// Extensions on [TextEmoji].
extension TextEmojiExtensions on TextEmoji {
/// Get the definition of this emoji.
Future<EmojiDefinition> getDefinition() async => (await getEmojiDefinitions())
.singleWhere((definition) => definition.surrogates == name || definition.alternateSurrogates == name);
Future<EmojiDefinition> getDefinition() async =>
(await getEmojiDefinitions()).singleWhere((definition) => definition.surrogates == name || definition.alternateSurrogates == name);
}
11 changes: 3 additions & 8 deletions lib/src/extensions/guild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ extension PartialGuildExtensions on PartialGuild {
/// Same as [listBans], but has no limit on the number of bans returned.
///
/// {@macro paginated_endpoint_streaming_parameters}
Stream<Ban> streamBans({Snowflake? after, Snowflake? before, int? pageSize}) =>
manager.streamBans(id, after: after, before: before, pageSize: pageSize);
Stream<Ban> streamBans({Snowflake? after, Snowflake? before, int? pageSize}) => manager.streamBans(id, after: after, before: before, pageSize: pageSize);

/// Return a list of channels in the client's cache that are in this guild.
List<GuildChannel> get cachedChannels =>
manager.client.channels.cache.values.whereType<GuildChannel>().where((element) => element.guildId == id).toList();
List<GuildChannel> get cachedChannels => manager.client.channels.cache.values.whereType<GuildChannel>().where((element) => element.guildId == id).toList();
}

/// 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'), '');
return name.replaceAll(r"'s ", ' ').replaceAllMapped(RegExp(r'\w+'), (match) => match[0]![0]).replaceAll(RegExp(r'\s'), '');
}
}
3 changes: 1 addition & 2 deletions lib/src/extensions/managers/scheduled_event_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ extension ScheduledEventManagerExtensions on ScheduledEventManager {
StreamOrder? order,
}) =>
streamPaginatedEndpoint(
({after, before, limit}) =>
listEventUsers(id, after: after, before: before, limit: limit, withMembers: withMembers),
({after, before, limit}) => listEventUsers(id, after: after, before: before, limit: limit, withMembers: withMembers),
extractId: (user) => user.user.id,
before: before,
after: after,
Expand Down
3 changes: 1 addition & 2 deletions lib/src/extensions/managers/user_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ extension UserManagerExtensions on UserManager {
StreamOrder? order,
}) =>
streamPaginatedEndpoint(
({after, before, limit}) =>
listCurrentUserGuilds(after: after, before: before, limit: limit, withCounts: withCounts),
({after, before, limit}) => listCurrentUserGuilds(after: after, before: before, limit: limit, withCounts: withCounts),
extractId: (guild) => guild.id,
before: before,
after: after,
Expand Down
3 changes: 1 addition & 2 deletions lib/src/extensions/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ 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());
Future<Permissions> computePermissionsIn(GuildChannel channel) async => await computePermissions(channel, await get());
}
3 changes: 1 addition & 2 deletions lib/src/utils/formatters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ String quoteBlock(String content) => '>>> $content';
String hideEmbed(String url) => '<$url>';

/// Format the [content] and the URL into a hyperlink (aka [Markdown link](https://www.markdownguide.org/basic-syntax/#links)), and optionally, add a [title] that will be displayed on hover.
String hyperlink(String content, String url, [String? title]) =>
'[$content](<$url>${title != null ? ' "$title"' : ''})';
String hyperlink(String content, String url, [String? title]) => '[$content](<$url>${title != null ? ' "$title"' : ''})';

/// Wraps the [content] inside `||`.
String spoiler(String content) => '||$content||';
Expand Down
13 changes: 4 additions & 9 deletions lib/src/utils/pagination.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,12 @@ class Pagination extends NyxxPlugin<NyxxGateway> {
/// A stream of interactions that were recognized as being created by a [Pagination] plugin different to this one.
///
/// This is often a sign of leftover menus from a previous client session.
Stream<InteractionCreateEvent<MessageComponentInteraction>> get onUnhandledInteraction =>
_unhandledInteractionsController.stream;
final StreamController<InteractionCreateEvent<MessageComponentInteraction>> _unhandledInteractionsController =
StreamController.broadcast();
Stream<InteractionCreateEvent<MessageComponentInteraction>> get onUnhandledInteraction => _unhandledInteractionsController.stream;
final StreamController<InteractionCreateEvent<MessageComponentInteraction>> _unhandledInteractionsController = StreamController.broadcast();

/// A stream of interactions that were recognized by this plugin but were not handled because the wrong user triggered the interaction.
Stream<InteractionCreateEvent<MessageComponentInteraction>> get onDisallowedUse => _disallowedUseController.stream;
final StreamController<InteractionCreateEvent<MessageComponentInteraction>> _disallowedUseController =
StreamController.broadcast();
final StreamController<InteractionCreateEvent<MessageComponentInteraction>> _disallowedUseController = StreamController.broadcast();

/// Create a new [Pagination] instance.
Pagination(this.options);
Expand Down Expand Up @@ -436,9 +433,7 @@ class _PaginationState {
}

final knownIds = {jumpToStartId, jumpToEndId, previousId, nextId};
if (builder.components?.any((row) =>
row.components.any((element) => element is ButtonBuilder && knownIds.contains(element.customId))) ==
true) {
if (builder.components?.any((row) => row.components.any((element) => element is ButtonBuilder && knownIds.contains(element.customId))) == true) {
// We've already added controls to this builder, likely when the user navigated to this page previously.
return;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/utils/sanitizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ Future<String> sanitizeContent(
SanitizerTarget.roles => '<@&$_whitespaceCharacter${match.group(1)!}>',
SanitizerTarget.everyone => '@$_whitespaceCharacter${match.group(1)!}',
SanitizerTarget.channels => '<#$_whitespaceCharacter${match.group(1)!}>',
SanitizerTarget.emojis =>
'<$_whitespaceCharacter${match.group(1) ?? ''}\\:${match.group(2)}\\:${match.group(3)}>',
SanitizerTarget.emojis => '<$_whitespaceCharacter${match.group(1) ?? ''}\\:${match.group(2)}\\:${match.group(3)}>',
SanitizerTarget.commands => '</$_whitespaceCharacter${match.namedGroup('commandName')}:${match.group(2)}>',
},
};
Expand Down
3 changes: 1 addition & 2 deletions test/integration/endpoint_streaming_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ void main() {
expect(oldestFirstMessages[i].id.isBefore(oldestFirstMessages[i + 1].id), isTrue);
}

final mostRecentFirstMessages =
await channel.messages.stream(order: StreamOrder.mostRecentFirst).take(50).toList();
final mostRecentFirstMessages = await channel.messages.stream(order: StreamOrder.mostRecentFirst).take(50).toList();
for (int i = 0; i < mostRecentFirstMessages.length - 1; i++) {
expect(mostRecentFirstMessages[i].id.isAfter(mostRecentFirstMessages[i + 1].id), isTrue);
}
Expand Down
3 changes: 1 addition & 2 deletions test/integration/member_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ void main() {

expect(
self.avatar.get(size: 3072, format: CdnFormat.jpeg),
Uri.https(
'cdn.discordapp.com', 'avatars/${self.id}/${self.avatarHash}.${CdnFormat.jpeg.extension}', {'size': '3072'}),
Uri.https('cdn.discordapp.com', 'avatars/${self.id}/${self.avatarHash}.${CdnFormat.jpeg.extension}', {'size': '3072'}),
);
});
}
9 changes: 3 additions & 6 deletions test/unit/sanitizer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ void main() {

test(
'ignore',
() async => expect(await sanitizeContent(sampleContent, channel: channel, action: SanitizerAction.ignore),
equals(sampleContent)),
() async => expect(await sanitizeContent(sampleContent, channel: channel, action: SanitizerAction.ignore), equals(sampleContent)),
);

test(
'remove',
() async => expect(
await sanitizeContent(sampleContent, channel: channel, action: SanitizerAction.remove), equals(removed)),
() async => expect(await sanitizeContent(sampleContent, channel: channel, action: SanitizerAction.remove), equals(removed)),
);

test(
'sanitize',
() async => expect(
await sanitizeContent(sampleContent, channel: channel, action: SanitizerAction.sanitize), equals(sanitized)),
() async => expect(await sanitizeContent(sampleContent, channel: channel, action: SanitizerAction.sanitize), equals(sanitized)),
);
});
}
35 changes: 7 additions & 28 deletions test/unit/timestamp_style_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,12 @@ final baseDate = Snowflake(846136758470443069).timestamp;
void main() {
group('Timestamp Test', () {
test('None', () => expect(baseDate.format(), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}>')));
test(
'Short Time',
() => expect(
baseDate.format(TimestampStyle.shortTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:t>')));
test(
'Long Time',
() => expect(
baseDate.format(TimestampStyle.longTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:T>')));
test(
'Short Date',
() => expect(
baseDate.format(TimestampStyle.shortDate), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:d>')));
test(
'Long Date',
() => expect(
baseDate.format(TimestampStyle.longDate), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:D>')));
test(
'Short Date Time',
() => expect(
baseDate.format(TimestampStyle.shortDateTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:f>')));
test(
'Long Date Time',
() => expect(
baseDate.format(TimestampStyle.longDateTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:F>')));
test(
'Relative Time',
() => expect(
baseDate.format(TimestampStyle.relativeTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:R>')));
test('Short Time', () => expect(baseDate.format(TimestampStyle.shortTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:t>')));
test('Long Time', () => expect(baseDate.format(TimestampStyle.longTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:T>')));
test('Short Date', () => expect(baseDate.format(TimestampStyle.shortDate), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:d>')));
test('Long Date', () => expect(baseDate.format(TimestampStyle.longDate), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:D>')));
test('Short Date Time', () => expect(baseDate.format(TimestampStyle.shortDateTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:f>')));
test('Long Date Time', () => expect(baseDate.format(TimestampStyle.longDateTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:F>')));
test('Relative Time', () => expect(baseDate.format(TimestampStyle.relativeTime), equals('<t:${baseDate.millisecondsSinceEpoch ~/ 1000}:R>')));
});
}

0 comments on commit 0a3a8cf

Please sign in to comment.