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 extension getter for cdn assets #37

Merged
merged 9 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 11 additions & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() async {
// Sanitizing content makes it safe to send to Discord without triggering any mentions
client.onMessageCreate.listen((event) async {
if (event.message.content.startsWith('!sanitize')) {
event.message.channel.sendMessage(MessageBuilder(
await event.message.channel.sendMessage(MessageBuilder(
content: 'Sanitized content: ${await sanitizeContent(event.message.content, channel: event.message.channel)}',
));
}
Expand Down Expand Up @@ -67,5 +67,15 @@ ullamcorper morbi tincidunt ornare.
}
});

client.onMessageCreate.listen((event) async {
if (event.message.content.startsWith('!avatar') && event.message.mentions.isNotEmpty) {
// Display the first mentioned user's avatar with the specified size.
final user = event.message.mentions.first;
await event.message.channel.sendMessage(MessageBuilder(
content: 'Avatar URL: ${user.avatar.get(format: CdnFormat.jpeg, size: 3072)}',
));
}
});

// ...and more!
}
1 change: 1 addition & 0 deletions lib/nyxx_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'src/utils/pagination.dart';
export 'src/utils/permissions.dart';
export 'src/utils/sanitizer.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';
Expand Down
22 changes: 22 additions & 0 deletions lib/src/extensions/cdn_asset.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:nyxx/nyxx.dart';

extension CdnAssetExtensions on CdnAsset {
Uri get({CdnFormat? format, int? size}) => getRequest(this, format ?? defaultFormat, size).prepare(client).url;
}

// Re-implementing the private method from CdnAsset
CdnRequest getRequest(CdnAsset asset, CdnFormat format, int? size) {
final route = HttpRoute();

for (final part in asset.base.parts) {
route.add(part);
}
route.add(HttpRoutePart('${asset.hash}.${format.extension}'));

return CdnRequest(
route,
queryParameters: {
if (size != null) 'size': size.toString(),
},
);
}
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.2.0
nyxx: ^6.3.0

dev_dependencies:
coverage: ^1.0.3
Expand Down
11 changes: 11 additions & 0 deletions test/integration/member_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ void main() {
expect(mutualGuilds, isNotEmpty);
},
);

test('avatar.get()', skip: testToken == null ? 'No token provided' : false, () async {
final client = await Nyxx.connectRest(testToken!);

final self = await client.users.fetchCurrentUser();

expect(
self.avatar.get(size: 3072, format: CdnFormat.jpeg),
Uri.https('cdn.discordapp.com', 'avatars/${self.id}/${self.avatarHash}.${CdnFormat.jpeg.extension}', {'size': '3072'}),
);
});
}
40 changes: 8 additions & 32 deletions test/unit/timestamp_style_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,13 @@ 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('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>')));
});
}
Loading